From 05c40f74c9fae3db345d092b7f191452f302c60c Mon Sep 17 00:00:00 2001 From: reshke Date: Tue, 19 Mar 2024 09:31:01 +0000 Subject: [PATCH 1/2] fix --- pkg/pool/shardpool_test.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/pool/shardpool_test.go b/pkg/pool/shardpool_test.go index a24980a17..f7d6270a0 100644 --- a/pkg/pool/shardpool_test.go +++ b/pkg/pool/shardpool_test.go @@ -3,7 +3,9 @@ package pool_test import ( "errors" "fmt" + "math/rand" "sync" + "sync/atomic" "testing" "time" @@ -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()] { @@ -173,7 +173,8 @@ func TestShardPoolConnectionAcquireLimit(t *testing.T) { }) var wg sync.WaitGroup - cntExec := 0 + + var cntExec atomic.Uint64 wg.Add(20) @@ -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() } @@ -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(), 15*100) } From db7f391681d0904483ad511a5d4286473c781ecb Mon Sep 17 00:00:00 2001 From: reshke Date: Tue, 19 Mar 2024 09:32:06 +0000 Subject: [PATCH 2/2] fix type --- pkg/pool/shardpool_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/pool/shardpool_test.go b/pkg/pool/shardpool_test.go index f7d6270a0..4e3b3b3a8 100644 --- a/pkg/pool/shardpool_test.go +++ b/pkg/pool/shardpool_test.go @@ -211,5 +211,5 @@ func TestShardPoolConnectionAcquireLimit(t *testing.T) { wg.Wait() // no more that 25% failure - assert.Greater(cntExec.Load(), 15*100) + assert.Greater(cntExec.Load(), uint64(15*100)) }