Skip to content

Commit 14e5817

Browse files
committed
Add test for go-sql-driver#858
1 parent 99ff426 commit 14e5817

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

connection_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package mysql
1010

1111
import (
12+
"context"
1213
"database/sql/driver"
1314
"testing"
1415
)
@@ -79,3 +80,31 @@ func TestCheckNamedValue(t *testing.T) {
7980
t.Fatalf("uint64 high-bit not converted, got %#v %T", value.Value, value.Value)
8081
}
8182
}
83+
84+
// TestCleanCancel tests passed context is cancelled at start.
85+
// No packet should be sent. Connection should keep current status.
86+
func TestCleanCancel(t *testing.T) {
87+
mc := &mysqlConn{
88+
closech: make(chan struct{}),
89+
}
90+
mc.startWatcher()
91+
defer mc.cleanup()
92+
93+
ctx, cancel := context.WithCancel(context.Background())
94+
cancel()
95+
96+
for i := 0; i < 3; i++ { // Repeat same behavior
97+
err := mc.Ping(ctx)
98+
if err != context.Canceled {
99+
t.Errorf("expected context.Canceled, got %#v", err)
100+
}
101+
102+
if mc.closed.IsSet() {
103+
t.Error("expected mc is not closed, closed actually")
104+
}
105+
106+
if mc.watching {
107+
t.Error("expected watching is false, but true")
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)