-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathratelimit_test.go
194 lines (155 loc) · 4.29 KB
/
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package integration_test
import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/rs/zerolog"
. "github.com/switchupcb/disgo"
"golang.org/x/sync/errgroup"
)
// TestRequestGlobalRateLimit tests the global rate limit mechanism (with the Default Bucket mechanism disabled)
// for HTTP requests.
func TestRequestGlobalRateLimit(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
// setup the bot.
bot := &Client{
Authentication: BotToken(os.Getenv("TOKEN")),
Config: DefaultConfig(),
}
bot.Config.Request.Timeout = time.Second * 5
bot.Config.Request.Retries = 0
bot.Config.Request.RateLimiter.SetDefaultBucket(nil)
// prepare the request.
request := new(GetCurrentBotApplicationInformation)
requests := 101
// prepare the test tracking variables.
eg, ctx := errgroup.WithContext(context.Background())
// send the requests concurrently.
for i := 1; i <= requests; i++ {
select {
case <-ctx.Done():
t.Fatalf("%v", eg.Wait())
default:
}
id := i
eg.Go(func() error {
t.Log("Spawned request goroutine", id)
app, err := request.Send(bot)
if err != nil {
return err
}
t.Log("Request", id, ":", app)
return nil
})
}
// wait until all requests are sent and responses are received.
if err := eg.Wait(); err != nil {
t.Fatalf("%v", err)
}
// confirm the next test starts with a full bucket.
time.After(time.Second * 2)
}
// TestRequestRouteRateLimit tests the per-route rate limit mechanism (with the Default Bucket mechanism enabled)
// for HTTP requests.
func TestRequestRouteRateLimit(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
// setup the bot.
bot := &Client{
Authentication: BotToken(os.Getenv("TOKEN")),
Config: DefaultConfig(),
}
bot.Config.Request.Timeout = time.Second * 5
bot.Config.Request.Retries = 0
bot.Config.Request.RateLimiter.SetDefaultBucket(
&Bucket{Limit: 1}, //nolint:exhaustruct
)
// prepare the request.
request := GetUser{UserID: os.Getenv("APPID")}
requests := 31
// prepare the test tracking variables.
eg, ctx := errgroup.WithContext(context.Background())
// send the requests concurrently.
for i := 1; i <= requests; i++ {
select {
case <-ctx.Done():
t.Fatalf("%v", eg.Wait())
default:
}
id := i
eg.Go(func() error {
t.Log("Spawned request goroutine", id)
user, err := request.Send(bot)
if err != nil {
return err
}
t.Log("Request", id, ":", user)
return nil
})
}
// wait until all requests are sent and responses are received.
if err := eg.Wait(); err != nil {
t.Fatalf("%v", err)
}
// confirm the next test starts with a full bucket.
time.After(time.Second * 2)
}
// TestGatewayIdentifyRateLimit tests the Identify rate limit mechanism for the Discord Gateway.
func TestGatewayIdentifyRateLimit(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
bot := &Client{
Authentication: BotToken(os.Getenv("TOKEN")),
Config: DefaultConfig(),
Handlers: new(Handlers),
Sessions: NewSessionManager(),
}
// a counter is used to count the amount of Ready events.
readyCount := 0
muReady := new(sync.Mutex)
// a Ready event is sent upon a successful connection.
if err := bot.Handle(FlagGatewayEventNameReady, func(*Ready) {
muReady.Lock()
readyCount++
muReady.Unlock()
}); err != nil {
t.Fatalf("%v", err)
}
s1 := NewSession()
s2 := NewSession()
// call Connect at the same time.
eg := new(errgroup.Group)
eg.Go(func() error {
// connect to the Discord Gateway (WebSocket Session).
if err := s1.Connect(bot); err != nil {
return fmt.Errorf("s1: %w", err)
}
return nil
})
eg.Go(func() error {
// connect to the Discord Gateway (WebSocket Session).
if err := s2.Connect(bot); err != nil {
return fmt.Errorf("s2: %w", err)
}
return nil
})
if err := eg.Wait(); err != nil {
t.Fatalf("%v", err)
}
// disconnect from the Discord Gateway (WebSocket Connection).
if err := s1.Disconnect(); err != nil {
t.Fatalf("s1: %v", err)
}
// disconnect from the Discord Gateway (WebSocket Connection).
if err := s2.Disconnect(); err != nil {
t.Fatalf("s2: %v", err)
}
muReady.Lock()
defer muReady.Unlock()
if readyCount != 2 {
t.Fatalf("expect to receive 2 Ready events but got %d", readyCount)
}
// allow Discord to close each session.
<-time.After(time.Second * 5)
}