Skip to content

Commit 42cb96e

Browse files
committed
test: Implement TestConnectorObeysDialTimeouts
1 parent 3ce2991 commit 42cb96e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

driver_go110_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,37 @@
1111
package mysql
1212

1313
import (
14+
"context"
15+
"database/sql"
1416
"database/sql/driver"
17+
"fmt"
18+
"net"
19+
"testing"
1520
)
1621

1722
var _ driver.DriverContext = &MySQLDriver{}
23+
24+
type dialCtxKey struct{}
25+
26+
func TestConnectorObeysDialTimeouts(t *testing.T) {
27+
RegisterDialContext("dialctxtest", func(ctx context.Context, addr string) (net.Conn, error) {
28+
var d net.Dialer
29+
if !ctx.Value(dialCtxKey{}).(bool) {
30+
return nil, fmt.Errorf("test error: query context is not propagated to our dialer")
31+
}
32+
return d.DialContext(ctx, prot, addr)
33+
})
34+
35+
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@dialctxtest(%s)/%s?timeout=30s", user, pass, addr, dbname))
36+
if err != nil {
37+
t.Fatalf("error connecting: %s", err.Error())
38+
}
39+
defer db.Close()
40+
41+
ctx := context.WithValue(context.Background(), dialCtxKey{}, true)
42+
43+
_, err = db.ExecContext(ctx, "DO 1")
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
}

0 commit comments

Comments
 (0)