Skip to content

Commit 53175b5

Browse files
committed
Test slaves rotation
1 parent 9696b2d commit 53175b5

File tree

1 file changed

+138
-4
lines changed

1 file changed

+138
-4
lines changed

t/sentinel.t

Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,142 @@ location /t {
147147
--- no_error_log
148148
[error]
149149

150+
=== TEST 4: Slaves rotation is off by default
151+
--- http_config eval: $::HttpConfig
152+
--- config
153+
location /t {
154+
content_by_lua_block {
155+
local rc = require("resty.redis.connector").new({
156+
url = "sentinel://mymaster:s",
157+
sentinels = {
158+
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 },
159+
},
160+
})
161+
162+
for i = 1, 10 do
163+
local sentinel, err = rc:connect()
164+
assert(sentinel and not err, "sentinel should connect without error")
165+
166+
local result, err = sentinel:get("very_special_key")
167+
assert(result and not err, "get should run without error")
168+
169+
sentinel:close()
170+
end
171+
172+
-- Now check slaves metrics for processed commands
173+
-- Only one slave should be used
174+
175+
local slave_1_was_used = false
176+
177+
rc = require("resty.redis.connector").new()
178+
179+
-- SLAVE_1
180+
local slave_1, err = rc:connect({
181+
port = $TEST_NGINX_REDIS_PORT_SL1,
182+
})
183+
assert(slave_1 and not err, "slave_1 should connect without error")
184+
185+
local metrics, error = slave_1:info("commandstats")
186+
assert(metrics and not error, "info should run without error")
187+
if (metrics:find("cmdstat_get:calls=10")) then
188+
slave_1_was_used = true
189+
else
190+
assert(not metrics:find("cmdstat_get:calls"), "SLAVE_1 " .. metrics)
191+
end
192+
193+
slave_1:close()
194+
195+
-- SLAVE_2
196+
local slave_2, err = rc:connect({
197+
port = $TEST_NGINX_REDIS_PORT_SL2,
198+
})
199+
assert(slave_2 and not err, "slave_2 should connect without error")
200+
201+
local metrics, error = slave_2:info("commandstats")
202+
assert(metrics and not error, "info should run without error")
203+
if (slave_1_was_used) then
204+
assert(not metrics:find("cmdstat_get:calls"), "SLAVE_2 " .. metrics)
205+
else
206+
assert(metrics:find("cmdstat_get:calls=10"), "SLAVE_2 " .. metrics)
207+
end
208+
209+
slave_2:close()
210+
}
211+
}
212+
--- request
213+
GET /t
214+
--- no_error_log
215+
[error]
216+
217+
=== TEST 5: When slaves rotation is set slaves should be rotated
218+
--- http_config eval: $::HttpConfig
219+
--- config
220+
location /t {
221+
content_by_lua_block {
222+
local rc = require("resty.redis.connector").new({
223+
url = "sentinel://mymaster:s",
224+
sentinels = {
225+
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 },
226+
},
227+
rotate_slaves=true,
228+
})
229+
230+
for i = 1, 10 do
231+
local sentinel, err = rc:connect()
232+
assert(sentinel and not err, "sentinel should connect without error")
233+
234+
local result, err = sentinel:get("very_special_key")
235+
assert(result and not err, "get should run without error")
236+
237+
sentinel:close()
238+
end
239+
240+
-- Now check slaves metrics for processed commands
241+
-- Both slaves should be used equally
242+
243+
local slave_1_was_used_in_previous_test = false
244+
245+
rc = require("resty.redis.connector").new()
246+
247+
-- SLAVE_1
248+
local slave_1, err = rc:connect({
249+
port = $TEST_NGINX_REDIS_PORT_SL1,
250+
})
251+
assert(slave_1 and not err, "slave_1 should connect without error")
252+
253+
local metrics, error = slave_1:info("commandstats")
254+
assert(metrics and not error, "info should run without error")
255+
if (metrics:find("cmdstat_get:calls=15")) then
256+
slave_1_was_used_in_previous_test = true
257+
else
258+
assert(metrics:find("cmdstat_get:calls=5"), "SLAVE_1 " .. metrics)
259+
end
260+
261+
slave_1:close()
262+
263+
-- SLAVE_2
264+
local slave_2, err = rc:connect({
265+
port = $TEST_NGINX_REDIS_PORT_SL2,
266+
})
267+
assert(slave_2 and not err, "slave_2 should connect without error")
268+
269+
local metrics, error = slave_2:info("commandstats")
270+
assert(metrics and not error, "info should run without error")
271+
if (slave_1_was_used_in_previous_test) then
272+
assert(metrics:find("cmdstat_get:calls=5"), "SLAVE_2 " .. metrics)
273+
else
274+
assert(metrics:find("cmdstat_get:calls=15"), "SLAVE_2 " .. metrics)
275+
end
276+
277+
slave_2:close()
278+
}
279+
}
280+
--- request
281+
GET /t
282+
--- no_error_log
283+
[error]
150284

151-
=== TEST 4: Get only healthy slaves
285+
=== TEST 6: Get only healthy slaves
152286
--- http_config eval: $::HttpConfig
153287
--- config
154288
location /t {
@@ -210,7 +344,7 @@ GET /t
210344
[error]
211345

212346

213-
=== TEST 5: connector.connect_via_sentinel
347+
=== TEST 7: connector.connect_via_sentinel
214348
--- http_config eval: $::HttpConfig
215349
--- config
216350
location /t {
@@ -242,7 +376,7 @@ GET /t
242376
[error]
243377

244378

245-
=== TEST 6: regression for slave sorting (iss12)
379+
=== TEST 8: regression for slave sorting (iss12)
246380
--- http_config eval: $::HttpConfig
247381
--- config
248382
location /t {
@@ -279,7 +413,7 @@ GET /t
279413
--- no_error_log
280414
[error]
281415

282-
=== TEST 7: connect with acl
416+
=== TEST 9: connect with acl
283417
--- http_config eval: $::HttpConfig
284418
--- config
285419
location /t {

0 commit comments

Comments
 (0)