Skip to content

Commit 8da557c

Browse files
committed
Refactored buggy Semaphore test.
1 parent f50c7e9 commit 8da557c

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

spec/concurrent/atomic/semaphore_spec.rb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,34 @@ def subject.simulate_spurious_wake_up
116116
end
117117

118118
it 'should resist to spurious wake ups without timeout' do
119-
@expected = true
120-
# would set @expected to false
121-
Thread.new { @expected = subject.acquire }
119+
actual = Concurrent::AtomicBoolean.new(true)
120+
latch = Concurrent::CountDownLatch.new
122121

123-
sleep(0.1)
122+
# would set actual to false
123+
t = Thread.new { latch.wait(1); actual.value = subject.acquire }
124+
125+
latch.count_down
124126
subject.simulate_spurious_wake_up
127+
t.join(0.1)
125128

126-
sleep(0.1)
127-
expect(@expected).to be_truthy
129+
expect(actual.value).to be true
130+
t.kill
128131
end
129132

130133
it 'should resist to spurious wake ups with timeout' do
131-
@expected = true
132-
# sets @expected to false in another thread
133-
t = Thread.new { @expected = subject.try_acquire(1, 0.3) }
134+
actual = Concurrent::AtomicBoolean.new(true)
135+
latch = Concurrent::CountDownLatch.new
134136

135-
sleep(0.1)
136-
subject.simulate_spurious_wake_up
137+
# sets actual to false in another thread
138+
t = Thread.new { latch.wait(1); actual.value = subject.try_acquire(1, 0.3) }
137139

138-
sleep(0.1)
139-
expect(@expected).to be_truthy
140+
latch.count_down
141+
subject.simulate_spurious_wake_up
142+
t.join(0.1)
140143

144+
expect(actual.value).to be true
141145
t.join
142-
expect(@expected).to be_falsey
146+
expect(actual.value).to be false
143147
end
144148
end
145149
end

0 commit comments

Comments
 (0)