Skip to content

Commit

Permalink
TestExecContextCancel: Reduce timeout to make less flaky (#879)
Browse files Browse the repository at this point in the history
This test fails fairly often. On my system, I can trigger it with:

    go test . -run=TestExecContextCancel -count=10 -failfast -v

This makes the test less flaky by timing out the context after a
consistent 50 millisecond delay. This was enough time for the query
to start, then get cancelled with sqlite3_interrupt() in my tests.
This now passes the above check.

This is a modified version of the change suggested in:
#865
  • Loading branch information
evanj committed Nov 16, 2020
1 parent 70c7709 commit 3fb3c0d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sqlite3_go18_test.go
Expand Up @@ -150,8 +150,11 @@ func TestExecContextCancel(t *testing.T) {
ts := time.Now()
initDatabase(t, db, 1000)
spent := time.Since(ts)
if spent < 100*time.Millisecond {
t.Skip("test will be too racy, as ExecContext below will be too fast.")
const minTestTime = 100 * time.Millisecond
if spent < minTestTime {
t.Skipf("test will be too racy (spent=%s < min=%s) as ExecContext below will be too fast.",
spent.String(), minTestTime.String(),
)
}

// expected to be extremely slow query
Expand All @@ -160,14 +163,17 @@ INSERT INTO test_table (key1, key_id, key2, key3, key4, key5, key6, data)
SELECT t1.key1 || t2.key1, t1.key_id || t2.key_id, t1.key2 || t2.key2, t1.key3 || t2.key3, t1.key4 || t2.key4, t1.key5 || t2.key5, t1.key6 || t2.key6, t1.data || t2.data
FROM test_table t1 LEFT OUTER JOIN test_table t2`
// expect query above take ~ same time as setup above
ctx, cancel := context.WithTimeout(context.Background(), spent/2)
// This is racy: the context must be valid so sql/db.ExecContext calls the sqlite3 driver.
// It starts the query, the context expires, then calls sqlite3_interrupt
ctx, cancel := context.WithTimeout(context.Background(), minTestTime/2)
defer cancel()
ts = time.Now()
r, err := db.ExecContext(ctx, q)
// racy check
if r != nil {
n, err := r.RowsAffected()
t.Log(n, err, time.Since(ts))
t.Logf("query should not have succeeded: rows=%d; err=%v; duration=%s",
n, err, time.Since(ts).String())
}
if err != context.DeadlineExceeded {
t.Fatal(err, ctx.Err())
Expand Down

0 comments on commit 3fb3c0d

Please sign in to comment.