|
1 |
| -require_relative "helper" |
| 1 | +# frozen_string_literal: true |
2 | 2 |
|
3 |
| -class SentinelCommandsTest < Test::Unit::TestCase |
| 3 | +require_relative 'helper' |
4 | 4 |
|
| 5 | +# @see https://redis.io/topics/sentinel#sentinel-commands Sentinel commands |
| 6 | +class SentinelCommandsTest < Test::Unit::TestCase |
5 | 7 | include Helper::Client
|
6 | 8 |
|
7 |
| - def test_sentinel_command_master |
| 9 | + MASTER_PORT = PORT.to_s |
| 10 | + SLAVE_PORT = '6382' |
| 11 | + SENTINEL_PORT = '6400' |
| 12 | + MASTER_NAME = 'master1' |
| 13 | + LOCALHOST = '127.0.0.1' |
8 | 14 |
|
9 |
| - handler = lambda do |id| |
10 |
| - { |
11 |
| - :sentinel => lambda do |command, *args| |
12 |
| - ["name", "master1", "ip", "127.0.0.1"] |
13 |
| - end |
14 |
| - } |
15 |
| - end |
| 15 | + def build_sentinel_client |
| 16 | + Redis.new(host: LOCALHOST, port: SENTINEL_PORT, timeout: TIMEOUT) |
| 17 | + end |
16 | 18 |
|
17 |
| - RedisMock.start(handler.call(:s1)) do |port| |
18 |
| - redis = Redis.new(:host => "127.0.0.1", :port => port) |
| 19 | + def test_sentinel_command_master |
| 20 | + redis = build_sentinel_client |
| 21 | + result = redis.sentinel('master', MASTER_NAME) |
19 | 22 |
|
20 |
| - result = redis.sentinel('master', 'master1') |
21 |
| - assert_equal result, { "name" => "master1", "ip" => "127.0.0.1" } |
22 |
| - end |
| 23 | + assert_equal result['name'], MASTER_NAME |
| 24 | + assert_equal result['ip'], LOCALHOST |
23 | 25 | end
|
24 | 26 |
|
25 | 27 | def test_sentinel_command_masters
|
| 28 | + redis = build_sentinel_client |
| 29 | + result = redis.sentinel('masters') |
26 | 30 |
|
27 |
| - handler = lambda do |id| |
28 |
| - { |
29 |
| - :sentinel => lambda do |command, *args| |
30 |
| - [%w[name master1 ip 127.0.0.1 port 6381], %w[name master1 ip 127.0.0.1 port 6382]] |
31 |
| - end |
32 |
| - } |
33 |
| - end |
34 |
| - |
35 |
| - RedisMock.start(handler.call(:s1)) do |port| |
36 |
| - redis = Redis.new(:host => "127.0.0.1", :port => port) |
37 |
| - |
38 |
| - result = redis.sentinel('masters') |
39 |
| - assert_equal result[0], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6381" } |
40 |
| - assert_equal result[1], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6382" } |
41 |
| - end |
| 31 | + assert_equal result[0]['name'], MASTER_NAME |
| 32 | + assert_equal result[0]['ip'], LOCALHOST |
| 33 | + assert_equal result[0]['port'], MASTER_PORT |
42 | 34 | end
|
43 | 35 |
|
44 |
| - def test_sentinel_command_get_master_by_name |
| 36 | + def test_sentinel_command_slaves |
| 37 | + redis = build_sentinel_client |
| 38 | + result = redis.sentinel('slaves', MASTER_NAME) |
45 | 39 |
|
46 |
| - handler = lambda do |id| |
47 |
| - { |
48 |
| - :sentinel => lambda do |command, *args| |
49 |
| - ["127.0.0.1", "6381"] |
50 |
| - end |
51 |
| - } |
52 |
| - end |
| 40 | + assert_equal result[0]['name'], "#{LOCALHOST}:#{SLAVE_PORT}" |
| 41 | + assert_equal result[0]['ip'], LOCALHOST |
| 42 | + assert_equal result[0]['port'], SLAVE_PORT |
| 43 | + end |
53 | 44 |
|
54 |
| - RedisMock.start(handler.call(:s1)) do |port| |
55 |
| - redis = Redis.new(:host => "127.0.0.1", :port => port) |
| 45 | + def test_sentinel_command_sentinels |
| 46 | + redis = build_sentinel_client |
| 47 | + result = redis.sentinel('sentinels', MASTER_NAME) |
56 | 48 |
|
57 |
| - result = redis.sentinel('get-master-addr-by-name', 'master1') |
58 |
| - assert_equal result, ["127.0.0.1", "6381"] |
59 |
| - end |
| 49 | + assert_equal result[0]['ip'], LOCALHOST |
| 50 | + |
| 51 | + actual_ports = result.map { |r| r['port'] }.sort |
| 52 | + expected_ports = (SENTINEL_PORT.to_i + 1..SENTINEL_PORT.to_i + 2).map(&:to_s) |
| 53 | + assert_equal actual_ports, expected_ports |
| 54 | + end |
| 55 | + |
| 56 | + def test_sentinel_command_get_master_by_name |
| 57 | + redis = build_sentinel_client |
| 58 | + result = redis.sentinel('get-master-addr-by-name', MASTER_NAME) |
| 59 | + |
| 60 | + assert_equal result, [LOCALHOST, MASTER_PORT] |
60 | 61 | end
|
61 | 62 |
|
62 | 63 | def test_sentinel_command_ckquorum
|
63 |
| - handler = lambda do |id| |
64 |
| - { |
65 |
| - :sentinel => lambda do |command, *args| |
66 |
| - "+OK 2 usable Sentinels. Quorum and failover authorization can be reached" |
67 |
| - end |
68 |
| - } |
69 |
| - end |
70 |
| - |
71 |
| - RedisMock.start(handler.call(:s1)) do |port| |
72 |
| - redis = Redis.new(:host => "127.0.0.1", :port => port) |
73 |
| - |
74 |
| - result = redis.sentinel('ckquorum', 'master1') |
75 |
| - assert_equal result, "OK 2 usable Sentinels. Quorum and failover authorization can be reached" |
76 |
| - end |
| 64 | + redis = build_sentinel_client |
| 65 | + result = redis.sentinel('ckquorum', MASTER_NAME) |
| 66 | + |
| 67 | + assert_equal result, 'OK 3 usable Sentinels. Quorum and failover authorization can be reached' |
77 | 68 | end
|
78 | 69 | end
|
0 commit comments