Skip to content

Commit 05089fd

Browse files
committed
Merge in julienschmidt's suggested improvements.
1 parent fb0dc84 commit 05089fd

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

driver_test.go

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,20 +1223,7 @@ func TestConcurrent(t *testing.T) {
12231223
}
12241224
dbt.Logf("Testing up to %d concurrent connections \r\n", max)
12251225

1226-
canStopVal := false
1227-
var canStopM sync.Mutex
1228-
canStop := func() bool {
1229-
canStopM.Lock()
1230-
defer canStopM.Unlock()
1231-
return canStopVal
1232-
}
1233-
setCanStop := func() {
1234-
canStopM.Lock()
1235-
defer canStopM.Unlock()
1236-
canStopVal = true
1237-
}
1238-
1239-
var succeeded int32
1226+
var remaining, succeeded int32 = int32(max), 0
12401227

12411228
var wg sync.WaitGroup
12421229
wg.Add(max)
@@ -1254,41 +1241,40 @@ func TestConcurrent(t *testing.T) {
12541241
defer wg.Done()
12551242

12561243
tx, err := dbt.db.Begin()
1244+
atomic.AddInt32(&remaining, -1)
1245+
12571246
if err != nil {
1258-
setCanStop()
1259-
if err.Error() == "Error 1040: Too many connections" {
1260-
return
1261-
} else {
1262-
fatal("Error on Con %d: %s", id, err.Error())
1247+
if err.Error() != "Error 1040: Too many connections" {
1248+
fatal("Error on Conn %d: %s", id, err.Error())
12631249
}
1250+
return
12641251
}
12651252

1266-
atomic.AddInt32(&succeeded, 1)
1267-
1268-
var hasSelected bool
1269-
for !canStop() || !hasSelected {
1270-
_, err = tx.Exec("SELECT 1")
1271-
if err != nil {
1272-
setCanStop()
1273-
fatal("Error on Con %d: %s", id, err.Error())
1253+
// keep the connection busy until all connections are open
1254+
for remaining > 0 {
1255+
if _, err = tx.Exec("DO 1"); err != nil {
1256+
fatal("Error on Conn %d: %s", id, err.Error())
1257+
return
12741258
}
1275-
hasSelected = true
12761259
}
1277-
err = tx.Commit()
1278-
if err != nil {
1279-
fatal("Error on Con %d: %s", id, err.Error())
1260+
1261+
if err = tx.Commit(); err != nil {
1262+
fatal("Error on Conn %d: %s", id, err.Error())
1263+
return
12801264
}
1265+
1266+
// everything went fine with this connection
1267+
atomic.AddInt32(&succeeded, 1)
12811268
}(i)
12821269
}
12831270

1284-
setCanStop()
1285-
1271+
// wait until all conections are open
12861272
wg.Wait()
12871273

12881274
if fatalError != "" {
12891275
dbt.Fatal(fatalError)
12901276
}
12911277

1292-
dbt.Logf("Reached %d concurrent connections \r\n", succeeded)
1278+
dbt.Logf("Reached %d concurrent connections\r\n", succeeded)
12931279
})
12941280
}

0 commit comments

Comments
 (0)