|
| 1 | +// Copyright (c) 2018 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package utils |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestRateLimiter(t *testing.T) { |
| 25 | + limiter := NewRateLimiter(2.0, 2.0) |
| 26 | + // stop time |
| 27 | + ts := time.Now() |
| 28 | + limiter.(*rateLimiter).lastTick = ts |
| 29 | + limiter.(*rateLimiter).timeNow = func() time.Time { |
| 30 | + return ts |
| 31 | + } |
| 32 | + ok, waitTime := limiter.CheckCredit(1.0) |
| 33 | + assert.True(t, ok) |
| 34 | + assert.Equal(t, time.Duration(0), waitTime) |
| 35 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 36 | + assert.True(t, ok) |
| 37 | + assert.Equal(t, time.Duration(0), waitTime) |
| 38 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 39 | + assert.False(t, ok) |
| 40 | + assert.Equal(t, time.Second/2, waitTime) |
| 41 | + // move time 250ms forward, not enough credits to pay for 1.0 item |
| 42 | + limiter.(*rateLimiter).timeNow = func() time.Time { |
| 43 | + return ts.Add(time.Second / 4) |
| 44 | + } |
| 45 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 46 | + assert.False(t, ok) |
| 47 | + assert.Equal(t, time.Second/4, waitTime) |
| 48 | + // move time 500ms forward, now enough credits to pay for 1.0 item |
| 49 | + limiter.(*rateLimiter).timeNow = func() time.Time { |
| 50 | + return ts.Add(time.Second/4 + time.Second/2) |
| 51 | + } |
| 52 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 53 | + assert.True(t, ok) |
| 54 | + assert.Equal(t, time.Duration(0), waitTime) |
| 55 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 56 | + assert.False(t, ok) |
| 57 | + assert.Equal(t, time.Second/4, waitTime) |
| 58 | + // move time 5s forward, enough to accumulate credits for 10 messages, but it should still be capped at 2 |
| 59 | + limiter.(*rateLimiter).lastTick = ts |
| 60 | + limiter.(*rateLimiter).timeNow = func() time.Time { |
| 61 | + return ts.Add(5 * time.Second) |
| 62 | + } |
| 63 | + for i := 0; i < 2; i++ { |
| 64 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 65 | + assert.True(t, ok) |
| 66 | + assert.Equal(t, time.Duration(0), waitTime) |
| 67 | + } |
| 68 | + for i := 0; i < 3; i++ { |
| 69 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 70 | + assert.False(t, ok) |
| 71 | + assert.Equal(t, time.Second/2, waitTime) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestMaxBalance(t *testing.T) { |
| 76 | + limiter := NewRateLimiter(0.1, 1.0) |
| 77 | + // stop time |
| 78 | + ts := time.Now() |
| 79 | + limiter.(*rateLimiter).lastTick = ts |
| 80 | + limiter.(*rateLimiter).timeNow = func() time.Time { |
| 81 | + return ts |
| 82 | + } |
| 83 | + // on initialization, should have enough credits for 1 message |
| 84 | + ok, waitTime := limiter.CheckCredit(1.0) |
| 85 | + assert.True(t, ok) |
| 86 | + assert.Equal(t, time.Duration(0), waitTime) |
| 87 | + |
| 88 | + // move time 20s forward, enough to accumulate credits for 2 messages, but it should still be capped at 1 |
| 89 | + limiter.(*rateLimiter).timeNow = func() time.Time { |
| 90 | + return ts.Add(time.Second * 20) |
| 91 | + } |
| 92 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 93 | + assert.True(t, ok) |
| 94 | + assert.Equal(t, time.Duration(0), waitTime) |
| 95 | + ok, waitTime = limiter.CheckCredit(1.0) |
| 96 | + assert.False(t, ok) |
| 97 | + assert.Equal(t, 10*time.Second, waitTime) |
| 98 | +} |
0 commit comments