Skip to content

Commit

Permalink
TestConnConcurrency has been failing on CI
Browse files Browse the repository at this point in the history
This probably won't fix it, but at the very least we should not be
running assertions in a goroutine.
  • Loading branch information
jackc committed Jul 28, 2023
1 parent 1a9b2a5 commit d626dfe
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions stdlib/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"database/sql"
"encoding/json"
"fmt"
"math"
"os"
"reflect"
Expand Down Expand Up @@ -331,6 +332,7 @@ func TestConnConcurrency(t *testing.T) {
var wg sync.WaitGroup

concurrency := 50
errChan := make(chan error, concurrency)

for i := 1; i <= concurrency; i++ {
wg.Add(1)
Expand All @@ -344,14 +346,29 @@ func TestConnConcurrency(t *testing.T) {
str := strconv.Itoa(idx)
duration := time.Duration(idx) * time.Second
_, err := db.ExecContext(ctx, "insert into t values($1)", idx)
require.NoError(t, err)
if err != nil {
errChan <- fmt.Errorf("insert failed: %d %w", idx, err)
return
}
_, err = db.ExecContext(ctx, "update t set str = $1 where id = $2", str, idx)
require.NoError(t, err)
if err != nil {
errChan <- fmt.Errorf("update 1 failed: %d %w", idx, err)
return
}
_, err = db.ExecContext(ctx, "update t set dur_str = $1 where id = $2", duration, idx)
require.NoError(t, err)
if err != nil {
errChan <- fmt.Errorf("update 2 failed: %d %w", idx, err)
return
}

errChan <- nil
}(i)
}
wg.Wait()
for i := 1; i <= concurrency; i++ {
err := <-errChan
require.NoError(t, err)
}

for i := 1; i <= concurrency; i++ {
wg.Add(1)
Expand All @@ -366,17 +383,26 @@ func TestConnConcurrency(t *testing.T) {
var str string
var duration pgtype.Interval
err := db.QueryRowContext(ctx, "select id,str,dur_str from t where id = $1", idx).Scan(&id, &str, &duration)
require.NoError(t, err)
if err != nil {
errChan <- fmt.Errorf("select failed: %d %w", idx, err)
return
}
require.Equal(t, idx, id)
require.Equal(t, strconv.Itoa(idx), str)
expectedDuration := pgtype.Interval{
Microseconds: int64(idx) * time.Second.Microseconds(),
Valid: true,
}
require.Equal(t, expectedDuration, duration)

errChan <- nil
}(i)
}
wg.Wait()
for i := 1; i <= concurrency; i++ {
err := <-errChan
require.NoError(t, err)
}
})
}

Expand Down

0 comments on commit d626dfe

Please sign in to comment.