-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptors_test.go
192 lines (156 loc) · 4.6 KB
/
interceptors_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package confunc_test
import (
"errors"
"github.com/alperkose/confunc"
"math/rand"
"strconv"
"testing"
"time"
)
func Test_CacheOnceInterceptor(t *testing.T) {
interceptorUnderTest := confunc.CacheOnce()
cfn := interceptorUnderTest(someRandomStuff)
firstVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
for i := 0; i < 100; i++ {
cfn = interceptorUnderTest(someRandomStuff)
aVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if firstVal != aVal {
t.Errorf("expected '%v' to be '%v'", firstVal, aVal)
}
}
}
func Test_CacheOnce_WhenSourceHasTemporaryError(t *testing.T) {
interceptorUnderTest := confunc.CacheOnce()
testCacheInterceptor_WhenSourceHasTemporaryError(t, interceptorUnderTest)
}
func Test_CacheForInterceptor(t *testing.T) {
var cacheOffset = 250 * time.Millisecond
interceptorUnderTest := confunc.CacheFor(cacheOffset)
cfn := interceptorUnderTest(someRandomStuff)
firstVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
for i := 0; i < 100; i++ {
cfn = interceptorUnderTest(someRandomStuff)
aVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if firstVal != aVal {
t.Errorf("expected '%v' to be '%v'", firstVal, aVal)
}
}
time.Sleep(cacheOffset)
cfn = interceptorUnderTest(someRandomStuff)
lastVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if firstVal == lastVal {
t.Errorf("expected '%v' to be different than '%v'", lastVal, firstVal)
}
for i := 0; i < 100; i++ {
cfn = interceptorUnderTest(someRandomStuff)
aVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if lastVal != aVal {
t.Errorf("expected '%v' to be '%v'", firstVal, aVal)
}
}
}
func Test_CacheFor_WhenSourceHasTemporaryError(t *testing.T) {
interceptorUnderTest := confunc.CacheFor(250 * time.Millisecond)
testCacheInterceptor_WhenSourceHasTemporaryError(t, interceptorUnderTest)
}
func testCacheInterceptor_WhenSourceHasTemporaryError(t *testing.T, interceptorUnderTest confunc.Interceptor) {
confuncToCover := confuncWithErrorAndSuccessCycle()
cfn := interceptorUnderTest(confuncToCover)
_, err := cfn()
if err == nil {
t.Errorf("an error should have occurred")
}
cfn = interceptorUnderTest(confuncToCover)
secondVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if len(secondVal) == 0 {
t.Errorf("should have returned a value")
}
cfn = interceptorUnderTest(confuncToCover)
thirdVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if thirdVal != secondVal {
t.Errorf("should have returned value from cache")
}
}
func Test_DefaultInterceptor_WhenSourceHasNoValue_ShouldReturnDefaultValue(t *testing.T) {
defaultValue := "42"
interceptorUnderTest := confunc.Default(defaultValue)
cfn := interceptorUnderTest(func() (string, error) { return "", errors.New("Configuration Not found") })
actualVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if actualVal != defaultValue {
t.Errorf("expected '%v' to be '%v'", actualVal, defaultValue)
}
}
func Test_DefaultInterceptor_WhenSourceHasValue_ShouldReturnSourceValue(t *testing.T) {
sourceValue := "7"
defaultValue := "42"
interceptorUnderTest := confunc.Default(defaultValue)
cfn := interceptorUnderTest(func() (string, error) { return sourceValue, nil })
actualVal, err := cfn()
if err != nil {
t.Errorf("error should not have occurred : %v ", err)
}
if actualVal != sourceValue {
t.Errorf("expected '%v' to be '%v'", actualVal, sourceValue)
}
}
var rndSource = rand.New(rand.NewSource(time.Now().Unix()))
func someRandomStuff() (string, error) {
return strconv.FormatFloat(rndSource.Float64(), 'f', 10, 64), nil
}
func confuncWithErrorAndSuccessCycle() confunc.Confunc {
var hammerTime = true
return func() (string, error) {
v := ""
var err error
err = nil
if hammerTime {
err = errors.New("Can't touch this!!!")
} else {
v, err = someRandomStuff()
}
hammerTime = !hammerTime
return v, err
}
}
type testSource struct{}
func (ts *testSource) Value(k string) (string, error) {
v, err := someRandomStuff()
return v, err
}
func BenchmarkAll(b *testing.B) {
confuncUnderTest := confunc.From(&testSource{}).Float64("somekey", confunc.CacheOnce())
firstVal := confuncUnderTest()
for i := 0; i < b.N; i++ {
aVal := confuncUnderTest()
if firstVal != aVal {
b.Errorf("expected '%v' to be '%v'", firstVal, aVal)
}
}
}