Skip to content

Commit 4272db4

Browse files
committed
Improve tests
1 parent 0ac67e6 commit 4272db4

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

spec/concurrent/atomic/condition_spec.rb

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Concurrent
2929
it 'should block the thread' do
3030
latch_1 = Concurrent::CountDownLatch.new
3131
latch_2 = Concurrent::CountDownLatch.new
32-
mutex = Mutex.new
32+
mutex = Mutex.new
3333

3434
t = Thread.new do
3535
mutex.synchronize do
@@ -46,8 +46,8 @@ module Concurrent
4646
end
4747

4848
it 'should return a woken up result when is woken up by #signal' do
49-
result = nil
50-
mutex = Mutex.new
49+
result = nil
50+
mutex = Mutex.new
5151
latch_1 = Concurrent::CountDownLatch.new
5252
latch_2 = Concurrent::CountDownLatch.new
5353

@@ -74,8 +74,8 @@ module Concurrent
7474
end
7575

7676
it 'should return a woken up result when is woken up by #broadcast' do
77-
result = nil
78-
mutex = Mutex.new
77+
result = nil
78+
mutex = Mutex.new
7979
latch_1 = Concurrent::CountDownLatch.new
8080
latch_2 = Concurrent::CountDownLatch.new
8181

@@ -110,7 +110,7 @@ module Concurrent
110110
it 'should block the thread' do
111111
latch_1 = Concurrent::CountDownLatch.new
112112
latch_2 = Concurrent::CountDownLatch.new
113-
mutex = Mutex.new
113+
mutex = Mutex.new
114114

115115
t = Thread.new do
116116
mutex.synchronize do
@@ -127,8 +127,8 @@ module Concurrent
127127
end
128128

129129
it 'should return remaining time when is woken up by #signal' do
130-
result = nil
131-
mutex = Mutex.new
130+
result = nil
131+
mutex = Mutex.new
132132
latch_1 = Concurrent::CountDownLatch.new
133133
latch_2 = Concurrent::CountDownLatch.new
134134

@@ -156,8 +156,8 @@ module Concurrent
156156
end
157157

158158
it 'should return remaining time when is woken up by #broadcast' do
159-
result = nil
160-
mutex = Mutex.new
159+
result = nil
160+
mutex = Mutex.new
161161
latch_1 = Concurrent::CountDownLatch.new
162162
latch_2 = Concurrent::CountDownLatch.new
163163

@@ -186,8 +186,8 @@ module Concurrent
186186

187187
it 'should return 0 or negative number if timed out' do
188188
result = nil
189-
mutex = Mutex.new
190-
latch = Concurrent::CountDownLatch.new
189+
mutex = Mutex.new
190+
latch = Concurrent::CountDownLatch.new
191191

192192
t = Thread.new do
193193
mutex.synchronize do
@@ -214,7 +214,7 @@ module Concurrent
214214
describe '#wait' do
215215

216216
it 'should block threads' do
217-
mutex = Mutex.new
217+
mutex = Mutex.new
218218
latch_1 = Concurrent::CountDownLatch.new(2)
219219
latch_2 = Concurrent::CountDownLatch.new(2)
220220

@@ -240,7 +240,7 @@ module Concurrent
240240
it 'wakes up only one thread' do
241241
latch_1 = Concurrent::CountDownLatch.new(2)
242242
latch_2 = Concurrent::CountDownLatch.new(2)
243-
mutex = Mutex.new
243+
mutex = Mutex.new
244244

245245
t1 = Thread.new do
246246
mutex.synchronize do
@@ -270,18 +270,22 @@ module Concurrent
270270

271271
describe '#broadcast' do
272272
it 'wakes up all threads' do
273-
latch = CountDownLatch.new(2)
274-
mutex = Mutex.new
275-
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)
273+
mutex = Mutex.new
274+
go = CountDownLatch.new(2)
275+
threads = Array.new(2) do
276+
Thread.new do
277+
mutex.synchronize do
278+
go.count_down
279+
subject.wait(mutex)
280+
end
281+
end
282+
end
283+
go.wait
280284
mutex.synchronize { subject.broadcast }
281-
sleep(0.2)
282285

283-
expect(latch.count).to eq 0
284-
[t1, t2].each { |t| t.kill }
286+
threads.each do |t|
287+
expect(t.join(0.1)).to eq t
288+
end
285289
end
286290
end
287291
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)