database/sql: deflake query cancel tests

Rather then using a sleep in the fake DB, go to a channel
select and wait for the context to be done.

Fixes #18115

Change-Id: I6bc3a29db58c568d0a7ea06c2a354c18c9e798b2
Reviewed-on: https://go-review.googlesource.com/33712
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Daniel Theophanes 2016-11-30 09:30:31 -08:00 committed by Brad Fitzpatrick
parent f6bff1d587
commit 2a64ebfc6d
2 changed files with 26 additions and 5 deletions

View file

@ -511,6 +511,10 @@ func (c *fakeConn) prepareInsert(stmt *fakeStmt, parts []string) (*fakeStmt, err
var hookPrepareBadConn func() bool
func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
panic("use PrepareContext")
}
func (c *fakeConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
c.numPrepare++
if c.db == nil {
panic("nil c.db; conn = " + fmt.Sprintf("%#v", c))
@ -549,7 +553,13 @@ func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
parts = parts[1:]
if stmt.wait > 0 {
time.Sleep(stmt.wait)
wait := time.NewTimer(stmt.wait)
select {
case <-wait.C:
case <-ctx.Done():
wait.Stop()
return nil, ctx.Err()
}
}
c.incrStat(&c.stmtsMade)