Skip to content

Commit e7daabf

Browse files
committed
Improve tests
1 parent 0ac67e6 commit e7daabf

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

spec/concurrent/atomic/condition_spec.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,14 @@ module Concurrent
270270

271271
describe '#broadcast' do
272272
it 'wakes up all threads' do
273-
latch = CountDownLatch.new(2)
274273
mutex = Mutex.new
275274

276-
t1 = Thread.new { mutex.synchronize { subject.wait(mutex); latch.count_down } }
277-
t2 = Thread.new { mutex.synchronize { subject.wait(mutex); latch.count_down } }
278-
279-
sleep(0.1)
275+
t1 = Thread.new { mutex.synchronize { subject.wait(mutex)} }
276+
t2 = Thread.new { mutex.synchronize { subject.wait(mutex)} }
280277
mutex.synchronize { subject.broadcast }
281-
sleep(0.2)
282-
283-
expect(latch.count).to eq 0
284-
[t1, t2].each { |t| t.kill }
278+
[t1, t2].each do |t|
279+
expect(t.join(1)).to eq t # TODO returns sometimes nil
280+
end
285281
end
286282
end
287283
end

spec/concurrent/atomic/count_down_latch_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ module Concurrent
9999
before(:each) do
100100
def subject.simulate_spurious_wake_up
101101
synchronize do
102-
signal
103-
broadcast
102+
ns_signal
103+
ns_broadcast
104104
end
105105
end
106106
end

spec/concurrent/atomic/semaphore_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
context 'not enough permits available' do
2121
it 'should block thread until permits are available' do
2222
semaphore.drain_permits
23-
Thread.new { sleep(0.2) && semaphore.release }
23+
Thread.new { sleep(0.2); semaphore.release }
2424

2525
result = semaphore.acquire
2626
expect(result).to be_nil
@@ -64,7 +64,7 @@
6464

6565
it 'acquires when permits are available within timeout' do
6666
semaphore.drain_permits
67-
Thread.new { sleep 0.1 && semaphore.release }
67+
Thread.new { sleep 0.1; semaphore.release }
6868
result = semaphore.try_acquire(1, 1)
6969
expect(result).to be_truthy
7070
end
@@ -112,15 +112,16 @@ def subject.simulate_spurious_wake_up
112112
@condition.broadcast
113113
end
114114
end
115+
115116
subject.drain_permits
116117
end
117118

118119
it 'should resist to spurious wake ups without timeout' do
119120
actual = Concurrent::AtomicBoolean.new(true)
120-
latch = Concurrent::CountDownLatch.new
121+
latch = Concurrent::CountDownLatch.new
121122

122123
# would set actual to false
123-
t = Thread.new { latch.wait(1); actual.value = subject.acquire }
124+
t = Thread.new { latch.wait(1); actual.value = subject.acquire }
124125

125126
latch.count_down
126127
subject.simulate_spurious_wake_up
@@ -132,10 +133,10 @@ def subject.simulate_spurious_wake_up
132133

133134
it 'should resist to spurious wake ups with timeout' do
134135
actual = Concurrent::AtomicBoolean.new(true)
135-
latch = Concurrent::CountDownLatch.new
136+
latch = Concurrent::CountDownLatch.new
136137

137138
# sets actual to false in another thread
138-
t = Thread.new { latch.wait(1); actual.value = subject.try_acquire(1, 0.3) }
139+
t = Thread.new { latch.wait(1); actual.value = subject.try_acquire(1, 0.3) }
139140

140141
latch.count_down
141142
subject.simulate_spurious_wake_up

spec/concurrent/executor/global_thread_pool_shared.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
it 'aliases #<<' do
2727
latch = Concurrent::CountDownLatch.new(1)
2828
subject << proc { latch.count_down }
29-
latch.wait(0.2)
30-
expect(latch.count).to eq 0
29+
expect(latch.wait(0.2)).to eq true
3130
end
3231
end
3332
end

0 commit comments

Comments
 (0)