-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle_test.go
52 lines (42 loc) · 1.05 KB
/
throttle_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
package patterns
import (
"context"
"strings"
"testing"
"time"
)
func TestThrottle(t *testing.T) {
c, stop := Throttle(Effector(getCounter()), 5, time.Second)
var count string
var err error
for i := 0; i < 5; i++ {
_, err = c(context.Background())
}
if err != nil {
t.Errorf("got %v, want %v", err, nil)
}
_, err = c(context.Background())
if !strings.Contains(err.Error(), "throttled") {
t.Errorf("want %v to contain %q", err, "throttled")
}
time.Sleep(time.Second*2 + time.Millisecond*50)
for i := 0; i < 2; i++ {
count, err = c(context.Background())
}
if err != nil {
t.Errorf("got %v, want %v", err, nil)
}
if count != "7" {
t.Errorf("count == %v, want %v", count, "7")
}
_, err = c(context.Background())
if err == nil || !strings.Contains(err.Error(), "throttled") {
t.Errorf("want %v to contain %q", err, "throttled")
}
stop()
time.Sleep(time.Second*2 + time.Millisecond*50)
_, err = c(context.Background())
if !strings.Contains(err.Error(), "throttled") {
t.Errorf("want %v to contain %q", err, "throttled")
}
}