Skip to content

Commit caf1814

Browse files
committed
Refactored buggy tests.
1 parent ff35d27 commit caf1814

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

spec/concurrent/atomic/cyclic_barrier_spec.rb

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ module Concurrent
5757

5858
describe 'reset' do
5959
it 'should release all waiting threads' do
60-
latch = CountDownLatch.new(1)
60+
start_latch = CountDownLatch.new(1)
61+
continue_latch = CountDownLatch.new(1)
6162

62-
Thread.new { barrier.wait; latch.count_down }
63-
sleep(0.1)
63+
Thread.new do
64+
start_latch.count_down
65+
barrier.wait
66+
continue_latch.count_down
67+
end
68+
69+
start_latch.wait(1)
6470
barrier.reset
65-
expect(latch.wait(0.1)).to be_truthy
71+
expect(continue_latch.wait(1)).to be_truthy
6672

6773
expect(barrier).not_to be_broken
6874
expect(barrier.number_waiting).to eq 0
@@ -73,7 +79,7 @@ module Concurrent
7379
context 'without timeout' do
7480
it 'should block the thread' do
7581
t = Thread.new { barrier.wait }
76-
sleep(0.1)
82+
t.join(0.1)
7783

7884
expect(t.status).to eq 'sleep'
7985
end
@@ -82,7 +88,7 @@ module Concurrent
8288
latch = CountDownLatch.new(parties)
8389

8490
parties.times { Thread.new { barrier.wait; latch.count_down } }
85-
expect(latch.wait(0.1)).to be_truthy
91+
expect(latch.wait(1)).to be_truthy
8692
expect(barrier.number_waiting).to eq 0
8793
expect(barrier).not_to be_broken
8894
end
@@ -91,7 +97,7 @@ module Concurrent
9197
latch = CountDownLatch.new(parties)
9298

9399
parties.times { Thread.new { latch.count_down if barrier.wait == true } }
94-
expect(latch.wait(0.1)).to be_truthy
100+
expect(latch.wait(1)).to be_truthy
95101
end
96102

97103
it 'executes the block once' do
@@ -101,36 +107,36 @@ module Concurrent
101107
latch = CountDownLatch.new(parties)
102108

103109
parties.times { Thread.new { latch.count_down if barrier.wait == true } }
104-
expect(latch.wait(0.1)).to be_truthy
110+
expect(latch.wait(1)).to be_truthy
105111

106112
expect(counter.value).to eq 1
107113
end
108114

109115
it 'can be reused' do
110116
first_latch = CountDownLatch.new(parties)
111117
parties.times { Thread.new { barrier.wait; first_latch.count_down } }
112-
expect(first_latch.wait(0.1)).to be_truthy
118+
expect(first_latch.wait(1)).to be_truthy
113119

114120
latch = CountDownLatch.new(parties)
115121
parties.times { Thread.new { barrier.wait; latch.count_down } }
116-
expect(latch.wait(0.1)).to be_truthy
122+
expect(latch.wait(1)).to be_truthy
117123
end
118124

119125
it 'return false if barrier has been reset' do
120126
latch = CountDownLatch.new(1)
121127

122-
Thread.new { latch.count_down if barrier.wait == false }
123-
sleep(0.1)
128+
t = Thread.new { latch.count_down if barrier.wait == false }
129+
t.join(0.1)
124130
barrier.reset
125-
expect(latch.wait(0.1)).to be_truthy
131+
expect(latch.wait(1)).to be_truthy
126132
end
127133
end
128134

129135
context 'with timeout' do
130136
context 'timeout not expiring' do
131137
it 'should block the thread' do
132138
t = Thread.new { barrier.wait(1) }
133-
sleep(0.1)
139+
t.join(0.1)
134140

135141
expect(t.status).to eq 'sleep'
136142
end
@@ -147,7 +153,7 @@ module Concurrent
147153
latch = CountDownLatch.new(parties)
148154

149155
parties.times { Thread.new { latch.count_down if barrier.wait(1) == true } }
150-
expect(latch.wait(0.1)).to be_truthy
156+
expect(latch.wait(1)).to be_truthy
151157
end
152158
end
153159

@@ -157,7 +163,7 @@ module Concurrent
157163
latch = CountDownLatch.new(1)
158164

159165
Thread.new { latch.count_down if barrier.wait(0.1) == false }
160-
expect(latch.wait(0.2)).to be_truthy
166+
expect(latch.wait(1)).to be_truthy
161167
end
162168

163169
it 'breaks the barrier and release all other threads' do
@@ -166,7 +172,7 @@ module Concurrent
166172
Thread.new { barrier.wait(0.1); latch.count_down }
167173
Thread.new { barrier.wait; latch.count_down }
168174

169-
expect(latch.wait(0.2)).to be_truthy
175+
expect(latch.wait(1)).to be_truthy
170176
expect(barrier).to be_broken
171177
end
172178

@@ -192,17 +198,17 @@ module Concurrent
192198

193199
context '#broken barrier' do
194200
it 'should not accept new threads' do
195-
Thread.new { barrier.wait(0.1) }
196-
sleep(0.2)
201+
t = Thread.new { barrier.wait(0.1) }
202+
t.join(0.2)
197203

198204
expect(barrier).to be_broken
199205

200206
expect(barrier.wait).to be_falsey
201207
end
202208

203209
it 'can be reset' do
204-
Thread.new { barrier.wait(0.1) }
205-
sleep(0.2)
210+
t = Thread.new { barrier.wait(0.1) }
211+
t.join(0.2)
206212

207213
expect(barrier).to be_broken
208214

@@ -226,23 +232,23 @@ def barrier.simulate_spurious_wake_up
226232

227233
it 'should resist to spurious wake ups without timeout' do
228234
@expected = false
229-
Thread.new { barrier.wait; @expected = true }
235+
t = Thread.new { barrier.wait; @expected = true }
236+
t.join(0.1)
230237

231-
sleep(0.1)
232238
barrier.simulate_spurious_wake_up
233239

234-
sleep(0.1)
240+
t.join(0.1)
235241
expect(@expected).to be_falsey
236242
end
237243

238244
it 'should resist to spurious wake ups with timeout' do
239245
@expected = false
240-
Thread.new { barrier.wait(0.5); @expected = true }
246+
t = Thread.new { barrier.wait(0.5); @expected = true }
241247

242-
sleep(0.1)
248+
t.join(0.1)
243249
barrier.simulate_spurious_wake_up
244250

245-
sleep(0.1)
251+
t.join(0.1)
246252
expect(@expected).to be_falsey
247253

248254
end

spec/concurrent/channel/buffered_channel_spec.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Channel
2424
channel.push 2
2525

2626
t = Thread.new { channel.push 3 }
27-
sleep(0.05)
27+
t.join(0.1)
2828
expect(t.status).to eq 'sleep'
2929
end
3030

@@ -34,13 +34,11 @@ module Channel
3434

3535
result = nil
3636

37-
Thread.new { channel.push 'bar'; result = 42 }
38-
39-
sleep(0.1)
37+
t = Thread.new { channel.push 'bar'; result = 42 }
4038

39+
t.join(0.1)
4140
channel.pop
42-
43-
sleep(0.1)
41+
t.join(0.1)
4442

4543
expect(result).to eq 42
4644
end
@@ -55,7 +53,7 @@ module Channel
5553
describe '#pop' do
5654
it 'should block if buffer is empty' do
5755
t = Thread.new { channel.pop }
58-
sleep(0.05)
56+
t.join(0.1)
5957
expect(t.status).to eq 'sleep'
6058
end
6159

@@ -81,9 +79,7 @@ module Channel
8179

8280
it 'does not block' do
8381
t = Thread.new { channel.select(probe) }
84-
85-
sleep(0.05)
86-
82+
t.join(0.1)
8783
expect(t.status).to eq false
8884
end
8985

spec/concurrent/scheduled_task_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ def trigger_observable(observable)
157157

158158
it 'uses the :executor from the options' do
159159
latch = Concurrent::CountDownLatch.new
160-
executor = Concurrent::SingleThreadExecutor.new
160+
executor = Concurrent::ImmediateExecutor.new
161161
expect(executor).to receive(:post).once.with(any_args).and_call_original
162-
task = ScheduledTask.execute(0.1, executor: executor) do
162+
task = ScheduledTask.execute(0, executor: executor) do
163163
latch.count_down
164164
end
165165
latch.wait(2)

0 commit comments

Comments
 (0)