Skip to content

Commit fe91893

Browse files
authored
Merge pull request #853 from supercaracal/fix-unstable-test-cases
Fix unstable test cases for CI
2 parents 26d2979 + ad88254 commit fe91893

File tree

6 files changed

+79
-52
lines changed

6 files changed

+79
-52
lines changed

Rakefile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
require 'rake/testtask'
22
Rake::TestTask.new 'test' do |t|
3+
if ENV['SOCKET_PATH'].nil?
4+
sock_file = Dir.glob("#{__dir__}/**/redis.sock").first
5+
6+
if sock_file.nil?
7+
puts '`SOCKET_PATH` environment variable required'
8+
exit 1
9+
end
10+
11+
ENV['SOCKET_PATH'] = sock_file
12+
end
13+
314
t.libs = %w(lib test)
4-
t.pattern = "test/*_test.rb"
15+
16+
if ARGV.size == 1
17+
t.pattern = 'test/*_test.rb'
18+
else
19+
t.test_files = ARGV[1..-1]
20+
end
21+
22+
t.options = '-v'
523
end
624

725
task default: :test

bin/cluster_creater

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env ruby
2+
3+
# frozen_string_literal: true
4+
5+
require_relative '../test/support/cluster/orchestrator'
6+
7+
urls = ARGV.map { |host_port| "redis://#{host_port}" }
8+
orchestrator = ClusterOrchestrator.new(urls, timeout: 30.0)
9+
orchestrator.rebuild
10+
orchestrator.close

makefile

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,48 @@ CLUSTER_CONF_PATHS := $(addprefix ${TMP}/nodes,$(addsuffix .conf,${CLUSTER_PORTS
1919
CLUSTER_ADDRS := $(addprefix 127.0.0.1:,${CLUSTER_PORTS})
2020

2121
define kill-redis
22-
(ls $1 2> /dev/null && kill $$(cat $1) && rm -f $1) || true
22+
(ls $1 > /dev/null 2>&1 && kill $$(cat $1) && rm -f $1) || true
2323
endef
2424

2525
all:
26-
make start_all
27-
make test
28-
make stop_all
26+
@make --no-print-directory start_all
27+
@make --no-print-directory test
28+
@make --no-print-directory stop_all
2929

3030
start_all:
31-
make start
32-
make start_slave
33-
make start_sentinel
34-
make start_cluster
35-
make create_cluster
31+
@make --no-print-directory start
32+
@make --no-print-directory start_slave
33+
@make --no-print-directory start_sentinel
34+
@make --no-print-directory start_cluster
35+
@make --no-print-directory create_cluster
3636

3737
stop_all:
38-
make stop_sentinel
39-
make stop_slave
40-
make stop
41-
make stop_cluster
38+
@make --no-print-directory stop_sentinel
39+
@make --no-print-directory stop_slave
40+
@make --no-print-directory stop
41+
@make --no-print-directory stop_cluster
4242

4343
${TMP}:
44-
mkdir -p $@
44+
@mkdir -p $@
4545

4646
${BINARY}: ${TMP}
47-
bin/build ${REDIS_BRANCH} $<
47+
@bin/build ${REDIS_BRANCH} $<
4848

4949
test:
50-
env SOCKET_PATH=${SOCKET_PATH} rake test
50+
@env SOCKET_PATH=${SOCKET_PATH} bundle exec rake test
5151

5252
stop:
53-
$(call kill-redis,${PID_PATH})
53+
@$(call kill-redis,${PID_PATH})
5454

5555
start: ${BINARY}
56-
${BINARY} \
57-
--daemonize yes \
58-
--pidfile ${PID_PATH} \
59-
--port ${PORT} \
56+
@${BINARY}\
57+
--daemonize yes\
58+
--pidfile ${PID_PATH}\
59+
--port ${PORT}\
6060
--unixsocket ${SOCKET_PATH}
6161

6262
stop_slave:
63-
$(call kill-redis,${SLAVE_PID_PATH})
63+
@$(call kill-redis,${SLAVE_PID_PATH})
6464

6565
start_slave: ${BINARY}
6666
${BINARY}\
@@ -71,11 +71,11 @@ start_slave: ${BINARY}
7171
--slaveof 127.0.0.1 ${PORT}
7272

7373
stop_sentinel:
74-
$(call kill-redis,${SENTINEL_PID_PATHS})
75-
rm -f ${TMP}/sentinel*.conf || true
74+
@$(call kill-redis,${SENTINEL_PID_PATHS})
75+
@rm -f ${TMP}/sentinel*.conf || true
7676

7777
start_sentinel: ${BINARY}
78-
for port in ${SENTINEL_PORTS}; do\
78+
@for port in ${SENTINEL_PORTS}; do\
7979
conf=${TMP}/sentinel$$port.conf;\
8080
touch $$conf;\
8181
echo '' > $$conf;\
@@ -91,29 +91,28 @@ start_sentinel: ${BINARY}
9191
done
9292

9393
stop_cluster:
94-
$(call kill-redis,${CLUSTER_PID_PATHS})
95-
rm -f appendonly.aof || true
96-
rm -f ${CLUSTER_CONF_PATHS} || true
94+
@$(call kill-redis,${CLUSTER_PID_PATHS})
95+
@rm -f appendonly.aof || true
96+
@rm -f ${CLUSTER_CONF_PATHS} || true
9797

9898
start_cluster: ${BINARY}
99-
for port in ${CLUSTER_PORTS}; do \
100-
${BINARY} \
101-
--daemonize yes \
102-
--appendonly yes \
103-
--cluster-enabled yes \
104-
--cluster-config-file ${TMP}/nodes$$port.conf \
105-
--cluster-node-timeout 5000 \
106-
--pidfile ${TMP}/redis$$port.pid \
107-
--port $$port \
108-
--unixsocket ${TMP}/redis$$port.sock; \
99+
@for port in ${CLUSTER_PORTS}; do\
100+
${BINARY}\
101+
--daemonize yes\
102+
--appendonly yes\
103+
--cluster-enabled yes\
104+
--cluster-config-file ${TMP}/nodes$$port.conf\
105+
--cluster-node-timeout 5000\
106+
--pidfile ${TMP}/redis$$port.pid\
107+
--port $$port\
108+
--unixsocket ${TMP}/redis$$port.sock;\
109109
done
110110

111111
create_cluster:
112-
yes yes | ((${REDIS_CLIENT} --cluster create ${CLUSTER_ADDRS} --cluster-replicas 1) || \
113-
(bundle exec ruby ${REDIS_TRIB} create --replicas 1 ${CLUSTER_ADDRS}))
112+
@bin/cluster_creater ${CLUSTER_ADDRS}
114113

115114
clean:
116-
(test -d ${BUILD_DIR} && cd ${BUILD_DIR}/src && make clean distclean) || true
115+
@(test -d ${BUILD_DIR} && cd ${BUILD_DIR}/src && make clean distclean) || true
117116

118117
.PHONY: all test stop start stop_slave start_slave stop_sentinel start_sentinel\
119118
stop_cluster start_cluster create_cluster stop_all start_all clean

test/cluster_client_replicas_test.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ def test_client_can_command_with_replica
1313
assert_equal 'OK', r.set("key#{i}", i)
1414
end
1515

16-
begin
17-
r.wait(6, 5_000)
18-
rescue Redis::TimeoutError
19-
end
16+
r.wait(1, TIMEOUT.to_i * 1000)
2017

2118
100.times do |i|
2219
assert_equal i.to_s, r.get("key#{i}")
@@ -35,10 +32,7 @@ def test_some_reference_commands_are_sent_to_slaves_if_needed
3532

3633
5.times { |i| r.set("key#{i}", i) }
3734

38-
begin
39-
r.wait(6, 5_000)
40-
rescue Redis::TimeoutError
41-
end
35+
r.wait(1, TIMEOUT.to_i * 1000)
4236

4337
assert_equal %w[key0 key1 key2 key3 key4], r.keys
4438
assert_equal 5, r.dbsize

test/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
PORT = 6381
2121
DB = 15
22-
TIMEOUT = Float(ENV['TIMEOUT'] || 0.1)
22+
TIMEOUT = Float(ENV['TIMEOUT'] || 1.0)
2323
LOW_TIMEOUT = Float(ENV['LOW_TIMEOUT'] || 0.01) # for blocking-command tests
2424
OPTIONS = { port: PORT, db: DB, timeout: TIMEOUT }.freeze
2525

test/support/cluster/orchestrator.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ class ClusterOrchestrator
77

88
def initialize(node_addrs, timeout: 30.0)
99
raise 'Redis Cluster requires at least 3 master nodes.' if node_addrs.size < 3
10-
@clients = node_addrs.map { |addr| Redis.new(url: addr, timeout: timeout) }
10+
@clients = node_addrs.map do |addr|
11+
Redis.new(url: addr,
12+
timeout: timeout,
13+
reconnect_attempts: 10,
14+
reconnect_delay: 1.5,
15+
reconnect_delay_max: 10.0)
16+
end
1117
@timeout = timeout
1218
end
1319

0 commit comments

Comments
 (0)