-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter_test.go
60 lines (40 loc) · 995 Bytes
/
limiter_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
package tokenbucket_test
import (
"testing"
"time"
"github.com/mauricioabreu/ratelimiter/internal/tokenbucket"
"github.com/stretchr/testify/assert"
)
func TestRemainingToken(t *testing.T) {
tb := tokenbucket.New(10, 1, 60)
ip := "127.0.0.1"
assert.Equal(t, tb.Remaining(ip), 0)
}
func TestAddToken(t *testing.T) {
tb := tokenbucket.New(10, 1, 60)
ip := "127.0.0.1"
assert.Equal(t, tb.Remaining(ip), 0)
tb.Add(ip)
tb.Add(ip)
tb.Add(ip)
assert.Equal(t, tb.Remaining(ip), 10)
}
func TestRemoveToken(t *testing.T) {
tb := tokenbucket.New(10, 1, 60)
ip := "127.0.0.1"
assert.Equal(t, tb.Remaining(ip), 0)
err := tb.Remove(ip)
assert.NoError(t, err)
assert.Equal(t, tb.Remaining(ip), 9)
}
func TestRefill(t *testing.T) {
tb := tokenbucket.New(10, 1, 60)
ip := "127.0.0.1"
assert.Equal(t, tb.Remaining(ip), 0)
tb.Remove(ip)
assert.Equal(t, tb.Remaining(ip), 9)
go tb.Refill()
time.Sleep(2 * time.Second)
tb.Stop()
assert.True(t, tb.Remaining(ip) > 9)
}