summaryrefslogtreecommitdiffstats
path: root/internal/bot/ratelimit_test.go
blob: 7f097870d5f0559fbede10bde29c98bca4919bc2 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package bot

import (
	"sync"
	"testing"
	"time"
)

type fakeClock struct {
	mu  sync.Mutex
	now time.Time
}

func (c *fakeClock) Now() time.Time {
	c.mu.Lock()
	defer c.mu.Unlock()
	return c.now
}

func (c *fakeClock) Advance(d time.Duration) {
	c.mu.Lock()
	defer c.mu.Unlock()
	c.now = c.now.Add(d)
}

func TestLimiterAllowsTheBurstThenStops(t *testing.T) {
	clock := &fakeClock{now: time.Unix(1_700_000_000, 0)}
	limiter := NewLimiterWith(3, time.Second, 30*time.Second, clock.Now)

	for i := 0; i < 3; i++ {
		if decision := limiter.Allow(1); !decision.Allowed {
			t.Fatalf("request %d was refused inside the burst", i+1)
		}
	}
	decision := limiter.Allow(1)
	if decision.Allowed {
		t.Fatal("the fourth request should be refused")
	}
	if !decision.Warn {
		t.Fatal("the first refusal should warn the user")
	}
	if decision.RetryAfter <= 0 {
		t.Fatalf("retry after = %v, want a positive hint", decision.RetryAfter)
	}
}

func TestLimiterWarnsOncePerWindow(t *testing.T) {
	clock := &fakeClock{now: time.Unix(1_700_000_000, 0)}
	limiter := NewLimiterWith(1, time.Minute, 30*time.Second, clock.Now)

	limiter.Allow(1)
	if !limiter.Allow(1).Warn {
		t.Fatal("the first refusal should warn")
	}
	if limiter.Allow(1).Warn {
		t.Fatal("a second refusal inside the window should stay silent")
	}

	clock.Advance(31 * time.Second)
	if !limiter.Allow(1).Warn {
		t.Fatal("a refusal after the window should warn again")
	}
}

func TestLimiterRefills(t *testing.T) {
	clock := &fakeClock{now: time.Unix(1_700_000_000, 0)}
	limiter := NewLimiterWith(2, time.Second, time.Minute, clock.Now)

	limiter.Allow(1)
	limiter.Allow(1)
	if limiter.Allow(1).Allowed {
		t.Fatal("the bucket should be empty")
	}

	clock.Advance(time.Second)
	if !limiter.Allow(1).Allowed {
		t.Fatal("one second should have bought one token")
	}
	if limiter.Allow(1).Allowed {
		t.Fatal("only one token should have been refilled")
	}
}

func TestLimiterNeverExceedsTheBurst(t *testing.T) {
	clock := &fakeClock{now: time.Unix(1_700_000_000, 0)}
	limiter := NewLimiterWith(2, time.Second, time.Minute, clock.Now)

	limiter.Allow(1)
	clock.Advance(time.Hour)

	if !limiter.Allow(1).Allowed || !limiter.Allow(1).Allowed {
		t.Fatal("the bucket should be full again")
	}
	if limiter.Allow(1).Allowed {
		t.Fatal("an idle hour must not bank more than the burst")
	}
}

func TestLimiterKeysAreIndependent(t *testing.T) {
	clock := &fakeClock{now: time.Unix(1_700_000_000, 0)}
	limiter := NewLimiterWith(1, time.Minute, time.Minute, clock.Now)

	if !limiter.Allow(1).Allowed {
		t.Fatal("first user refused")
	}
	if limiter.Allow(1).Allowed {
		t.Fatal("first user should now be limited")
	}
	if !limiter.Allow(2).Allowed {
		t.Fatal("one user's flood must not silence another")
	}
}

func TestLimiterIsConcurrencySafe(t *testing.T) {
	limiter := NewLimiterWith(100, time.Millisecond, time.Second, nil)

	var wg sync.WaitGroup
	for i := 0; i < 50; i++ {
		wg.Add(1)
		go func(key int64) {
			defer wg.Done()
			for j := 0; j < 20; j++ {
				limiter.Allow(key % 5)
			}
		}(int64(i))
	}
	wg.Wait()
}