Skip to content

Commit

Permalink
Merge pull request #1192 from casperisfine/fix-sentinel-tests
Browse files Browse the repository at this point in the history
Update sentinel tests for redis-client 0.14
  • Loading branch information
byroot committed May 22, 2023
2 parents 5904289 + 3c7a0e6 commit bb67a53
Showing 1 changed file with 98 additions and 19 deletions.
117 changes: 98 additions & 19 deletions test/sentinel/sentinel_test.rb
Expand Up @@ -72,7 +72,14 @@ def test_sentinel_failover
s2 = {
sentinel: lambda do |command, *args|
commands[:s2] << [command, *args]
["127.0.0.1", "6381"]
case command
when "get-master-addr-by-name"
["127.0.0.1", "6381"]
when "sentinels"
[]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
}

Expand All @@ -87,7 +94,7 @@ def test_sentinel_failover
end

assert_equal commands[:s1], [%w[get-master-addr-by-name master1]]
assert_equal commands[:s2], [%w[get-master-addr-by-name master1]]
assert_equal commands[:s2], [%w[get-master-addr-by-name master1], ["sentinels", "master1"]]
end

def test_sentinel_failover_prioritize_healthy_sentinel
Expand All @@ -109,7 +116,17 @@ def test_sentinel_failover_prioritize_healthy_sentinel
s2 = {
sentinel: lambda do |command, *args|
commands[:s2] << [command, *args]
["127.0.0.1", "6381"]
case command
when "get-master-addr-by-name"
["127.0.0.1", "6381"]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
["ip", "127.0.0.1", "port", "26382"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
}

Expand All @@ -128,7 +145,7 @@ def test_sentinel_failover_prioritize_healthy_sentinel
end

assert_equal [%w[get-master-addr-by-name master1]], commands[:s1]
assert_equal [%w[get-master-addr-by-name master1]], commands[:s2]
assert_equal [%w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s2]
end

def test_sentinel_with_non_sentinel_options
Expand All @@ -146,7 +163,17 @@ def test_sentinel_with_non_sentinel_options
end,
sentinel: lambda do |command, *args|
commands[:s1] << [command, *args]
['127.0.0.1', port.to_s]
case command
when "get-master-addr-by-name"
["127.0.0.1", port.to_s]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
["ip", "127.0.0.1", "port", "26382"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
}
end
Expand All @@ -170,7 +197,7 @@ def test_sentinel_with_non_sentinel_options
end
end

assert_equal [%w[get-master-addr-by-name master1]], commands[:s1]
assert_equal [%w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
assert_equal [%w[auth foo], %w[role]], commands[:m1]
end

Expand All @@ -189,7 +216,17 @@ def test_authentication_for_sentinel
end,
sentinel: lambda do |command, *args|
commands[:s1] << [command, *args]
['127.0.0.1', port.to_s]
case command
when "get-master-addr-by-name"
["127.0.0.1", port.to_s]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
["ip", "127.0.0.1", "port", "26382"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
}
end
Expand All @@ -213,7 +250,7 @@ def test_authentication_for_sentinel
end
end

assert_equal [%w[auth foo], %w[get-master-addr-by-name master1]], commands[:s1]
assert_equal [%w[auth foo], %w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
assert_equal [%w[role]], commands[:m1]
end

Expand All @@ -232,8 +269,18 @@ def test_authentication_for_sentinel_and_redis
end,
sentinel: lambda do |command, *args|
commands[:s1] << [command, *args]
['127.0.0.1', port.to_s]
end
case command
when "get-master-addr-by-name"
["127.0.0.1", port.to_s]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
["ip", "127.0.0.1", "port", "26382"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end,
}
end

Expand All @@ -245,7 +292,10 @@ def test_authentication_for_sentinel_and_redis
role: lambda do
commands[:m1] << ['role']
['master']
end
end,
sentinel: lambda do |command, *args|
commands[:s2] << [command, *args]
end,
}

RedisMock.start(master) do |master_port|
Expand All @@ -256,7 +306,7 @@ def test_authentication_for_sentinel_and_redis
end
end

assert_equal [%w[auth foo], %w[get-master-addr-by-name master1]], commands[:s1]
assert_equal [%w[auth foo], %w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
assert_equal [%w[auth bar], %w[role]], commands[:m1]
end

Expand All @@ -275,7 +325,17 @@ def test_authentication_with_acl
end,
sentinel: lambda do |command, *args|
commands[:s1] << [command, *args]
['127.0.0.1', port.to_s]
case command
when "get-master-addr-by-name"
["127.0.0.1", port.to_s]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
["ip", "127.0.0.1", "port", "26382"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
}
end
Expand All @@ -299,7 +359,7 @@ def test_authentication_with_acl
end
end

assert_equal [%w[auth bob foo], %w[get-master-addr-by-name master1]], commands[:s1]
assert_equal [%w[auth bob foo], %w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
assert_equal [%w[auth alice bar], %w[role]], commands[:m1]
end

Expand All @@ -308,8 +368,17 @@ def test_sentinel_role_mismatch

sentinel = lambda do |port|
{
sentinel: lambda do |_command, *_args|
["127.0.0.1", port.to_s]
sentinel: lambda do |command, *_args|
case command
when "get-master-addr-by-name"
["127.0.0.1", port.to_s]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
}
end
Expand Down Expand Up @@ -342,13 +411,23 @@ def test_sentinel_retries

handler = lambda do |id, port|
{
sentinel: lambda do |_command, *_args|
sentinel: lambda do |command, *_args|
connections << id

if connections.count(id) < 2
:close
else
["127.0.0.1", port.to_s]
case command
when "get-master-addr-by-name"
["127.0.0.1", port.to_s]
when "sentinels"
[
["ip", "127.0.0.1", "port", "26381"],
["ip", "127.0.0.1", "port", "26382"],
]
else
raise "Unexpected command #{[command, *args].inspect}"
end
end
end
}
Expand All @@ -372,7 +451,7 @@ def test_sentinel_retries
end
end

assert_equal %i[s1 s2 s1], connections
assert_equal %i[s1 s2 s1 s1], connections

connections.clear

Expand Down

0 comments on commit bb67a53

Please sign in to comment.