@@ -2,7 +2,9 @@ package breaker
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"strconv"
7
+ "strings"
6
8
"testing"
7
9
8
10
"github.com/stretchr/testify/assert"
@@ -33,6 +35,84 @@ func TestLogReason(t *testing.T) {
33
35
assert .Equal (t , numHistoryReasons , errs .count )
34
36
}
35
37
38
+ func TestErrorWindow (t * testing.T ) {
39
+ tests := []struct {
40
+ name string
41
+ reasons []string
42
+ }{
43
+ {
44
+ name : "no error" ,
45
+ },
46
+ {
47
+ name : "one error" ,
48
+ reasons : []string {"foo" },
49
+ },
50
+ {
51
+ name : "two errors" ,
52
+ reasons : []string {"foo" , "bar" },
53
+ },
54
+ {
55
+ name : "five errors" ,
56
+ reasons : []string {"first" , "second" , "third" , "fourth" , "fifth" },
57
+ },
58
+ {
59
+ name : "six errors" ,
60
+ reasons : []string {"first" , "second" , "third" , "fourth" , "fifth" , "sixth" },
61
+ },
62
+ }
63
+
64
+ for _ , test := range tests {
65
+ t .Run (test .name , func (t * testing.T ) {
66
+ var ew errorWindow
67
+ for _ , reason := range test .reasons {
68
+ ew .add (reason )
69
+ }
70
+ var reasons []string
71
+ if len (test .reasons ) > numHistoryReasons {
72
+ reasons = test .reasons [len (test .reasons )- numHistoryReasons :]
73
+ } else {
74
+ reasons = test .reasons
75
+ }
76
+ for _ , reason := range reasons {
77
+ assert .True (t , strings .Contains (ew .String (), reason ), fmt .Sprintf ("actual: %s" , ew .String ()))
78
+ }
79
+ })
80
+ }
81
+ }
82
+
83
+ func TestPromiseWithReason (t * testing.T ) {
84
+ tests := []struct {
85
+ name string
86
+ reason string
87
+ expect string
88
+ }{
89
+ {
90
+ name : "success" ,
91
+ },
92
+ {
93
+ name : "success" ,
94
+ reason : "fail" ,
95
+ expect : "fail" ,
96
+ },
97
+ }
98
+
99
+ for _ , test := range tests {
100
+ t .Run (test .name , func (t * testing.T ) {
101
+ promise := promiseWithReason {
102
+ promise : new (mockedPromise ),
103
+ errWin : new (errorWindow ),
104
+ }
105
+ if len (test .reason ) == 0 {
106
+ promise .Accept ()
107
+ } else {
108
+ promise .Reject (test .reason )
109
+ }
110
+
111
+ assert .True (t , strings .Contains (promise .errWin .String (), test .expect ))
112
+ })
113
+ }
114
+ }
115
+
36
116
func BenchmarkGoogleBreaker (b * testing.B ) {
37
117
br := NewBreaker ()
38
118
for i := 0 ; i < b .N ; i ++ {
@@ -41,3 +121,12 @@ func BenchmarkGoogleBreaker(b *testing.B) {
41
121
})
42
122
}
43
123
}
124
+
125
+ type mockedPromise struct {
126
+ }
127
+
128
+ func (m * mockedPromise ) Accept () {
129
+ }
130
+
131
+ func (m * mockedPromise ) Reject () {
132
+ }
0 commit comments