@@ -1223,20 +1223,7 @@ func TestConcurrent(t *testing.T) {
1223
1223
}
1224
1224
dbt .Logf ("Testing up to %d concurrent connections \r \n " , max )
1225
1225
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
1240
1227
1241
1228
var wg sync.WaitGroup
1242
1229
wg .Add (max )
@@ -1254,41 +1241,40 @@ func TestConcurrent(t *testing.T) {
1254
1241
defer wg .Done ()
1255
1242
1256
1243
tx , err := dbt .db .Begin ()
1244
+ atomic .AddInt32 (& remaining , - 1 )
1245
+
1257
1246
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 ())
1263
1249
}
1250
+ return
1264
1251
}
1265
1252
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
1274
1258
}
1275
- hasSelected = true
1276
1259
}
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
1280
1264
}
1265
+
1266
+ // everything went fine with this connection
1267
+ atomic .AddInt32 (& succeeded , 1 )
1281
1268
}(i )
1282
1269
}
1283
1270
1284
- setCanStop ()
1285
-
1271
+ // wait until all conections are open
1286
1272
wg .Wait ()
1287
1273
1288
1274
if fatalError != "" {
1289
1275
dbt .Fatal (fatalError )
1290
1276
}
1291
1277
1292
- dbt .Logf ("Reached %d concurrent connections \r \n " , succeeded )
1278
+ dbt .Logf ("Reached %d concurrent connections\r \n " , succeeded )
1293
1279
})
1294
1280
}
0 commit comments