Skip to content

Commit e3476d9

Browse files
committed
add -race
Signed-off-by: lance6716 <[email protected]>
1 parent 2bb8fe3 commit e3476d9

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ jobs:
3939
- name: Run tests
4040
run: |
4141
# separate test to avoid RESET MASTER conflict
42-
go test $(go list ./... | grep -v canal)
43-
go test $(go list ./... | grep canal)
42+
go test -race $(go list ./... | grep -v canal)
43+
go test -race $(go list ./... | grep canal)
4444
4545
mysqltest:
4646
strategy:

driver/driver_options_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"reflect"
1111
"strconv"
1212
"strings"
13+
"sync"
1314
"sync/atomic"
1415
"testing"
1516
"time"
@@ -35,14 +36,18 @@ type testServer struct {
3536
type mockHandler struct {
3637
// the number of times a query executed
3738
queryCount atomic.Int32
39+
modifier *sync.WaitGroup
3840
}
3941

4042
func TestDriverOptions_SetRetriesOn(t *testing.T) {
4143
log.SetLevel(log.LevelDebug)
4244
srv := CreateMockServer(t)
4345
defer srv.Stop()
46+
var wg sync.WaitGroup
47+
srv.handler.modifier = &wg
48+
wg.Add(3)
4449

45-
conn, err := sql.Open("mysql", "[email protected]:3307/test?readTimeout=1s")
50+
conn, err := sql.Open("mysql", "[email protected]:3307/test?readTimeout=100ms")
4651
defer func() {
4752
_ = conn.Close()
4853
}()
@@ -54,6 +59,7 @@ func TestDriverOptions_SetRetriesOn(t *testing.T) {
5459
// we want to get a golang database/sql/driver ErrBadConn
5560
require.ErrorIs(t, err, sqlDriver.ErrBadConn)
5661

62+
wg.Wait()
5763
// here we issue assert that even though we only issued 1 query, that the retries
5864
// remained on and there were 3 calls to the DB.
5965
require.EqualValues(t, 3, srv.handler.queryCount.Load())
@@ -63,8 +69,11 @@ func TestDriverOptions_SetRetriesOff(t *testing.T) {
6369
log.SetLevel(log.LevelDebug)
6470
srv := CreateMockServer(t)
6571
defer srv.Stop()
72+
var wg sync.WaitGroup
73+
srv.handler.modifier = &wg
74+
wg.Add(1)
6675

67-
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")
6877
defer func() {
6978
_ = conn.Close()
7079
}()
@@ -75,6 +84,7 @@ func TestDriverOptions_SetRetriesOff(t *testing.T) {
7584
// we want the native error from this driver implementation
7685
require.ErrorIs(t, err, mysql.ErrBadConn)
7786

87+
wg.Wait()
7888
// here we issue assert that even though we only issued 1 query, that the retries
7989
// remained on and there were 3 calls to the DB.
8090
require.EqualValues(t, 1, srv.handler.queryCount.Load())
@@ -311,6 +321,12 @@ func (h *mockHandler) UseDB(dbName string) error {
311321
}
312322

313323
func (h *mockHandler) handleQuery(query string, binary bool, args []interface{}) (*mysql.Result, error) {
324+
defer func() {
325+
if h.modifier != nil {
326+
h.modifier.Done()
327+
}
328+
}()
329+
314330
h.queryCount.Add(1)
315331
ss := strings.Split(query, " ")
316332
switch strings.ToLower(ss[0]) {
@@ -329,7 +345,7 @@ func (h *mockHandler) handleQuery(query string, binary bool, args []interface{})
329345
}, binary)
330346
} else {
331347
if strings.Contains(query, "slow") {
332-
time.Sleep(time.Second * 5)
348+
time.Sleep(time.Second)
333349
}
334350

335351
var aValue uint64 = 1

0 commit comments

Comments
 (0)