|
| 1 | +# vim:set ft= ts=4 sw=4 et: |
| 2 | + |
| 3 | +use Test::Nginx::Socket::Lua; |
| 4 | +use Cwd qw(cwd); |
| 5 | + |
| 6 | +repeat_each(2); |
| 7 | + |
| 8 | +plan tests => repeat_each() * (3 * blocks()); |
| 9 | + |
| 10 | +my $pwd = cwd(); |
| 11 | + |
| 12 | +our $HttpConfig = qq{ |
| 13 | + lua_package_path "$pwd/lib/?.lua;;"; |
| 14 | +}; |
| 15 | + |
| 16 | +$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; |
| 17 | +$ENV{TEST_NGINX_REDIS_PORT} ||= 6379; |
| 18 | + |
| 19 | +no_long_string(); |
| 20 | +#no_diff(); |
| 21 | + |
| 22 | +run_tests(); |
| 23 | + |
| 24 | +__DATA__ |
| 25 | + |
| 26 | +=== TEST 1: Get the master |
| 27 | +--- http_config eval: $::HttpConfig |
| 28 | +--- config |
| 29 | + location /t { |
| 30 | + content_by_lua ' |
| 31 | + local redis_connector = require "resty.redis.connector" |
| 32 | + local rc = redis_connector.new() |
| 33 | +
|
| 34 | + local sentinel, err = rc:connect{ url = "redis://127.0.0.1:6381" } |
| 35 | + if not sentinel then |
| 36 | + ngx.say("failed to connect: ", err) |
| 37 | + return |
| 38 | + end |
| 39 | +
|
| 40 | + local redis_sentinel = require "resty.redis.sentinel" |
| 41 | + |
| 42 | + local master, err = redis_sentinel.get_master(sentinel, "mymaster") |
| 43 | + if not master then |
| 44 | + ngx.say(err) |
| 45 | + else |
| 46 | + ngx.say("host: ", master.host) |
| 47 | + ngx.say("port: ", master.port) |
| 48 | + end |
| 49 | +
|
| 50 | + sentinel:close() |
| 51 | + '; |
| 52 | + } |
| 53 | +--- request |
| 54 | + GET /t |
| 55 | +--- response_body |
| 56 | +host: 127.0.0.1 |
| 57 | +port: 6379 |
| 58 | +--- no_error_log |
| 59 | +[error] |
| 60 | + |
| 61 | + |
| 62 | +=== TEST 2: Get slaves |
| 63 | +--- http_config eval: $::HttpConfig |
| 64 | +--- config |
| 65 | + location /t { |
| 66 | + content_by_lua ' |
| 67 | + local redis_connector = require "resty.redis.connector" |
| 68 | + local rc = redis_connector.new() |
| 69 | +
|
| 70 | + local sentinel, err = rc:connect{ url = "redis://127.0.0.1:6381" } |
| 71 | + if not sentinel then |
| 72 | + ngx.say("failed to connect: ", err) |
| 73 | + return |
| 74 | + end |
| 75 | +
|
| 76 | + local redis_sentinel = require "resty.redis.sentinel" |
| 77 | + |
| 78 | + local slaves, err = redis_sentinel.get_slaves(sentinel, "mymaster") |
| 79 | + if not slaves then |
| 80 | + ngx.say(err) |
| 81 | + else |
| 82 | + for _,slave in ipairs(slaves) do |
| 83 | + ngx.say("host: ", slave.host) |
| 84 | + ngx.say("port: ", slave.port) |
| 85 | + end |
| 86 | + end |
| 87 | +
|
| 88 | + sentinel:close() |
| 89 | + '; |
| 90 | + } |
| 91 | +--- request |
| 92 | + GET /t |
| 93 | +--- response_body |
| 94 | +host: 127.0.0.1 |
| 95 | +port: 6380 |
| 96 | +--- no_error_log |
| 97 | +[error] |
| 98 | + |
0 commit comments