@@ -115,6 +115,157 @@ func (s *Suite) TestAddAndGet() {
115
115
log .Println (events )
116
116
}
117
117
118
+ func (s * Suite ) TestRateLimit () {
119
+ // Events are close to each other so should be rate limited.
120
+ times := []time.Time {
121
+ Now (),
122
+ Now ().Add (time .Second ),
123
+ Now ().Add (2 * time .Second ),
124
+ Now ().Add (3 * time .Second ),
125
+ Now ().Add (4 * time .Second ),
126
+ Now ().Add (6 * time .Second ),
127
+ Now ().Add (7 * time .Second ),
128
+ }
129
+
130
+ description := EventDescription {Details : map [string ]interface {}{"file" : "abc" }, Type : "rate_limit_check" }
131
+
132
+ for _ , t := range times {
133
+ event := Event {
134
+ Timestamp : t ,
135
+ Description : description ,
136
+ }
137
+ s .NoError (s .store .Add (& event ))
138
+ }
139
+
140
+ // Test GetKeys
141
+ keys , err := s .store .GetKeys ()
142
+ s .NoError (err , "error returned when getting all keys" )
143
+ // Check 5 events + 1 rate limit event
144
+ s .Equal (5 + 1 , len (keys ), "error with number of keys returned" )
145
+
146
+ // Check that there was a rate limit event
147
+ rateLimitEvent := false
148
+ for _ , key := range keys {
149
+ eventBytes , err := s .store .Get (key )
150
+ s .NoError (err )
151
+ event := & Event {}
152
+ s .NoError (json .Unmarshal (eventBytes , event ))
153
+ if event .Description .Type == "rate_limit" {
154
+ rateLimitEvent = true
155
+ }
156
+ }
157
+ if ! rateLimitEvent {
158
+ s .Fail ("Rate limit event not found" )
159
+ }
160
+ }
161
+
162
+ func (s * Suite ) TestNoRateLimit () {
163
+ // Events are far apart from each other so shouldn't be rate limited.
164
+ times := []time.Time {
165
+ Now (),
166
+ Now ().Add (time .Hour ),
167
+ Now ().Add (2 * time .Hour ),
168
+ Now ().Add (3 * time .Hour ),
169
+ Now ().Add (4 * time .Hour ),
170
+ Now ().Add (6 * time .Hour ),
171
+ Now ().Add (7 * time .Hour ),
172
+ }
173
+
174
+ description := EventDescription {Details : map [string ]interface {}{"file" : "abc" }, Type : "rate_limit_check" }
175
+
176
+ for _ , t := range times {
177
+ event := Event {
178
+ Timestamp : t ,
179
+ Description : description ,
180
+ }
181
+ s .NoError (s .store .Add (& event ))
182
+ }
183
+
184
+ // Test GetKeys
185
+ keys , err := s .store .GetKeys ()
186
+ s .NoError (err , "error returned when getting all keys" )
187
+ // Check 5 events + 1 rate limit event
188
+ s .Equal (7 , len (keys ), "error with number of keys returned" )
189
+
190
+ // Check that there was a rate limit event
191
+ rateLimitEvent := false
192
+ for _ , key := range keys {
193
+ eventBytes , err := s .store .Get (key )
194
+ s .NoError (err )
195
+ event := & Event {}
196
+ s .NoError (json .Unmarshal (eventBytes , event ))
197
+ if event .Description .Type == "rate_limit" {
198
+ rateLimitEvent = true
199
+ }
200
+ }
201
+ if rateLimitEvent {
202
+ s .Fail ("Rate limit event found" )
203
+ }
204
+ }
205
+
206
+ func (s * Suite ) TestRateLimitThenNoRateLimit () {
207
+ // Event will be rate limited then not rate limited.
208
+
209
+ // Intervals between events.
210
+ durations := []time.Duration {
211
+ time .Minute ,
212
+ time .Minute ,
213
+ time .Minute ,
214
+ time .Minute ,
215
+ time .Minute ,
216
+ time .Minute ,
217
+ time .Minute ,
218
+ time .Minute ,
219
+ time .Minute ,
220
+ time .Minute ,
221
+ time .Minute ,
222
+ 1 * time .Hour ,
223
+ time .Minute ,
224
+ time .Minute ,
225
+ time .Minute ,
226
+ time .Minute ,
227
+ }
228
+
229
+ // Make event times
230
+ eventTime := time .Now ()
231
+ times := []time.Time {eventTime }
232
+ for _ , d := range durations {
233
+ eventTime = eventTime .Add (d )
234
+ times = append (times , eventTime )
235
+ }
236
+
237
+ // Make events
238
+ description := EventDescription {Details : map [string ]interface {}{"file" : "abc" }, Type : "rate_limit_check" }
239
+ for _ , t := range times {
240
+ event := Event {
241
+ Timestamp : t ,
242
+ Description : description ,
243
+ }
244
+ s .NoError (s .store .Add (& event ))
245
+ }
246
+
247
+ // Test GetKeys
248
+ keys , err := s .store .GetKeys ()
249
+ s .NoError (err , "error returned when getting all keys" )
250
+ // Check 10 events + 1 rate limit event
251
+ s .Equal (11 , len (keys ), "error with number of keys returned" )
252
+
253
+ // Check that there was a rate limit event
254
+ rateLimitEvent := false
255
+ for _ , key := range keys {
256
+ eventBytes , err := s .store .Get (key )
257
+ s .NoError (err )
258
+ event := & Event {}
259
+ s .NoError (json .Unmarshal (eventBytes , event ))
260
+ if event .Description .Type == "rate_limit" {
261
+ rateLimitEvent = true
262
+ }
263
+ }
264
+ if ! rateLimitEvent {
265
+ s .Fail ("Rate limit event not found" )
266
+ }
267
+ }
268
+
118
269
func TestRun (t * testing.T ) {
119
270
suite .Run (t , new (Suite ))
120
271
}
0 commit comments