-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
rate_test.go
69 lines (59 loc) · 1.55 KB
/
rate_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
// Copyright © by Jeff Foley 2022-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"testing"
"time"
)
func TestUpdateRateLimiters(t *testing.T) {
rt := NewRateTracker()
defer rt.Stop()
domain := "owasp.org"
// add the name servers to the rate tracker
rt.Take(domain)
tracker := rt.getDomainRateTracker(domain)
tracker.Lock()
qps := tracker.qps
tracker.Unlock()
num := qps / 3
// set a large number of timeouts
for i := 0; i < num; i++ {
rt.Success(domain)
}
max := tracker.qps - num
for i := 0; i < max; i++ {
rt.Timeout(domain)
}
time.Sleep(rateUpdateInterval + (rateUpdateInterval / 2))
tracker.Lock()
qps2 := tracker.qps
tracker.Unlock()
// the QPS should now be lower
if qps2 >= qps {
t.Errorf("Unexpected QPS, expected QPS lower than %d, got %d", qps, qps2)
}
tracker.Lock()
succ := tracker.success
tout := tracker.timeout
tracker.Unlock()
// check that the counters have been cleared
if succ != 0 || tout != 0 {
t.Errorf("Unexpected counter values, Success Counter %d, Timeout Counter %d", succ, tout)
}
tracker.Lock()
qps = tracker.qps
tracker.Unlock()
// set a large number of successes
for i := 0; i < qps; i++ {
rt.Success(domain)
}
time.Sleep(rateUpdateInterval + (rateUpdateInterval / 2))
tracker.Lock()
qps2 = tracker.qps
tracker.Unlock()
// the QPS should now be higher
if qps2 <= qps {
t.Errorf("Unexpected QPS, expected QPS higher than %d, got %d", qps, qps2)
}
}