-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_test.go
107 lines (83 loc) · 3.24 KB
/
redis_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
package circuitbreaker_test
import (
"context"
"strconv"
"testing"
"time"
"github.com/go-redis/redismock/v8"
"github.com/mrsoftware/circuitbreaker"
"github.com/stretchr/testify/assert"
)
const (
tempkey = "circuitBreaker:test"
failuresField = "failures"
successField = "success"
stateField = "state"
)
var (
serviceName = "test"
failureRateThreshold int64 = 2
successRateThreshold int64 = 2
options = []circuitbreaker.StorageOption{
circuitbreaker.WithServiceName(serviceName),
circuitbreaker.WithFailureRateThreshold(failureRateThreshold),
circuitbreaker.WithSuccessRateThreshold(successRateThreshold),
circuitbreaker.WithOpenWindow(circuitbreaker.DefaultOpenWindow),
circuitbreaker.WithHalfOpenWindow(circuitbreaker.DefaultHalfOpenWindow),
}
)
func TestRedisStorage_GetStatus(t *testing.T) {
redisClient, mock := redismock.NewClientMock()
rs := circuitbreaker.NewRedisStorage(redisClient, options...)
t.Run("key expired or not exits == close", func(t *testing.T) {
mock.ExpectPTTL(tempkey).SetVal(-1)
state, err := rs.GetState(context.Background())
assert.Nil(t, err)
assert.Equal(t, circuitbreaker.StateClose, state)
})
t.Run("we are in halfOpen window == halfOpen", func(t *testing.T) {
mock.ExpectPTTL(tempkey).SetVal(time.Second * 20)
state, err := rs.GetState(context.Background())
assert.Nil(t, err)
assert.Equal(t, circuitbreaker.StateHalfOpen, state)
})
t.Run("key exist and not in halfOpen window and errors count reached the limit == open", func(t *testing.T) {
mock.ExpectPTTL(tempkey).SetVal(time.Second * 40)
mock.ExpectHGet(tempkey, failuresField).SetVal(strconv.Itoa(int(failureRateThreshold)))
state, err := rs.GetState(context.Background())
assert.Nil(t, err)
assert.Equal(t, circuitbreaker.StateOpen, state)
})
t.Run("key exist and not in halfOpen window and reachRateLimit got redis nil == close", func(t *testing.T) {
mock.ExpectPTTL(tempkey).SetVal(time.Second * 40)
mock.ExpectHGet(tempkey, failuresField).RedisNil()
state, err := rs.GetState(context.Background())
assert.Nil(t, err)
assert.Equal(t, circuitbreaker.StateClose, state)
})
}
func TestRedisStorage_Failure(t *testing.T) {
redisClient, mock := redismock.NewClientMock()
rs := circuitbreaker.NewRedisStorage(redisClient, options...)
t.Run("incr failure count to change state to open", func(t *testing.T) {
mock.ExpectHIncrBy(tempkey, failuresField, 2).SetVal(2)
mock.ExpectHDel(tempkey, successField).SetVal(2)
mock.ExpectExpire(tempkey, circuitbreaker.DefaultOpenWindow).SetVal(true)
err := rs.Failure(context.Background(), 2)
assert.Nil(t, err)
mock.ExpectPTTL(tempkey).SetVal(time.Second * 40)
mock.ExpectHGet(tempkey, failuresField).SetVal(strconv.Itoa(int(failureRateThreshold)))
state, err := rs.GetState(context.Background())
assert.Nil(t, err)
assert.Equal(t, circuitbreaker.StateOpen, state)
})
}
func TestRedisStorage_Success(t *testing.T) {
redisClient, mock := redismock.NewClientMock()
rs := circuitbreaker.NewRedisStorage(redisClient, options...)
t.Run("incr success count to change state to close", func(t *testing.T) {
mock.ExpectHIncrBy(tempkey, successField, 1).SetVal(1)
err := rs.Success(context.Background(), 1)
assert.Nil(t, err)
})
}