TestExecContextCancel: Reduce timeout to make less flaky (#879)

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:
https://github.com/mattn/go-sqlite3/pull/865
move-code
Evan Jones 2020-11-16 11:52:56 -05:00 committed by GitHub
parent 70c77097f2
commit 3fb3c0de37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -150,8 +150,11 @@ func TestExecContextCancel(t *testing.T) {
ts := time.Now() ts := time.Now()
initDatabase(t, db, 1000) initDatabase(t, db, 1000)
spent := time.Since(ts) spent := time.Since(ts)
if spent < 100*time.Millisecond { const minTestTime = 100 * time.Millisecond
t.Skip("test will be too racy, as ExecContext below will be too fast.") 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 // expected to be extremely slow query
@ -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 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` FROM test_table t1 LEFT OUTER JOIN test_table t2`
// expect query above take ~ same time as setup above // 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() defer cancel()
ts = time.Now() ts = time.Now()
r, err := db.ExecContext(ctx, q) r, err := db.ExecContext(ctx, q)
// racy check // racy check
if r != nil { if r != nil {
n, err := r.RowsAffected() 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 { if err != context.DeadlineExceeded {
t.Fatal(err, ctx.Err()) t.Fatal(err, ctx.Err())