Skip to content

Commit ef518f6

Browse files
authored
test: move a test case to the right place (#394)
1 parent 97707c5 commit ef518f6

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gemspec name: 'redis-cluster-client'
66
gem 'async-redis', platform: :mri
77
gem 'benchmark-ips'
88
gem 'hiredis-client', '~> 0.6'
9+
gem 'logger'
910
gem 'memory_profiler'
1011
gem 'minitest'
1112
gem 'rake'

bin/pubsub

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module PubSubDebug
4949
handle_errors('Subscriber') do
5050
e = ps.next_event(0.01)
5151
log "#{role}: recv: #{e.nil? ? 'nil' : e}"
52-
ps.call('ssubscribe', c) if e.first == 'sunsubscribe'
52+
ps.call('ssubscribe', c) if !e.nil? && e.first == 'sunsubscribe'
5353
end
5454
ensure
5555
sleep 1.0

test/cluster_controller.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,11 @@ def wait_failover(clients, primary_node_key:, replica_node_key:, max_attempts:)
398398

399399
def wait_replication_delay(clients, replica_size:, timeout:)
400400
timeout_msec = timeout.to_i * 1000
401+
server_side_timeout = timeout_msec > 100 ? timeout_msec - 100 : 10
402+
401403
wait_for_state(clients, max_attempts: clients.size + 1) do |client|
402404
swap_timeout(client, timeout: 0.1) do |cli|
403-
cli.blocking_call(timeout, 'WAIT', replica_size, timeout_msec - 100) if primary_client?(cli)
405+
cli.blocking_call(timeout, 'WAIT', replica_size, server_side_timeout) if primary_client?(cli)
404406
end
405407
true
406408
rescue ::RedisClient::ConnectionError

test/redis_client/test_cluster.rb

+3
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def test_pubsub_with_wrong_command
580580
assert_nil(pubsub.call_v(%w[SUBSCRIBE]))
581581
assert_raises(::RedisClient::CommandError, 'unknown command') { pubsub.next_event }
582582
assert_raises(::RedisClient::CommandError, 'wrong number of arguments') { pubsub.next_event }
583+
assert_nil(pubsub.next_event(0.01))
583584
pubsub.close
584585
end
585586

@@ -891,6 +892,8 @@ def wait_for_replication
891892
server_side_timeout = (TEST_TIMEOUT_SEC * 1000).to_i
892893
swap_timeout(@client, timeout: 0.1) do |client|
893894
client&.blocking_call(client_side_timeout, 'WAIT', TEST_REPLICA_SIZE, server_side_timeout)
895+
rescue RedisClient::ConnectionError
896+
# ignore
894897
end
895898
end
896899

test/test_against_cluster_broken.rb

+26-10
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,37 @@ def setup
2121
print "\n"
2222
@logger.info('setup: test')
2323
prepare_test_data
24-
@clients = Array.new(3) do
25-
build_client.tap { |c| c.call('echo', 'init') }
26-
end
24+
@clients = Array.new(3) { build_client.tap { |c| c.call('echo', 'init') } }
2725
end
2826

2927
def teardown
3028
@logger.info('teardown: test')
3129
revive_dead_nodes
3230
@clients.each(&:close)
3331
@controller&.close
34-
refute(@captured_commands.count('cluster', 'nodes').zero?, @captured_commands.to_a.map(&:command))
35-
print "#{@redirect_count.get}, "\
36-
"ClusterNodesCall: #{@captured_commands.count('cluster', 'nodes')}, "\
37-
"ClusterDownError: #{@cluster_down_error_count} = "
3832
end
3933

4034
def test_client_patience
35+
do_manual_failover
36+
wait_for_cluster_to_be_ready
37+
do_assertions(offset: 0)
38+
4139
# a replica
4240
sacrifice_replica = @controller.select_sacrifice_of_replica
4341
kill_a_node(sacrifice_replica)
4442
wait_for_cluster_to_be_ready(ignore: [sacrifice_replica])
45-
do_assertions(offset: 0)
43+
do_assertions(offset: 1)
4644

4745
# a primary
4846
sacrifice_primary = @controller.select_sacrifice_of_primary
4947
kill_a_node(sacrifice_primary)
5048
wait_for_cluster_to_be_ready(ignore: [sacrifice_replica, sacrifice_primary])
51-
do_assertions(offset: 1)
49+
do_assertions(offset: 2)
5250

5351
# recovery
5452
revive_dead_nodes
5553
wait_for_cluster_to_be_ready
56-
do_assertions(offset: 2)
54+
do_assertions(offset: 3)
5755
end
5856

5957
private
@@ -77,6 +75,10 @@ def prepare_test_data
7775
end
7876

7977
def do_assertions(offset:)
78+
@captured_commands.clear
79+
@redirect_count.clear
80+
@cluster_down_error_count = 0
81+
8082
log_info('assertions') do
8183
log_info('assertions: single') do
8284
NUMBER_OF_KEYS.times do |i|
@@ -122,6 +124,8 @@ def do_assertions(offset:)
122124
assert_equal(want, got, 'Case: Transaction: SET')
123125
end
124126
end
127+
128+
log_metrics
125129
end
126130
end
127131

@@ -139,6 +143,12 @@ def wait_for_cluster_to_be_ready(ignore: [])
139143
end
140144
end
141145

146+
def do_manual_failover
147+
log_info('failover') do
148+
@controller.failover
149+
end
150+
end
151+
142152
def kill_a_node(sacrifice)
143153
log_info("kill #{sacrifice.config.host}:#{sacrifice.config.port}") do
144154
refute_nil(sacrifice, "#{sacrifice.config.host}:#{sacrifice.config.port}")
@@ -171,6 +181,12 @@ def log_info(message)
171181
@logger.info(" done: #{message}")
172182
end
173183

184+
def log_metrics
185+
print "#{@redirect_count.get}, "\
186+
"ClusterNodesCall: #{@captured_commands.count('cluster', 'nodes')}, "\
187+
"ClusterDownError: #{@cluster_down_error_count}\n"
188+
end
189+
174190
def retryable(attempts: MAX_ATTEMPTS, wait_sec: WAIT_SEC)
175191
loop do
176192
raise MaxRetryExceeded if attempts <= 0

test/test_against_cluster_state.rb

-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ def teardown
2929
"ClusterNodesCall: #{@captured_commands.count('cluster', 'nodes')} = "
3030
end
3131

32-
def test_the_state_of_cluster_failover
33-
@controller.failover
34-
1000.times { |i| assert_equal('OK', @client.call('SET', "key#{i}", i)) }
35-
wait_for_replication
36-
1000.times { |i| assert_equal(i.to_s, @client.call('GET', "key#{i}")) }
37-
refute(@redirect_count.zero?, @redirect_count.get)
38-
end
39-
4032
def test_the_state_of_cluster_resharding
4133
resharded_keys = nil
4234
do_resharding_test do |keys|

0 commit comments

Comments
 (0)