Skip to content

Commit 2d7f728

Browse files
committed
fix a datarace in tests
1 parent e112011 commit 2d7f728

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

driver/driver_options_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ import (
1010
"reflect"
1111
"strconv"
1212
"strings"
13+
"sync/atomic"
1314
"testing"
1415
"time"
1516

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"
1917
"github.com/pingcap/errors"
2018
"github.com/siddontang/go/log"
2119
"github.com/stretchr/testify/require"
20+
21+
"github.com/go-mysql-org/go-mysql/client"
22+
"github.com/go-mysql-org/go-mysql/mysql"
23+
"github.com/go-mysql-org/go-mysql/server"
2224
)
2325

2426
var _ server.Handler = &mockHandler{}
@@ -32,7 +34,7 @@ type testServer struct {
3234

3335
type mockHandler struct {
3436
// the number of times a query executed
35-
queryCount int
37+
queryCount atomic.Int32
3638
}
3739

3840
func TestDriverOptions_SetRetriesOn(t *testing.T) {
@@ -54,7 +56,7 @@ func TestDriverOptions_SetRetriesOn(t *testing.T) {
5456

5557
// here we issue assert that even though we only issued 1 query, that the retries
5658
// remained on and there were 3 calls to the DB.
57-
require.Equal(t, 3, srv.handler.queryCount)
59+
require.EqualValues(t, 3, srv.handler.queryCount.Load())
5860
}
5961

6062
func TestDriverOptions_SetRetriesOff(t *testing.T) {
@@ -75,7 +77,7 @@ func TestDriverOptions_SetRetriesOff(t *testing.T) {
7577

7678
// here we issue assert that even though we only issued 1 query, that the retries
7779
// remained on and there were 3 calls to the DB.
78-
require.Equal(t, 1, srv.handler.queryCount)
80+
require.EqualValues(t, 1, srv.handler.queryCount.Load())
7981
}
8082

8183
func TestDriverOptions_SetCollation(t *testing.T) {
@@ -309,7 +311,7 @@ func (h *mockHandler) UseDB(dbName string) error {
309311
}
310312

311313
func (h *mockHandler) handleQuery(query string, binary bool, args []interface{}) (*mysql.Result, error) {
312-
h.queryCount++
314+
h.queryCount.Add(1)
313315
ss := strings.Split(query, " ")
314316
switch strings.ToLower(ss[0]) {
315317
case "select":

0 commit comments

Comments
 (0)