Skip to content

Commit e8f2518

Browse files
authored
Merge pull request #798 from supercaracal/fix-cluster-tests-that-occasionally-fails-on-ci
Fix several test cases that occasionally fail on CI
2 parents 1453d1b + f2ce248 commit e8f2518

File tree

6 files changed

+57
-28
lines changed

6 files changed

+57
-28
lines changed

.travis.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ env:
2626
global:
2727
- VERBOSE=true
2828
- TIMEOUT=30
29+
- LOW_TIMEOUT=0.01
2930
matrix:
3031
- DRIVER=ruby REDIS_BRANCH=3.0
3132
- DRIVER=ruby REDIS_BRANCH=3.2
@@ -43,17 +44,14 @@ branches:
4344

4445
matrix:
4546
exclude:
46-
# hiredis
4747
- rvm: jruby-9.1.17.0
48-
env: DRIVER=hiredis REDIS_BRANCH=3.0
48+
include:
4949
- rvm: jruby-9.1.17.0
50-
env: DRIVER=hiredis REDIS_BRANCH=3.2
51-
52-
# synchrony
50+
env: DRIVER=ruby REDIS_BRANCH=3.0 LOW_TIMEOUT=0.1
5351
- rvm: jruby-9.1.17.0
54-
env: DRIVER=synchrony REDIS_BRANCH=3.0
52+
env: DRIVER=ruby REDIS_BRANCH=3.2 LOW_TIMEOUT=0.1
5553
- rvm: jruby-9.1.17.0
56-
env: DRIVER=synchrony REDIS_BRANCH=3.2
54+
env: DRIVER=ruby REDIS_BRANCH=4.0 LOW_TIMEOUT=0.1
5755

5856
notifications:
5957
irc:

test/cluster_client_options_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_client_accepts_valid_node_configs
3838

3939
def test_client_accepts_valid_options
4040
assert_nothing_raised do
41-
build_another_client(timeout: 1.0)
41+
build_another_client(timeout: TIMEOUT)
4242
end
4343
end
4444

test/cluster_client_transactions_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_transaction_with_replicas
4040
100.times { |i| cli.set("{key}#{i}", i) }
4141
end
4242

43-
sleep 0.1
43+
sleep 0.5
4444

4545
100.times { |i| assert_equal i.to_s, rc1.get("{key}#{i}") }
4646
100.times { |i| assert_equal i.to_s, rc2.get("{key}#{i}") }

test/helper.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
require_relative "support/connection/#{ENV["DRIVER"]}"
1616
require_relative 'support/cluster/orchestrator'
1717

18-
PORT = 6381
19-
OPTIONS = {:port => PORT, :db => 15, :timeout => Float(ENV["TIMEOUT"] || 0.1)}
20-
NODES = ["redis://127.0.0.1:#{PORT}/15"]
18+
PORT = 6381
19+
DB = 15
20+
TIMEOUT = Float(ENV['TIMEOUT'] || 0.1)
21+
LOW_TIMEOUT = Float(ENV['LOW_TIMEOUT'] || 0.01) # for blocking-command tests
22+
OPTIONS = { port: PORT, db: DB, timeout: TIMEOUT }.freeze
2123

2224
def driver(*drivers, &blk)
2325
if drivers.map(&:to_s).include?(ENV["DRIVER"])
@@ -178,9 +180,10 @@ def _new_client(options = {})
178180
end
179181

180182
module Distributed
181-
182183
include Generic
183184

185+
NODES = ["redis://127.0.0.1:#{PORT}/15"].freeze
186+
184187
def version
185188
Version.new(redis.info.first["redis_version"])
186189
end

test/lint/blocking_commands.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Lint
22
module BlockingCommands
3-
LOW_TIMEOUT = 0.01
4-
53
def setup
64
super
75

test/support/cluster/orchestrator.rb

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def rebuild
2121
replicate(@clients)
2222
save_config(@clients)
2323
wait_cluster_building(@clients)
24-
sleep 3
2524
end
2625

2726
def down
@@ -30,8 +29,9 @@ def down
3029
end
3130

3231
def failover
33-
take_slaves(@clients).last.cluster(:failover, :takeover)
34-
sleep 3
32+
master, slave = take_replication_pairs(@clients)
33+
slave.cluster(:failover, :takeover)
34+
wait_failover(to_node_key(master), to_node_key(slave), @clients)
3535
end
3636

3737
def start_resharding(slot, src_node_key, dest_node_key)
@@ -157,20 +157,44 @@ def save_config(clients)
157157
clients.each { |c| c.cluster(:saveconfig) }
158158
end
159159

160-
def wait_cluster_building(clients)
161-
first_cliient = clients.first
160+
def wait_cluster_building(clients, max_attempts: 200)
161+
attempt_count = 0
162162

163-
loop do
164-
info = hashify_cluster_info(first_cliient)
165-
break if info['cluster_state'] == 'ok'
166-
sleep 0.1
163+
clients.each do |client|
164+
loop do
165+
info = hashify_cluster_info(client)
166+
attempt_count += 1
167+
break if info['cluster_state'] == 'ok' || attempt_count > max_attempts
168+
sleep 0.1
169+
end
170+
end
171+
end
172+
173+
def wait_failover(master_key, slave_key, clients, max_attempts: 200)
174+
attempt_count = 0
175+
176+
clients.each do |client|
177+
loop do
178+
flags = hashify_cluster_node_flags(client)
179+
attempt_count += 1
180+
break if (flags[master_key] == 'slave' && flags[slave_key] == 'master') || attempt_count > max_attempts
181+
sleep 0.1
182+
end
167183
end
168184
end
169185

170186
def hashify_cluster_info(client)
171187
client.cluster(:info).split("\r\n").map { |str| str.split(':') }.to_h
172188
end
173189

190+
def hashify_cluster_node_flags(client)
191+
client.cluster(:nodes)
192+
.split("\n")
193+
.map { |str| str.split(' ') }
194+
.map { |arr| [arr[1].split('@').first, (arr[2].split(',') & %w[master slave]).first] }
195+
.to_h
196+
end
197+
174198
def hashify_node_map(client)
175199
client.cluster(:nodes)
176200
.split("\n")
@@ -191,10 +215,16 @@ def take_slaves(clients)
191215
clients[size..size * 2]
192216
end
193217

218+
def take_replication_pairs(clients)
219+
[take_masters(clients).last, take_slaves(clients).last]
220+
end
221+
194222
def find_client(clients, node_key)
195-
clients.find do |cli|
196-
con = cli.connection
197-
node_key == "#{con.fetch(:host)}:#{con.fetch(:port)}"
198-
end
223+
clients.find { |cli| node_key == to_node_key(cli) }
224+
end
225+
226+
def to_node_key(client)
227+
con = client.connection
228+
"#{con.fetch(:host)}:#{con.fetch(:port)}"
199229
end
200230
end

0 commit comments

Comments
 (0)