Skip to content
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

fix unittest race #561

Merged
merged 2 commits into from
Mar 19, 2024
Merged
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
19 changes: 8 additions & 11 deletions pkg/pool/shardpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package pool_test
import (
"errors"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -150,12 +152,10 @@ func TestShardPoolConnectionAcquireLimit(t *testing.T) {
}

var mu sync.Mutex
cntAcq := 0

shp := pool.NewShardPool(func(shardKey kr.ShardKey, host string, rule *config.BackendRule) (shard.Shard, error) {
mu.Lock()
defer mu.Unlock()
cntAcq++

for _, sh := range conns {
if !used[sh.ID()] {
Expand All @@ -173,7 +173,8 @@ func TestShardPoolConnectionAcquireLimit(t *testing.T) {
})

var wg sync.WaitGroup
cntExec := 0

var cntExec atomic.Uint64

wg.Add(20)

Expand All @@ -187,24 +188,20 @@ func TestShardPoolConnectionAcquireLimit(t *testing.T) {
})
if err != nil {
// too much connections
assert.Less(cntAcq, connLimit)
continue
}

assert.NotNil(conn)

// imitate use
time.Sleep(50 * time.Millisecond)

cntExec++
time.Sleep(time.Duration(1+rand.Uint32()%50) * time.Millisecond)

mu.Lock()
cntExec.Add(1)

used[conn.ID()] = false

cntAcq--

shp.Put(conn)
_ = shp.Put(conn)

mu.Unlock()
}
Expand All @@ -214,5 +211,5 @@ func TestShardPoolConnectionAcquireLimit(t *testing.T) {
wg.Wait()

// no more that 25% failure
assert.Greater(cntExec, 15*100)
assert.Greater(cntExec.Load(), uint64(15*100))
}
Loading