@@ -418,26 +418,31 @@ func TestQueryContextWait(t *testing.T) {
418
418
defer closeDB (t , db )
419
419
prepares0 := numPrepares (t , db )
420
420
421
- // TODO(kardianos): convert this from using a timeout to using an explicit
422
- // cancel when the query signals that it is "executing" the query.
423
- ctx , cancel := context .WithTimeout (context .Background (), 300 * time .Millisecond )
421
+ ctx , cancel := context .WithCancel (context .Background ())
424
422
defer cancel ()
425
423
426
424
// This will trigger the *fakeConn.Prepare method which will take time
427
425
// performing the query. The ctxDriverPrepare func will check the context
428
426
// after this and close the rows and return an error.
429
- _ , err := db .QueryContext (ctx , "WAIT|1s|SELECT|people|age,name|" )
430
- if err != context .DeadlineExceeded {
427
+ c , err := db .Conn (ctx )
428
+ if err != nil {
429
+ t .Fatal (err )
430
+ }
431
+
432
+ c .dc .ci .(* fakeConn ).waiter = func (c context.Context ) {
433
+ cancel ()
434
+ <- ctx .Done ()
435
+ }
436
+ _ , err = c .QueryContext (ctx , "SELECT|people|age,name|" )
437
+ c .Close ()
438
+ if err != context .Canceled {
431
439
t .Fatalf ("expected QueryContext to error with context deadline exceeded but returned %v" , err )
432
440
}
433
441
434
442
// Verify closed rows connection after error condition.
435
443
waitForFree (t , db , 1 )
436
444
if prepares := numPrepares (t , db ) - prepares0 ; prepares != 1 {
437
- // TODO(kardianos): if the context timeouts before the db.QueryContext
438
- // executes this check may fail. After adjusting how the context
439
- // is canceled above revert this back to a Fatal error.
440
- t .Logf ("executed %d Prepare statements; want 1" , prepares )
445
+ t .Fatalf ("executed %d Prepare statements; want 1" , prepares )
441
446
}
442
447
}
443
448
@@ -455,14 +460,14 @@ func TestTxContextWait(t *testing.T) {
455
460
}
456
461
tx .keepConnOnRollback = false
457
462
458
- go func () {
459
- time .Sleep (15 * time .Millisecond )
463
+ tx .dc .ci .(* fakeConn ).waiter = func (c context.Context ) {
460
464
cancel ()
461
- }()
465
+ <- ctx .Done ()
466
+ }
462
467
// This will trigger the *fakeConn.Prepare method which will take time
463
468
// performing the query. The ctxDriverPrepare func will check the context
464
469
// after this and close the rows and return an error.
465
- _ , err = tx .QueryContext (ctx , "WAIT|1s| SELECT|people|age,name|" )
470
+ _ , err = tx .QueryContext (ctx , "SELECT|people|age,name|" )
466
471
if err != context .Canceled {
467
472
t .Fatalf ("expected QueryContext to error with context canceled but returned %v" , err )
468
473
}
0 commit comments