@@ -10,15 +10,18 @@ import (
1010 "reflect"
1111 "strconv"
1212 "strings"
13+ "sync"
14+ "sync/atomic"
1315 "testing"
1416 "time"
1517
16- "github.com/go-mysql-org/go-mysql/client"
17- "github.com/go-mysql-org/go-mysql/mysql"
18- "github.com/go-mysql-org/go-mysql/server"
1918 "github.com/pingcap/errors"
2019 "github.com/siddontang/go/log"
2120 "github.com/stretchr/testify/require"
21+
22+ "github.com/go-mysql-org/go-mysql/client"
23+ "github.com/go-mysql-org/go-mysql/mysql"
24+ "github.com/go-mysql-org/go-mysql/server"
2225)
2326
2427var _ server.Handler = & mockHandler {}
@@ -32,15 +35,19 @@ type testServer struct {
3235
3336type mockHandler struct {
3437 // the number of times a query executed
35- queryCount int
38+ queryCount atomic.Int32
39+ modifier * sync.WaitGroup
3640}
3741
3842func TestDriverOptions_SetRetriesOn (t * testing.T ) {
3943 log .SetLevel (log .LevelDebug )
4044 srv := CreateMockServer (t )
4145 defer srv .Stop ()
46+ var wg sync.WaitGroup
47+ srv .handler .modifier = & wg
48+ wg .Add (3 )
4249
43- conn ,
err := sql .
Open (
"mysql" ,
"[email protected] :3307/test?readTimeout=1s " )
50+ conn ,
err := sql .
Open (
"mysql" ,
"[email protected] :3307/test?readTimeout=100ms " )
4451 defer func () {
4552 _ = conn .Close ()
4653 }()
@@ -52,17 +59,21 @@ func TestDriverOptions_SetRetriesOn(t *testing.T) {
5259 // we want to get a golang database/sql/driver ErrBadConn
5360 require .ErrorIs (t , err , sqlDriver .ErrBadConn )
5461
62+ wg .Wait ()
5563 // here we issue assert that even though we only issued 1 query, that the retries
5664 // remained on and there were 3 calls to the DB.
57- require .Equal (t , 3 , srv .handler .queryCount )
65+ require .EqualValues (t , 3 , srv .handler .queryCount . Load () )
5866}
5967
6068func TestDriverOptions_SetRetriesOff (t * testing.T ) {
6169 log .SetLevel (log .LevelDebug )
6270 srv := CreateMockServer (t )
6371 defer srv .Stop ()
72+ var wg sync.WaitGroup
73+ srv .handler .modifier = & wg
74+ wg .Add (1 )
6475
65- conn ,
err := sql .
Open (
"mysql" ,
"[email protected] :3307/test?readTimeout=1s &retries=off" )
76+ conn ,
err := sql .
Open (
"mysql" ,
"[email protected] :3307/test?readTimeout=100ms &retries=off" )
6677 defer func () {
6778 _ = conn .Close ()
6879 }()
@@ -73,9 +84,10 @@ func TestDriverOptions_SetRetriesOff(t *testing.T) {
7384 // we want the native error from this driver implementation
7485 require .ErrorIs (t , err , mysql .ErrBadConn )
7586
87+ wg .Wait ()
7688 // here we issue assert that even though we only issued 1 query, that the retries
7789 // remained on and there were 3 calls to the DB.
78- require .Equal (t , 1 , srv .handler .queryCount )
90+ require .EqualValues (t , 1 , srv .handler .queryCount . Load () )
7991}
8092
8193func TestDriverOptions_SetCollation (t * testing.T ) {
@@ -153,7 +165,7 @@ func TestDriverOptions_ReadTimeout(t *testing.T) {
153165 srv := CreateMockServer (t )
154166 defer srv .Stop ()
155167
156- conn ,
err := sql .
Open (
"mysql" ,
"[email protected] :3307/test?readTimeout=1s " )
168+ conn ,
err := sql .
Open (
"mysql" ,
"[email protected] :3307/test?readTimeout=100ms " )
157169 defer func () {
158170 _ = conn .Close ()
159171 }()
@@ -309,7 +321,13 @@ func (h *mockHandler) UseDB(dbName string) error {
309321}
310322
311323func (h * mockHandler ) handleQuery (query string , binary bool , args []interface {}) (* mysql.Result , error ) {
312- h .queryCount ++
324+ defer func () {
325+ if h .modifier != nil {
326+ h .modifier .Done ()
327+ }
328+ }()
329+
330+ h .queryCount .Add (1 )
313331 ss := strings .Split (query , " " )
314332 switch strings .ToLower (ss [0 ]) {
315333 case "select" :
@@ -327,7 +345,7 @@ func (h *mockHandler) handleQuery(query string, binary bool, args []interface{})
327345 }, binary )
328346 } else {
329347 if strings .Contains (query , "slow" ) {
330- time .Sleep (time .Second * 5 )
348+ time .Sleep (time .Second )
331349 }
332350
333351 var aValue uint64 = 1
0 commit comments