@@ -16,14 +16,19 @@ func TestLocalFallback(t *testing.T) {
16
16
redis , err := miniredis .Run ()
17
17
redisPort , _ := strconv .Atoi (redis .Port ())
18
18
19
+ var onErrorCalled bool
20
+ var onFallbackCalled bool
21
+
19
22
limitCounter , err := httprateredis .NewRedisLimitCounter (& httprateredis.Config {
20
- Host : redis .Host (),
21
- Port : uint16 (redisPort ),
22
- MaxIdle : 0 ,
23
- MaxActive : 1 ,
24
- ClientName : "httprateredis_test" ,
25
- PrefixKey : fmt .Sprintf ("httprate:test:%v" , rand .Int31n (100000 )), // Unique Redis key for each test
26
- FallbackTimeout : 200 * time .Millisecond ,
23
+ Host : redis .Host (),
24
+ Port : uint16 (redisPort ),
25
+ MaxIdle : 0 ,
26
+ MaxActive : 1 ,
27
+ ClientName : "httprateredis_test" ,
28
+ PrefixKey : fmt .Sprintf ("httprate:test:%v" , rand .Int31n (100000 )), // Unique Redis key for each test
29
+ FallbackTimeout : 200 * time .Millisecond ,
30
+ OnError : func (err error ) { onErrorCalled = true },
31
+ OnFallbackChange : func (fallbackActivated bool ) { onFallbackCalled = true },
27
32
})
28
33
if err != nil {
29
34
t .Fatalf ("redis not available: %v" , err )
@@ -37,6 +42,12 @@ func TestLocalFallback(t *testing.T) {
37
42
if limitCounter .IsFallbackActivated () {
38
43
t .Error ("fallback should not be activated at the beginning" )
39
44
}
45
+ if onErrorCalled {
46
+ t .Error ("onError() should not be called at the beginning" )
47
+ }
48
+ if onFallbackCalled {
49
+ t .Error ("onFallback() should not be called before we simulate redis failure" )
50
+ }
40
51
41
52
err = limitCounter .IncrementBy ("key:fallback" , currentWindow , 1 )
42
53
if err != nil {
@@ -51,6 +62,12 @@ func TestLocalFallback(t *testing.T) {
51
62
if limitCounter .IsFallbackActivated () {
52
63
t .Error ("fallback should not be activated before we simulate redis failure" )
53
64
}
65
+ if onErrorCalled {
66
+ t .Error ("onError() should not be called before we simulate redis failure" )
67
+ }
68
+ if onFallbackCalled {
69
+ t .Error ("onFallback() should not be called before we simulate redis failure" )
70
+ }
54
71
55
72
redis .Close ()
56
73
@@ -67,4 +84,11 @@ func TestLocalFallback(t *testing.T) {
67
84
if ! limitCounter .IsFallbackActivated () {
68
85
t .Error ("fallback should be activated after we simulate redis failure" )
69
86
}
87
+ if ! onErrorCalled {
88
+ t .Error ("onError() should be called after we simulate redis failure" )
89
+ }
90
+ if ! onFallbackCalled {
91
+ t .Error ("onFallback() should be called after we simulate redis failure" )
92
+ }
93
+
70
94
}
0 commit comments