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

@ -24,6 +24,17 @@ func init() {
c *driverConn
}
freedFrom := make(map[dbConn]string)
var mu sync.Mutex
getFreedFrom := func(c dbConn) string {
mu.Lock()
defer mu.Unlock()
return freedFrom[c]
}
setFreedFrom := func(c dbConn, s string) {
mu.Lock()
defer mu.Unlock()
freedFrom[c] = s
}
putConnHook = func(db *DB, c *driverConn) {
idx := -1
for i, v := range db.freeConn {
@ -36,10 +47,10 @@ func init() {
// print before panic, as panic may get lost due to conflicting panic
// (all goroutines asleep) elsewhere, since we might not unlock
// the mutex in freeConn here.
println("double free of conn. conflicts are:\nA) " + freedFrom[dbConn{db, c}] + "\n\nand\nB) " + stack())
println("double free of conn. conflicts are:\nA) " + getFreedFrom(dbConn{db, c}) + "\n\nand\nB) " + stack())
panic("double free of conn.")
}
freedFrom[dbConn{db, c}] = stack()
setFreedFrom(dbConn{db, c}, stack())
}
}
@ -344,7 +355,7 @@ func TestQueryContextWait(t *testing.T) {
// This will trigger the *fakeConn.Prepare method which will take time
// performing the query. The ctxDriverPrepare func will check the context
// after this and close the rows and return an error.
_, err := db.QueryContext(ctx, "WAIT|30ms|SELECT|people|age,name|")
_, err := db.QueryContext(ctx, "WAIT|1s|SELECT|people|age,name|")
if err != context.DeadlineExceeded {
t.Fatalf("expected QueryContext to error with context deadline exceeded but returned %v", err)
}
@ -372,7 +383,7 @@ func TestTxContextWait(t *testing.T) {
// This will trigger the *fakeConn.Prepare method which will take time
// performing the query. The ctxDriverPrepare func will check the context
// after this and close the rows and return an error.
_, err = tx.QueryContext(ctx, "WAIT|30ms|SELECT|people|age,name|")
_, err = tx.QueryContext(ctx, "WAIT|1s|SELECT|people|age,name|")
if err != context.DeadlineExceeded {
t.Fatalf("expected QueryContext to error with context deadline exceeded but returned %v", err)
}