From e4c70c17197bb86122f33150e0e7b45a93912492 Mon Sep 17 00:00:00 2001 From: Kyle Xiao Date: Wed, 25 Sep 2024 11:02:03 +0800 Subject: [PATCH] tests(limiter): fix unstable TestConnectionLimiter (#1560) --- pkg/limiter/connection_limiter_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/limiter/connection_limiter_test.go b/pkg/limiter/connection_limiter_test.go index 4e92ded29f..8266a188fc 100644 --- a/pkg/limiter/connection_limiter_test.go +++ b/pkg/limiter/connection_limiter_test.go @@ -27,26 +27,28 @@ import ( ) func TestConnectionLimiter(t *testing.T) { - connLimit := 50 + const ( + concurrency = 10 + connLimit = 1000 + ) ctx := context.Background() lim := NewConnectionLimiter(connLimit) var wg sync.WaitGroup var connCount int32 - for i := 0; i < 100; i++ { + for i := 0; i < concurrency; i++ { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < 10; j++ { + time.Sleep(4 * time.Millisecond) // wait other goroutines ready to start + for j := 0; j < connLimit; j++ { if lim.Acquire(ctx) { atomic.AddInt32(&connCount, 1) - time.Sleep(time.Millisecond) } else { lim.Release(ctx) - time.Sleep(time.Millisecond) } _, curr := lim.Status(ctx) - test.Assert(t, curr <= 60, curr) + test.Assert(t, curr <= connLimit+concurrency, curr) } }() }