lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.04k stars 909 forks source link

Fix flaky TestConnPrepareContext #1061

Closed mjl- closed 2 years ago

mjl- commented 2 years ago

TestConnPrepareContext checks it receives an context.DeadlineExceeded. However, the context isn't necessarily always expired. This causes most of the test runs to fail for me, only occasionally succeeding. This change ensures the context used in the test is actually context.DeadlineExceeded. The negative duration causes the context package to return an already canceled context.

mjl- commented 2 years ago

This is the result I am seeing (on lib/pq master, with go1.17.2 linux/amd64) without this PR applied:

$ PGHOST=localhost go test -count=1 -run TestConnPrepareContext github.com/lib/pq
--- FAIL: TestConnPrepareContext (0.01s)
    --- FAIL: TestConnPrepareContext/context.WithTimeout_exceeded (0.00s)
        conn_test.go:1854: conn.PrepareContext() unexpected nil err got = <nil>, exp
FAIL
FAIL    github.com/lib/pq       0.009s
FAIL

For completeness, I'm starting my postgres like this:

export PG=/usr/lib/postgresql/13
$PG/bin/initdb -D postgres13
$PG/bin/postgres -D postgres13 -p 5432 -k '' 2>&1 | tee postgres13/postgres.log
$PG/bin/createdb -h localhost pqgotest