forked from rubyist/circuitbreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_test.go
74 lines (60 loc) · 1.31 KB
/
window_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
package circuit
import (
"testing"
"time"
"github.com/facebookgo/clock"
)
func TestWindowCounts(t *testing.T) {
w := newWindow(time.Millisecond*10, 2)
w.Fail()
w.Fail()
w.Success()
w.Success()
if f := w.Failures(); f != 2 {
t.Fatalf("expected window to have 2 failures, got %d", f)
}
if s := w.Successes(); s != 2 {
t.Fatalf("expected window to have 2 successes, got %d", s)
}
if r := w.ErrorRate(); r != 0.5 {
t.Fatalf("expected window to have 0.5 error rate, got %f", r)
}
w.Reset()
if f := w.Failures(); f != 0 {
t.Fatalf("expected reset window to have 0 failures, got %d", f)
}
if s := w.Successes(); s != 0 {
t.Fatalf("expected window to have 0 successes, got %d", s)
}
}
func TestWindowSlides(t *testing.T) {
c := clock.NewMock()
w := newWindow(time.Millisecond*10, 2)
w.clock = c
w.lastAccess = c.Now()
w.Fail()
c.Add(time.Millisecond * 6)
w.Fail()
counts := 0
w.buckets.Do(func(x interface{}) {
b := x.(*bucket)
if b.failure > 0 {
counts++
}
})
if counts != 2 {
t.Fatalf("expected 2 buckets to have failures, got %d", counts)
}
c.Add(time.Millisecond * 15)
w.Success()
counts = 0
w.buckets.Do(func(x interface{}) {
b := x.(*bucket)
if b.failure > 0 {
counts++
}
})
if counts != 0 {
t.Fatalf("expected 0 buckets to have failures, got %d", counts)
}
}