From f08bd7cffab6131c826e50d68bb5e15a2f2a4d82 Mon Sep 17 00:00:00 2001 From: Shijie Sheng Date: Tue, 26 Nov 2024 10:15:22 -0800 Subject: [PATCH] fix flaky unit test --- internal/worker/concurrency.go | 1 - internal/worker/concurrency_test.go | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/internal/worker/concurrency.go b/internal/worker/concurrency.go index 65c0f695f..264c93cf7 100644 --- a/internal/worker/concurrency.go +++ b/internal/worker/concurrency.go @@ -69,7 +69,6 @@ func (p *permit) AcquireChan(ctx context.Context, wg *sync.WaitGroup) <-chan str wg.Add(1) go func() { defer wg.Done() - defer close(ch) // close channel when permit is acquired or expired if err := p.sem.Acquire(ctx, 1); err != nil { return } diff --git a/internal/worker/concurrency_test.go b/internal/worker/concurrency_test.go index 290a84d78..c21c84126 100644 --- a/internal/worker/concurrency_test.go +++ b/internal/worker/concurrency_test.go @@ -44,7 +44,7 @@ func TestPermit_Simulation(t *testing.T) { }{ { name: "enough permit, no blocking", - maxTestDuration: 200 * time.Millisecond, + maxTestDuration: 100 * time.Millisecond, capacity: []int{1000}, goroutines: 100, goroutinesAcquireChan: 100, @@ -60,7 +60,7 @@ func TestPermit_Simulation(t *testing.T) { }, { name: "not enough permit for some to acquire, fail some", - maxTestDuration: 100 * time.Millisecond, + maxTestDuration: 300 * time.Millisecond, capacity: []int{100}, goroutines: 500, goroutinesAcquireChan: 500, @@ -68,15 +68,15 @@ func TestPermit_Simulation(t *testing.T) { }, { name: "not enough permit at beginning but due to capacity change, blocking but all acquire", - maxTestDuration: 100 * time.Second, - capacity: []int{100, 200, 300}, + maxTestDuration: 300 * time.Millisecond, + capacity: []int{100, 300, 500}, goroutines: 500, goroutinesAcquireChan: 500, expectFailures: 0, }, { name: "not enough permit for any acquire, fail all", - maxTestDuration: 1 * time.Second, + maxTestDuration: 300 * time.Millisecond, capacity: []int{0}, goroutines: 1000, expectFailures: 1000,