Skip to content

Commit 334c283

Browse files
authored
test: fix a flaky test case (#389)
1 parent 58c09ee commit 334c283

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

test/test_against_cluster_down.rb

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
class TestAgainstClusterDown < TestingWrapper
66
WAIT_SEC = 0.1
7+
NUMBER_OF_JOBS = 5
78

89
def setup
910
@captured_commands = ::Middlewares::CommandCapture::CommandBuffer.new
1011
@redirect_count = ::Middlewares::RedirectCount::Counter.new
11-
@clients = Array.new(5) { build_client }
12+
@clients = Array.new(NUMBER_OF_JOBS) { build_client }
1213
@threads = []
1314
@controller = nil
1415
@cluster_down_counter = Counter.new
15-
@pubsub_recorder = Recorder.new
16+
@recorders = Array.new(NUMBER_OF_JOBS) { Recorder.new }
1617
@captured_commands.clear
1718
@redirect_count.clear
1819
end
@@ -27,11 +28,12 @@ def teardown
2728
end
2829

2930
def test_recoverability_from_cluster_down
30-
@threads << spawn_single(@clients[0])
31-
@threads << spawn_pipeline(@clients[1])
32-
@threads << spawn_transaction(@clients[2])
33-
@threads << spawn_subscriber(@clients[3])
34-
@threads << spawn_publisher(@clients[4])
31+
cases = %w[Single Pipeline Transaction Subscriber Publisher]
32+
@threads << spawn_single(@clients[0], @recorders[0])
33+
@threads << spawn_pipeline(@clients[1], @recorders[1])
34+
@threads << spawn_transaction(@clients[2], @recorders[2])
35+
@threads << spawn_subscriber(@clients[3], @recorders[3])
36+
@threads << spawn_publisher(@clients[4], @recorders[4])
3537
wait_for_jobs_to_be_stable
3638

3739
system('docker compose --progress quiet down', exception: true)
@@ -44,25 +46,12 @@ def test_recoverability_from_cluster_down
4446
refute(@cluster_down_counter.get.zero?, 'Case: cluster down count')
4547
refute(@captured_commands.count('cluster', 'nodes').zero?, 'Case: cluster nodes calls')
4648

47-
client = build_client(custom: nil, middlewares: nil)
48-
@clients << client
49-
50-
single_value1 = client.call('get', 'single', &:to_i)
51-
pipeline_value1 = client.call('get', 'pipeline', &:to_i)
52-
transaction_value1 = client.call('get', 'transaction', &:to_i)
53-
pubsub_message1 = @pubsub_recorder.get.to_i
54-
49+
@values_a = @recorders.map { |r| r.get.to_i }
5550
wait_for_jobs_to_be_stable
56-
57-
single_value2 = client.call('get', 'single', &:to_i)
58-
pipeline_value2 = client.call('get', 'pipeline', &:to_i)
59-
transaction_value2 = client.call('get', 'transaction', &:to_i)
60-
pubsub_message2 = @pubsub_recorder.get.to_i
61-
62-
assert(single_value1 < single_value2, "Single: #{single_value1} < #{single_value2}")
63-
assert(pipeline_value1 < pipeline_value2, "Pipeline: #{pipeline_value1} < #{pipeline_value2}")
64-
assert(transaction_value1 < transaction_value2, "Transaction: #{transaction_value1} < #{transaction_value2}")
65-
assert(pubsub_message1 < pubsub_message2, "PubSub: #{pubsub_message1} < #{pubsub_message2}")
51+
@values_b = @recorders.map { |r| r.get.to_i }
52+
@recorders.each_with_index do |_, i|
53+
assert(@values_a[i] < @values_b[i], "#{cases[i]}: #{@values_a[i]} < #{@values_b[i]}")
54+
end
6655
end
6756

6857
private
@@ -91,57 +80,63 @@ def build_controller
9180
)
9281
end
9382

94-
def spawn_single(cli)
95-
Thread.new(cli) do |r|
83+
def spawn_single(cli, rec)
84+
Thread.new(cli, rec) do |c, r|
9685
loop do
9786
handle_errors do
98-
r.call('incr', 'single')
99-
r.call('incr', 'single')
87+
c.call('incr', 'single')
88+
reply = c.call('incr', 'single')
89+
r.set(reply)
10090
end
10191
ensure
10292
sleep WAIT_SEC
10393
end
10494
end
10595
end
10696

107-
def spawn_pipeline(cli)
108-
Thread.new(cli) do |r|
97+
def spawn_pipeline(cli, rec)
98+
Thread.new(cli, rec) do |c, r|
10999
loop do
110100
handle_errors do
111-
r.pipelined do |pi|
101+
reply = c.pipelined do |pi|
112102
pi.call('incr', 'pipeline')
113103
pi.call('incr', 'pipeline')
114104
end
105+
106+
r.set(reply[1])
115107
end
116108
ensure
117109
sleep WAIT_SEC
118110
end
119111
end
120112
end
121113

122-
def spawn_transaction(cli)
123-
Thread.new(cli) do |r|
114+
def spawn_transaction(cli, rec)
115+
Thread.new(cli, rec) do |c, r|
124116
i = 0
125117
loop do
126118
handle_errors do
127-
r.multi(watch: i.odd? ? %w[transaction] : nil) do |tx|
119+
reply = c.multi(watch: i.odd? ? %w[transaction] : nil) do |tx|
128120
i += 1
129121
tx.call('incr', 'transaction')
130122
tx.call('incr', 'transaction')
131123
end
124+
125+
r.set(reply[1])
132126
end
133127
ensure
134128
sleep WAIT_SEC
135129
end
136130
end
137131
end
138132

139-
def spawn_publisher(cli)
140-
Thread.new(cli) do |r|
133+
def spawn_publisher(cli, rec)
134+
Thread.new(cli, rec) do |c, r|
141135
i = 0
142136
loop do
143137
handle_errors do
144-
r.call('spublish', 'chan', i)
138+
c.call('spublish', 'chan', i)
139+
r.set(i)
145140
i += 1
146141
end
147142
ensure
@@ -150,12 +145,12 @@ def spawn_publisher(cli)
150145
end
151146
end
152147

153-
def spawn_subscriber(cli)
154-
Thread.new(cli) do |r|
148+
def spawn_subscriber(cli, rec)
149+
Thread.new(cli, rec) do |c, r|
155150
ps = nil
156151

157152
loop do
158-
ps = r.pubsub
153+
ps = c.pubsub
159154
ps.call('ssubscribe', 'chan')
160155
break
161156
rescue StandardError
@@ -168,7 +163,7 @@ def spawn_subscriber(cli)
168163
handle_errors do
169164
event = ps.next_event(0.01)
170165
case event&.first
171-
when 'smessage' then @pubsub_recorder.set(event[2])
166+
when 'smessage' then r.set(event[2])
172167
end
173168
end
174169
ensure
@@ -197,16 +192,21 @@ def handle_errors
197192
end
198193

199194
def wait_for_jobs_to_be_stable(attempts: 100)
200-
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
195+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
196+
sleep_sec = WAIT_SEC * (@threads.size * 2)
201197

202-
loop do
203-
raise MaxRetryExceeded if attempts <= 0
198+
@recorders.each do |recorder|
199+
loop do
200+
raise MaxRetryExceeded if attempts <= 0
204201

205-
attempts -= 1
206-
before = @cluster_down_counter.get
207-
sleep WAIT_SEC * (@threads.size * 2)
208-
after = @cluster_down_counter.get
209-
break if before == after && @pubsub_recorder.updated?(now)
202+
attempts -= 1
203+
next sleep(sleep_sec) unless recorder.updated?(start)
204+
205+
value_a = recorder.get.to_i
206+
sleep sleep_sec
207+
value_b = recorder.get.to_i
208+
break if value_a < value_b
209+
end
210210
end
211211
end
212212

0 commit comments

Comments
 (0)