Skip to content

Commit

Permalink
fix: rate intermittent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lherman-cs committed May 23, 2024
1 parent 47e4c6b commit fe57a6c
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions utils/rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ func (r *runnerImpl) goWait(fn func()) {
}

func TestRateLimiter(t *testing.T) {
t.Parallel()
runTest(t, func(r testRunner) {
rl := r.createLimiter(100, WithoutSlack)

Expand All @@ -288,7 +287,6 @@ func TestRateLimiter(t *testing.T) {

func TestDelayedRateLimiter(t *testing.T) {
t.Skip(UnstableTest)
t.Parallel()
runTest(t, func(r testRunner) {
slow := r.createLimiter(10, WithoutSlack)
fast := r.createLimiter(100, WithoutSlack)
Expand All @@ -307,7 +305,6 @@ func TestDelayedRateLimiter(t *testing.T) {
}

func TestPer(t *testing.T) {
t.Parallel()
runTest(t, func(r testRunner) {
rl := r.createLimiter(7, WithoutSlack, Per(time.Minute))

Expand All @@ -322,7 +319,6 @@ func TestPer(t *testing.T) {

// TestInitial verifies that the initial sequence is scheduled as expected.
func TestInitial(t *testing.T) {
t.Parallel()
tests := []struct {
msg string
opts []Option
Expand All @@ -339,27 +335,25 @@ func TestInitial(t *testing.T) {
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
runTest(t, func(r testRunner) {
perRequest := 100 * time.Millisecond
rl := r.createLimiter(10, tt.opts...)

var (
clk = r.getClock()
prev = clk.Now()

results = make(chan time.Time)
results = make(chan time.Time, 3)
have []time.Duration
startWg sync.WaitGroup
)
startWg.Add(3)

for i := 0; i < 3; i++ {
go func() {
startWg.Done()
results <- rl.Take()
}()
}
results <- rl.Take()
clk.Add(perRequest)

results <- rl.Take()
clk.Add(perRequest)

startWg.Wait()
clk.Add(time.Second)
results <- rl.Take()
clk.Add(perRequest)

for i := 0; i < 3; i++ {
ts := <-results
Expand All @@ -370,8 +364,8 @@ func TestInitial(t *testing.T) {
assert.Equal(t,
[]time.Duration{
0,
time.Millisecond * 100,
time.Millisecond * 100,
perRequest,
perRequest,
},
have,
"bad timestamps for inital takes",
Expand All @@ -382,7 +376,6 @@ func TestInitial(t *testing.T) {
}

func TestMaxSlack(t *testing.T) {
t.Parallel()
runTest(t, func(r testRunner) {
rl := r.createLimiter(1, WithSlack(1))

Expand All @@ -398,7 +391,6 @@ func TestMaxSlack(t *testing.T) {
}

func TestSlack(t *testing.T) {
t.Parallel()
// To simulate slack, we combine two limiters.
// - First, we start a single goroutine with both of them,
// during this time the slow limiter will dominate,
Expand Down

0 comments on commit fe57a6c

Please sign in to comment.