Skip to content

Commit 485efe6

Browse files
committed
Tests for healthy slaves
1 parent 5a50939 commit 485efe6

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

t/sentinel.t

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Test::Nginx::Socket::Lua;
44
use Cwd qw(cwd);
55

6-
repeat_each(2);
6+
#repeat_each(2);
77

88
plan tests => repeat_each() * (3 * blocks());
99

@@ -38,7 +38,7 @@ __DATA__
3838
end
3939
4040
local redis_sentinel = require "resty.redis.sentinel"
41-
41+
4242
local master, err = redis_sentinel.get_master(sentinel, "mymaster")
4343
if not master then
4444
ngx.say(err)
@@ -74,7 +74,57 @@ port: 6379
7474
end
7575
7676
local redis_sentinel = require "resty.redis.sentinel"
77-
77+
78+
local slaves, err = redis_sentinel.get_slaves(sentinel, "mymaster")
79+
if not slaves then
80+
ngx.say(err)
81+
else
82+
-- order is undefined
83+
local all = {}
84+
for i,slave in ipairs(slaves) do
85+
all[i] = tonumber(slave.port)
86+
end
87+
table.sort(all)
88+
for _,p in ipairs(all) do
89+
ngx.say(p)
90+
end
91+
end
92+
93+
sentinel:close()
94+
';
95+
}
96+
--- request
97+
GET /t
98+
--- response_body
99+
6378
100+
6380
101+
--- no_error_log
102+
[error]
103+
104+
=== TEST 3: Get only healthy slaves
105+
--- http_config eval: $::HttpConfig
106+
--- config
107+
location /t {
108+
content_by_lua '
109+
110+
local redis = require "resty.redis"
111+
local r = redis.new()
112+
r:connect("127.0.0.1", 6378)
113+
r:slaveof("127.0.0.1", 7000)
114+
115+
ngx.sleep(9)
116+
117+
local redis_connector = require "resty.redis.connector"
118+
local rc = redis_connector.new()
119+
120+
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:6381" }
121+
if not sentinel then
122+
ngx.say("failed to connect: ", err)
123+
return
124+
end
125+
126+
local redis_sentinel = require "resty.redis.sentinel"
127+
78128
local slaves, err = redis_sentinel.get_slaves(sentinel, "mymaster")
79129
if not slaves then
80130
ngx.say(err)
@@ -90,9 +140,9 @@ port: 6379
90140
}
91141
--- request
92142
GET /t
143+
--- timeout: 10
93144
--- response_body
94145
host: 127.0.0.1
95146
port: 6380
96147
--- no_error_log
97148
[error]
98-

0 commit comments

Comments
 (0)