-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands_test.go
137 lines (104 loc) · 3.85 KB
/
commands_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
package twunproxy
import (
"github.com/golang/mock/gomock"
"testing"
"time"
)
func TestSingleConnectionNonExistentKeyBLPOP(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
key := "parsed:soccer:league:event:match"
response := "A correct response"
mockConn, mockPool := setupMockPool(ctrl)
mockConn.EXPECT().Do("BLPOP", key, 5.0).Return([]interface{}{[]byte(key), []byte(response)}, nil)
mockConn.EXPECT().Close()
proxy := getMockProxy(mockPool)
if resp, err := proxy.BLPop(key, 5*time.Second); err != nil || resp != response {
t.Fatalf("Did not receive expected command response.")
}
if _, ok := proxy.KeyInstance[key]; !ok {
t.Fatal("Expected mapping entry for Redis key.")
}
}
func TestMultipleConnectionNonExtantKeyBLPopReturnsCorrectlyAndAddsMapping(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
key := "parsed:soccer:league:event:match"
response := "A correct response"
mockConn1, mockPool1 := setupMockPool(ctrl)
mockConn2, mockPool2 := setupMockPool(ctrl)
mockConn1.EXPECT().Do("BLPOP", key, 5.0)
mockConn1.EXPECT().Close()
mockConn2.EXPECT().Do("BLPOP", key, 5.0).Return([]interface{}{[]byte(key), []byte(response)}, nil)
mockConn2.EXPECT().Close()
proxy := getMockProxy(mockPool1, mockPool2)
if resp, err := proxy.BLPop(key, 5*time.Second); err != nil || resp != response {
t.Fatalf("Did not receive expected command response.")
}
if _, ok := proxy.KeyInstance[key]; !ok {
t.Fatal("Expected mapping entry for Redis key.")
}
}
func TestSingleConnectionExtantKeyBLPopReturnsCorrectly(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
key := "parsed:soccer:league:event:match"
response := "A correct response"
mockConn, mockPool := setupMockPool(ctrl)
mockConn.EXPECT().Do("BLPOP", key, 5.0).Return([]interface{}{[]byte(key), []byte(response)}, nil)
mockConn.EXPECT().Close()
proxy := getMockProxy(mockPool)
proxy.KeyInstance[key] = mockPool
if resp, err := proxy.BLPop(key, 5*time.Second); err != nil || resp != response {
t.Fatalf("Did not receive expected command response.")
}
}
func TestMultipleConnectionExtantKeyBLPopReturnsCorrectly(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
key := "parsed:soccer:league:event:match"
response := "A correct response"
mockConn1, mockPool1 := setupMockPool(ctrl)
mockPool2 := NewMockConnGetter(ctrl)
mockConn1.EXPECT().Do("BLPOP", key, 5.0).Return([]interface{}{[]byte(key), []byte(response)}, nil)
mockConn1.EXPECT().Close()
proxy := getMockProxy(mockPool1, mockPool2)
proxy.KeyInstance[key] = mockPool1
if resp, err := proxy.BLPop(key, 5*time.Second); err != nil || resp != response {
t.Fatalf("Did not receive expected command response.")
}
}
func TestPromoteExecutesAgainstEachPool(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockConn1, mockPool1 := setupMockPool(ctrl)
mockConn2, mockPool2 := setupMockPool(ctrl)
mockConn1.EXPECT().Do("SLAVEOF", "NO", "ONE").Return(interface{}("+OK\r\n"), nil)
mockConn1.EXPECT().Close()
mockConn2.EXPECT().Do("SLAVEOF", "NO", "ONE").Return(interface{}("+OK\r\n"), nil)
mockConn2.EXPECT().Close()
c, err := getMockProxy(mockPool1, mockPool2).Promote()
if err != nil {
t.Fatalf(err.Error())
}
if c != 2 {
t.Fatalf("Incorrect number of commands issued: %d", c)
}
}
func TestBGSaveExecutesAgainstEachPool(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockConn1, mockPool1 := setupMockPool(ctrl)
mockConn2, mockPool2 := setupMockPool(ctrl)
mockConn1.EXPECT().Do("BGSAVE").Return(interface{}("+OK\r\n"), nil)
mockConn1.EXPECT().Close()
mockConn2.EXPECT().Do("BGSAVE").Return(interface{}("+OK\r\n"), nil)
mockConn2.EXPECT().Close()
c, err := getMockProxy(mockPool1, mockPool2).BGSave(1)
if err != nil {
t.Fatalf(err.Error())
}
if c != 2 {
t.Fatalf("Incorrect number of commands issued: %d", c)
}
}