-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_handler_test.go
199 lines (171 loc) · 6.47 KB
/
redis_handler_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
193
194
195
196
197
198
199
package delayed_job
import (
"net"
"strings"
"testing"
"github.com/gomodule/redigo/redis"
)
func TestRedisHandlerParameterError(t *testing.T) {
_, e := newRedisHandler(nil,
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "ctx is nil" != e.Error() {
t.Error("excepted error is 'ctx is nil', but actual is", e)
}
_, e = newRedisHandler(map[string]interface{}{},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "'redis' in the ctx is required" != e.Error() {
t.Error("excepted error is ''redis' in the ctx is required', but actual is", e)
}
_, e = newRedisHandler(map[string]interface{}{"redis": 0},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "'redis' in the ctx is not a *Redis - int" != e.Error() {
t.Error("excepted error is 'redis in the ctx is not a backend - int', but actual is", e)
}
_, e = newRedisHandler(map[string]interface{}{}, nil)
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "params is nil" != e.Error() {
t.Error("excepted error is 'params is nil', but actual is", e)
}
var client *redis_gateway = nil
_, e = newRedisHandler(map[string]interface{}{"redis": client},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "'redis' in the ctx is nil" != e.Error() {
t.Error("excepted error is ''redis' in the ctx is nil', but actual is", e)
}
redisTest(t, func(client *redis_gateway, c redis.Conn) {
_, e := newRedisHandler(map[string]interface{}{"redis": client},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "'command' or 'commands' is required" != e.Error() {
t.Error("excepted error is ''command' or 'commands' is required', but actual is", e)
}
})
}
// redis_client.c <- &redis_request{commands: [][]string{{"SET", "a1", "1223"}}}
// redis_client.c <- &redis_request{commands: [][]string{{"SET", "a2", "1224"}}}
// redis_client.Send([][]string{{"SET", "a3", "1225"}})
// redis_client.Send([][]string{{"SET", "a4", "1226"}})
// redis_client.Send([][]string{{"SET", "a6", "1227"}})
func TestRedisHandler(t *testing.T) {
for idx, test := range [][]map[string]interface{}{
[]map[string]interface{}{{"command": "SET", "arg0": "a1", "arg1": "1223"},
{"command": "SET", "arg0": "a2", "arg1": "1224"},
{"command": "SET", "arg0": "a3", "arg1": "1225"},
{"command": "SET", "arg0": "a4", "arg1": "1226"},
{"command": "SET", "arg0": "a5", "arg1": "1227"}},
[]map[string]interface{}{{"commands": []interface{}{map[string]interface{}{"command": "SET", "arg0": "a1", "arg1": "1223"},
map[string]interface{}{"command": "SET", "arg0": "a2", "arg1": "1224"},
map[string]interface{}{"command": "SET", "arg0": "a3", "arg1": "1225"},
map[string]interface{}{"command": "SET", "arg0": "a4", "arg1": "1226"},
map[string]interface{}{"command": "SET", "arg0": "a5", "arg1": "1227"}}}},
[]map[string]interface{}{{"commands": []interface{}{[]interface{}{"SET", "a1", "1223"},
[]interface{}{"SET", "a2", "1224"},
[]interface{}{"SET", "a3", "1225"},
[]interface{}{"SET", "a4", "1226"},
[]interface{}{"SET", "a5", "1227"}}}},
[]map[string]interface{}{{"commands": []interface{}{[]string{"SET", "a1", "1223"},
[]string{"SET", "a2", "1224"},
[]string{"SET", "a3", "1225"},
[]string{"SET", "a4", "1226"},
[]string{"SET", "a5", "1227"}}}},
[]map[string]interface{}{{"arguments": map[string]interface{}{"a1": "1223", "a2": "1224"},
"commands": []interface{}{[]string{"SET", "a1", "$a1"},
[]string{"SET", "a2", "$a2"},
[]string{"SET", "a3", "1225"},
[]string{"SET", "a4", "1226"},
[]string{"SET", "a5", "1227"}}}},
[]map[string]interface{}{{"arguments": map[string]string{"a1": "1223", "a2": "1224"},
"commands": []interface{}{[]string{"SET", "a1", "$a1"},
[]string{"SET", "a2", "$a2"},
[]string{"SET", "a3", "1225"},
[]string{"SET", "a4", "1226"},
[]string{"SET", "a5", "1227"}}}},
[]map[string]interface{}{{"arguments": []interface{}{map[string]string{"a1": "1223", "a2": "1224"}},
"commands": []interface{}{[]string{"SET", "a1", "$[0].a1"},
[]string{"SET", "a2", "$[0].a2"},
[]string{"SET", "a3", "1225"},
[]string{"SET", "a4", "1226"},
[]string{"SET", "a5", "1227"}}}},
[]map[string]interface{}{{"arguments": []interface{}{"1223", map[string]interface{}{"a1": "1223", "a2": "1224"}},
"commands": []interface{}{[]string{"SET", "a1", "$[0]"},
[]string{"SET", "a2", "$[1].a2"},
[]string{"SET", "a3", "1225"},
[]string{"SET", "a4", "1226"},
[]string{"SET", "a5", "1227"}}}}} {
redisTest(t, func(client *redis_gateway, c redis.Conn) {
for _, args := range test {
handler, e := newRedisHandler(map[string]interface{}{"redis": client}, args)
if nil != e {
t.Error("execute [", idx, "]", e)
return
}
e = handler.Perform()
if nil != e {
t.Error("execute [", idx, "]", e)
return
}
}
checkResult(t, c, "GET", "a1", "1223")
checkResult(t, c, "GET", "a2", "1224")
checkResult(t, c, "GET", "a3", "1225")
checkResult(t, c, "GET", "a4", "1226")
checkResult(t, c, "GET", "a5", "1227")
})
}
}
func TestRedisHandlerFailed(t *testing.T) {
listener, e := net.Listen("tcp", ":0")
if nil != e {
t.Error(e)
return
}
old := *redisAddress
ss := strings.Split(listener.Addr().String(), ":")
*redisAddress = "127.0.0.1:" + ss[len(ss)-1]
oldSim := *redisSim
*redisSim = false
defer func() {
*redisSim = oldSim
*redisAddress = old
listener.Close()
}()
go func() {
for {
t, e := listener.Accept()
if nil != e {
return
}
t.Close()
}
}()
for _, test := range []map[string]interface{}{
{"command": "SET", "arg0": "a2", "arg1": "1224"},
{"commands": []interface{}{[]interface{}{"SET", "a1", "1223"},
[]interface{}{"SET", "a2", "1224"},
[]interface{}{"SET", "a3", "1225"},
[]interface{}{"SET", "a4", "1226"},
[]interface{}{"SET", "a5", "1227"}}}} {
redisTest(t, func(client *redis_gateway, c redis.Conn) {
handler, e := newRedisHandler(map[string]interface{}{"redis": client}, test)
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
return
}
})
}
}