Skip to content

Commit 3a665d0

Browse files
Fix actual master and add -race in CI (#907)
* fix a linter warnings * fix a datarace in tests * add `-race` Signed-off-by: lance6716 <[email protected]> * fix UT Signed-off-by: lance6716 <[email protected]> --------- Signed-off-by: lance6716 <[email protected]> Co-authored-by: lance6716 <[email protected]>
1 parent 54cc110 commit 3a665d0

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

.github/workflows/ci.yml

+2-2
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

+29-11
Original file line numberDiff line numberDiff line change
@@ -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

2427
var _ server.Handler = &mockHandler{}
@@ -32,15 +35,19 @@ type testServer struct {
3235

3336
type mockHandler struct {
3437
// the number of times a query executed
35-
queryCount int
38+
queryCount atomic.Int32
39+
modifier *sync.WaitGroup
3640
}
3741

3842
func 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

6068
func 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

8193
func 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

311323
func (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

Comments
 (0)