Skip to content

Commit

Permalink
fix flaky unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiesheng committed Nov 26, 2024
1 parent 5bcc55b commit f08bd7c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
1 change: 0 additions & 1 deletion internal/worker/concurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions internal/worker/concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -60,23 +60,23 @@ 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,
expectFailuresAtLeast: 1,
},
{
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,
Expand Down

0 comments on commit f08bd7c

Please sign in to comment.