Skip to content

Commit a289e9c

Browse files
committed
database/sql: make WAIT tests more robust, rely on waiter trigger
Replace the WAIT query prefix with a function callback. This fixes timing issues when the testing on loaded servers. Fixes #51208 Change-Id: I5151b397b7066c27ce6bc02c160dde0b584934bc Reviewed-on: https://go-review.googlesource.com/c/go/+/385934 Run-TryBot: Daniel Theophanes <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Trust: Daniel Theophanes <[email protected]>
1 parent f985833 commit a289e9c

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/database/sql/fakedb_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ func (c *fakeConn) PrepareContext(ctx context.Context, query string) (driver.Stm
676676

677677
if c.waiter != nil {
678678
c.waiter(ctx)
679+
if err := ctx.Err(); err != nil {
680+
return nil, err
681+
}
679682
}
680683

681684
if stmt.wait > 0 {

src/database/sql/sql_test.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -418,26 +418,31 @@ func TestQueryContextWait(t *testing.T) {
418418
defer closeDB(t, db)
419419
prepares0 := numPrepares(t, db)
420420

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())
424422
defer cancel()
425423

426424
// This will trigger the *fakeConn.Prepare method which will take time
427425
// performing the query. The ctxDriverPrepare func will check the context
428426
// 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 {
431439
t.Fatalf("expected QueryContext to error with context deadline exceeded but returned %v", err)
432440
}
433441

434442
// Verify closed rows connection after error condition.
435443
waitForFree(t, db, 1)
436444
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)
441446
}
442447
}
443448

@@ -455,14 +460,14 @@ func TestTxContextWait(t *testing.T) {
455460
}
456461
tx.keepConnOnRollback = false
457462

458-
go func() {
459-
time.Sleep(15 * time.Millisecond)
463+
tx.dc.ci.(*fakeConn).waiter = func(c context.Context) {
460464
cancel()
461-
}()
465+
<-ctx.Done()
466+
}
462467
// This will trigger the *fakeConn.Prepare method which will take time
463468
// performing the query. The ctxDriverPrepare func will check the context
464469
// 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|")
466471
if err != context.Canceled {
467472
t.Fatalf("expected QueryContext to error with context canceled but returned %v", err)
468473
}

0 commit comments

Comments
 (0)