forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter_test.go
75 lines (64 loc) · 1.2 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package limiter
import (
"fmt"
"sync"
"testing"
"time"
)
type handler struct {
count int
sync.Mutex
}
func (h *handler) handle() error {
h.Lock()
defer h.Unlock()
h.count += 1
return nil
}
func (h *handler) counter() int {
h.Lock()
defer h.Unlock()
return h.count
}
func TestCoalescingSerializingRateLimiter(t *testing.T) {
fmt.Println("start")
tests := []struct {
Name string
Interval time.Duration
Times int
}{
{
Name: "3PO",
Interval: 3 * time.Second,
Times: 10,
},
{
Name: "five-fer",
Interval: 5 * time.Second,
Times: 20,
},
{
Name: "longjob",
Interval: 2 * time.Second,
Times: 20,
},
}
for _, tc := range tests {
h := &handler{}
rlf := NewCoalescingSerializingRateLimiter(tc.Interval, h.handle)
for i := 0; i < tc.Times; i++ {
fmt.Println("start")
rlf.RegisterChange()
fmt.Println("end")
}
select {
case <-time.After(tc.Interval + 2*time.Second):
fmt.Println("after")
counter := h.counter()
if tc.Interval > 0 && counter >= tc.Times/2 {
t.Errorf("For coalesced calls, expected number of invocations to be at least half. Expected: < %v Got: %v",
tc.Times/2, counter)
}
}
}
}