Skip to content

Commit

Permalink
Split TickerRateLimiter test
Browse files Browse the repository at this point in the history
One verifies that ticker fires at all, the other checks if context is
checked. Otherwise it was nondeterministic which will be chosen (ticker
or context).
  • Loading branch information
AwesomePatrol committed Jun 19, 2024
1 parent 0c93851 commit b056f7e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/cloud/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,26 @@ func TestMinimumRateLimiter(t *testing.T) {
}
}

func TestTickerRateLimiter(t *testing.T) {
func TestTickerRateLimiter_Tick(t *testing.T) {
t.Parallel()

trl := NewTickerRateLimiter(100, time.Second)
err := trl.Accept(context.Background(), nil)
if err != nil {
t.Errorf("TickerRateLimiter.Accept = %v, want nil", err)
}
}

func TestTickerRateLimiter_Cancel(t *testing.T) {
t.Parallel()

trl := NewTickerRateLimiter(1, time.Hour)
// Use context that has been cancelled and expect a context error returned.
ctxCancelled, cancelled := context.WithCancel(context.Background())
cancelled()
// Verify context is cancelled by now.
<-ctxCancelled.Done()
err = trl.Accept(ctxCancelled, nil)
err := trl.Accept(ctxCancelled, nil)
if err != ctxCancelled.Err() {
t.Errorf("TickerRateLimiter.Accept() = %v, want %v", err, ctxCancelled.Err())
}
Expand Down

0 comments on commit b056f7e

Please sign in to comment.