Skip to content

Commit

Permalink
add -race
Browse files Browse the repository at this point in the history
Signed-off-by: lance6716 <[email protected]>
  • Loading branch information
lance6716 committed Sep 10, 2024
1 parent 2bb8fe3 commit e3476d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
- name: Run tests
run: |
# separate test to avoid RESET MASTER conflict
go test $(go list ./... | grep -v canal)
go test $(go list ./... | grep canal)
go test -race $(go list ./... | grep -v canal)
go test -race $(go list ./... | grep canal)
mysqltest:
strategy:
Expand Down
22 changes: 19 additions & 3 deletions driver/driver_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand All @@ -35,14 +36,18 @@ type testServer struct {
type mockHandler struct {
// the number of times a query executed
queryCount atomic.Int32
modifier *sync.WaitGroup
}

func TestDriverOptions_SetRetriesOn(t *testing.T) {
log.SetLevel(log.LevelDebug)
srv := CreateMockServer(t)
defer srv.Stop()
var wg sync.WaitGroup
srv.handler.modifier = &wg
wg.Add(3)

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

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

conn, err := sql.Open("mysql", "[email protected]:3307/test?readTimeout=1s&retries=off")
conn, err := sql.Open("mysql", "[email protected]:3307/test?readTimeout=100ms&retries=off")
defer func() {
_ = conn.Close()
}()
Expand All @@ -75,6 +84,7 @@ func TestDriverOptions_SetRetriesOff(t *testing.T) {
// we want the native error from this driver implementation
require.ErrorIs(t, err, mysql.ErrBadConn)

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

func (h *mockHandler) handleQuery(query string, binary bool, args []interface{}) (*mysql.Result, error) {
defer func() {
if h.modifier != nil {
h.modifier.Done()
}
}()

h.queryCount.Add(1)
ss := strings.Split(query, " ")
switch strings.ToLower(ss[0]) {
Expand All @@ -329,7 +345,7 @@ func (h *mockHandler) handleQuery(query string, binary bool, args []interface{})
}, binary)
} else {
if strings.Contains(query, "slow") {
time.Sleep(time.Second * 5)
time.Sleep(time.Second)
}

var aValue uint64 = 1
Expand Down

0 comments on commit e3476d9

Please sign in to comment.