forked from hexdigest/gowrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_with_ratelimit_test.go
71 lines (50 loc) · 1.72 KB
/
interface_with_ratelimit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package templatestests
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTestInterfaceWithRateLimit_F(t *testing.T) {
impl := &testImpl{r1: "1", r2: "2"}
wrapped := NewTestInterfaceWithRateLimit(impl, 3, 10)
go func() {
for i := 0; i < 10; i++ {
r1, r2, err := wrapped.F(context.Background(), "a1")
assert.NoError(t, err)
assert.Equal(t, "1", r1)
assert.Equal(t, "2", r2)
}
}()
<-time.After(150 * time.Millisecond)
counter := atomic.LoadUint64(&impl.callCounter)
assert.EqualValues(t, 4, counter) //3 burst request + 1 requests after tick
<-time.After(200 * time.Millisecond)
counter = atomic.LoadUint64(&impl.callCounter)
assert.EqualValues(t, 6, counter) //after bust we should receiv 1 request per 100 milliseconds
}
func TestTestInterfaceWithRateLimit_F_HugeRPS(t *testing.T) {
impl := &testImpl{r1: "1", r2: "2", delay: 100 * time.Millisecond}
wrapped := NewTestInterfaceWithRateLimit(impl, 3, 1000)
for i := 0; i < 10; i++ {
go func() {
r1, r2, err := wrapped.F(context.Background(), "a1")
assert.NoError(t, err)
assert.Equal(t, "1", r1)
assert.Equal(t, "2", r2)
}()
}
<-time.After(10 * time.Millisecond)
counter := atomic.LoadUint64(&impl.callCounter)
assert.EqualValues(t, 3, counter) // the first burst
<-time.After(100 * time.Millisecond)
counter = atomic.LoadUint64(&impl.callCounter)
assert.EqualValues(t, 6, counter) // the second burst
<-time.After(100 * time.Millisecond)
counter = atomic.LoadUint64(&impl.callCounter)
assert.EqualValues(t, 9, counter) // the third burst
<-time.After(100 * time.Millisecond)
counter = atomic.LoadUint64(&impl.callCounter)
assert.EqualValues(t, 10, counter) // the 10th call
}