Skip to content

Fix the TestConcurrent test to pass Go's race detection. #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Xiuming Chen <cc at cxm.cc>

# Organizations

Barracuda Networks, Inc.
Google Inc.
65 changes: 43 additions & 22 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
Expand Down Expand Up @@ -1220,40 +1222,59 @@ func TestConcurrent(t *testing.T) {
dbt.Fatalf("%s", err.Error())
}
dbt.Logf("Testing up to %d concurrent connections \r\n", max)
canStop := false
c := make(chan struct{}, max)

var remaining, succeeded int32 = int32(max), 0

var wg sync.WaitGroup
wg.Add(max)

var fatalError string
var once sync.Once
fatal := func(s string, vals ...interface{}) {
once.Do(func() {
fatalError = fmt.Sprintf(s, vals...)
})
}

for i := 0; i < max; i++ {
go func(id int) {
defer wg.Done()

tx, err := dbt.db.Begin()
atomic.AddInt32(&remaining, -1)

if err != nil {
canStop = true
if err.Error() == "Error 1040: Too many connections" {
max--
return
} else {
dbt.Fatalf("Error on Con %d: %s", id, err.Error())
if err.Error() != "Error 1040: Too many connections" {
fatal("Error on Conn %d: %s", id, err.Error())
}
return
}
c <- struct{}{}
for !canStop {
_, err = tx.Exec("SELECT 1")
if err != nil {
canStop = true
dbt.Fatalf("Error on Con %d: %s", id, err.Error())

// keep the connection busy until all connections are open
for remaining > 0 {
if _, err = tx.Exec("DO 1"); err != nil {
fatal("Error on Conn %d: %s", id, err.Error())
return
}
}
err = tx.Commit()
if err != nil {
canStop = true
dbt.Fatalf("Error on Con %d: %s", id, err.Error())

if err = tx.Commit(); err != nil {
fatal("Error on Conn %d: %s", id, err.Error())
return
}

// everything went fine with this connection
atomic.AddInt32(&succeeded, 1)
}(i)
}
for i := 0; i < max; i++ {
<-c

// wait until all conections are open
wg.Wait()

if fatalError != "" {
dbt.Fatal(fatalError)
}
canStop = true

dbt.Logf("Reached %d concurrent connections \r\n", max)
dbt.Logf("Reached %d concurrent connections\r\n", succeeded)
})
}