| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Copyright (c) 2021 The Jaeger Authors. |
| 5 |
// Copyright (c) 2017 Uber Technologies, Inc. |
| 6 |
// |
| 7 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// You may obtain a copy of the License at |
| 10 |
// |
| 11 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
// |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
|
| 19 |
package ratelimiter |
| 20 |
|
| 21 |
import ( |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/stretchr/testify/assert" |
| 26 |
) |
| 27 |
|
| 28 |
func TestRateLimiter(t *testing.T) { |
| 29 |
rl := NewRateLimiter(2.0, 2.0) |
| 30 |
// stop time |
| 31 |
ts := time.Now() |
| 32 |
rl.lastTick = ts |
| 33 |
rl.timeNow = func() time.Time { |
| 34 |
return ts |
| 35 |
} |
| 36 |
assert.True(t, rl.CheckCredit(1.0)) |
| 37 |
assert.True(t, rl.CheckCredit(1.0)) |
| 38 |
assert.False(t, rl.CheckCredit(1.0)) |
| 39 |
// move time 250ms forward, not enough credits to pay for 1.0 item |
| 40 |
rl.timeNow = func() time.Time { |
| 41 |
return ts.Add(time.Second / 4) |
| 42 |
} |
| 43 |
assert.False(t, rl.CheckCredit(1.0)) |
| 44 |
// move time 500ms forward, now enough credits to pay for 1.0 item |
| 45 |
rl.timeNow = func() time.Time { |
| 46 |
return ts.Add(time.Second/4 + time.Second/2) |
| 47 |
} |
| 48 |
assert.True(t, rl.CheckCredit(1.0)) |
| 49 |
assert.False(t, rl.CheckCredit(1.0)) |
| 50 |
// move time 5s forward, enough to accumulate credits for 10 messages, but it should still be capped at 2 |
| 51 |
rl.lastTick = ts |
| 52 |
rl.timeNow = func() time.Time { |
| 53 |
return ts.Add(5 * time.Second) |
| 54 |
} |
| 55 |
assert.True(t, rl.CheckCredit(1.0)) |
| 56 |
assert.True(t, rl.CheckCredit(1.0)) |
| 57 |
assert.False(t, rl.CheckCredit(1.0)) |
| 58 |
assert.False(t, rl.CheckCredit(1.0)) |
| 59 |
assert.False(t, rl.CheckCredit(1.0)) |
| 60 |
} |
| 61 |
|
| 62 |
func TestRateLimiterMaxBalance(t *testing.T) { |
| 63 |
rl := NewRateLimiter(0.1, 1.0) |
| 64 |
// stop time |
| 65 |
ts := time.Now() |
| 66 |
rl.lastTick = ts |
| 67 |
rl.timeNow = func() time.Time { |
| 68 |
return ts |
| 69 |
} |
| 70 |
assert.True(t, rl.CheckCredit(1.0), "on initialization, should have enough credits for 1 message") |
| 71 |
|
| 72 |
// move time 20s forward, enough to accumulate credits for 2 messages, but it should still be capped at 1 |
| 73 |
rl.timeNow = func() time.Time { |
| 74 |
return ts.Add(time.Second * 20) |
| 75 |
} |
| 76 |
assert.True(t, rl.CheckCredit(1.0)) |
| 77 |
assert.False(t, rl.CheckCredit(1.0)) |
| 78 |
} |
| 79 |
|
| 80 |
func TestRateLimiterReconfigure(t *testing.T) { |
| 81 |
rl := NewRateLimiter(1, 1.0) |
| 82 |
assertBalance := func(expected float64) { |
| 83 |
const delta = 0.0000001 // just some precision for comparing floats |
| 84 |
assert.InDelta(t, expected, rl.balance, delta) |
| 85 |
} |
| 86 |
// stop time |
| 87 |
ts := time.Now() |
| 88 |
rl.lastTick = ts |
| 89 |
rl.timeNow = func() time.Time { |
| 90 |
return ts |
| 91 |
} |
| 92 |
assert.True(t, rl.CheckCredit(1.0), "first message must succeed") |
| 93 |
assert.False(t, rl.CheckCredit(1.0), "second message must be rejected") |
| 94 |
assertBalance(0.0) |
| 95 |
|
| 96 |
// move half-second forward |
| 97 |
rl.timeNow = func() time.Time { |
| 98 |
return ts.Add(time.Second / 2) |
| 99 |
} |
| 100 |
rl.updateBalance() |
| 101 |
assertBalance(0.5) // 50% of max |
| 102 |
|
| 103 |
rl.Update(2, 4) |
| 104 |
assertBalance(2) // 50% of max |
| 105 |
assert.EqualValues(t, 2, rl.creditsPerSecond) |
| 106 |
assert.EqualValues(t, 4, rl.maxBalance) |
| 107 |
} |