Skip to content

Commit 0beb854

Browse files
fix race condition in test, use testify assertions
1 parent 3cce9a7 commit 0beb854

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

server/server_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"database/sql"
66
"net"
77
"testing"
8+
"time"
89

910
vsql "github.com/dolthub/vitess/go/mysql"
1011
"github.com/go-sql-driver/mysql"
12+
"github.com/stretchr/testify/require"
1113

1214
sqle "github.com/dolthub/go-mysql-server"
1315
"github.com/dolthub/go-mysql-server/memory"
@@ -44,14 +46,7 @@ func TestSeverCustomListener(t *testing.T) {
4446
return memory.NewSession(gsql.NewBaseSessionWithClientServer(addr, client, c.ConnectionID), pro), nil
4547
}
4648
s, err := server.NewServer(cfg, engine, sessionBuilder, nil)
47-
if err != nil {
48-
t.Fatal(err)
49-
}
50-
go func() {
51-
if err = s.Start(); err != nil {
52-
panic(err)
53-
}
54-
}()
49+
require.NoError(t, err)
5550

5651
networkName := "testNetwork"
5752
// wire up go-mysql-driver to the listener
@@ -66,28 +61,33 @@ func TestSeverCustomListener(t *testing.T) {
6661
User: "root",
6762
AllowNativePasswords: true,
6863
})
69-
if err != nil {
70-
t.Fatal(err)
71-
}
64+
require.NoError(t, err)
65+
66+
// start go-mysql-server
67+
go func() {
68+
err := s.Start()
69+
require.NoError(t, err)
70+
}()
7271

7372
// open the db, ping it, and run some execs/queries
7473
db := sql.OpenDB(driver)
75-
err = db.Ping()
76-
if err != nil {
77-
t.Fatal(err)
74+
75+
var pingErr error
76+
for i := 0; i < 3; i++ {
77+
if pingErr = db.Ping(); pingErr == nil {
78+
break
79+
}
80+
time.Sleep(time.Second)
7881
}
82+
require.NoError(t, err)
7983

8084
_, err = db.Exec("CREATE TABLE table1 (id int)")
81-
if err != nil {
82-
t.Fatal(err)
83-
}
85+
require.NoError(t, err)
8486

8587
row := db.QueryRow("SHOW TABLES")
8688
var tableName string
8789
err = row.Scan(&tableName)
88-
if err != nil {
89-
t.Fatal(err)
90-
}
90+
require.NoError(t, err)
9191
if tableName != "table1" {
9292
t.Fatalf("expected to find table1, but got %s", tableName)
9393
}

0 commit comments

Comments
 (0)