Skip to content

Commit 3e0a18d

Browse files
committed
Refactored specs for CountDownLatch into behaves_as
1 parent ddaecea commit 3e0a18d

File tree

1 file changed

+87
-83
lines changed

1 file changed

+87
-83
lines changed
Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,129 @@
11
require 'spec_helper'
22

3-
module Concurrent
3+
share_examples_for :count_down_latch do
44

5-
describe CountDownLatch do
5+
let(:latch) { described_class.new(3) }
6+
let(:zero_count_latch) { described_class.new(0) }
67

7-
let(:latch) { CountDownLatch.new(3) }
8-
let(:zero_count_latch) { CountDownLatch.new(0) }
8+
context '#initialize' do
99

10-
context '#initialize' do
10+
it 'raises an exception if the initial count is less than zero' do
11+
expect {
12+
described_class.new(-1)
13+
}.to raise_error(ArgumentError)
14+
end
1115

12-
it 'raises an exception if the initial count is less than zero' do
13-
expect {
14-
CountDownLatch.new(-1)
15-
}.to raise_error(ArgumentError)
16-
end
16+
it 'raises an exception if the initial count is not an integer' do
17+
expect {
18+
described_class.new('foo')
19+
}.to raise_error(ArgumentError)
20+
end
21+
end
1722

18-
it 'raises an exception if the initial count is not an integer' do
19-
expect {
20-
CountDownLatch.new('foo')
21-
}.to raise_error(ArgumentError)
22-
end
23+
describe '#count' do
24+
25+
it 'should be the value passed to the constructor' do
26+
latch.count.should eq 3
27+
end
28+
29+
it 'should be decreased after every count down' do
30+
latch.count_down
31+
latch.count.should eq 2
2332
end
2433

25-
describe '#count' do
34+
it 'should not go below zero' do
35+
5.times { latch.count_down }
36+
latch.count.should eq 0
37+
end
38+
end
2639

27-
it 'should be the value passed to the constructor' do
28-
latch.count.should eq 3
29-
end
40+
describe '#wait' do
3041

31-
it 'should be decreased after every count down' do
32-
latch.count_down
33-
latch.count.should eq 2
42+
context 'count set to zero' do
43+
it 'should return true immediately' do
44+
result = zero_count_latch.wait
45+
result.should be_true
3446
end
3547

36-
it 'should not go below zero' do
37-
5.times { latch.count_down }
38-
latch.count.should eq 0
48+
it 'should return true immediately with timeout' do
49+
result = zero_count_latch.wait(5)
50+
result.should be_true
3951
end
4052
end
4153

42-
describe '#wait' do
54+
context 'non zero count' do
4355

44-
context 'count set to zero' do
45-
it 'should return true immediately' do
46-
result = zero_count_latch.wait
47-
result.should be_true
56+
it 'should block thread until counter is set to zero' do
57+
3.times do
58+
Thread.new { sleep(0.1); latch.count_down }
4859
end
4960

50-
it 'should return true immediately with timeout' do
51-
result = zero_count_latch.wait(5)
52-
result.should be_true
53-
end
61+
result = latch.wait
62+
result.should be_true
63+
latch.count.should eq 0
5464
end
5565

56-
context 'non zero count' do
57-
58-
it 'should block thread until counter is set to zero' do
59-
3.times do
60-
Thread.new { sleep(0.1); latch.count_down }
61-
end
62-
63-
result = latch.wait
64-
result.should be_true
65-
latch.count.should eq 0
66+
it 'should block until counter is set to zero with timeout' do
67+
3.times do
68+
Thread.new { sleep(0.1); latch.count_down }
6669
end
6770

68-
it 'should block until counter is set to zero with timeout' do
69-
3.times do
70-
Thread.new { sleep(0.1); latch.count_down }
71-
end
72-
73-
result = latch.wait(1)
74-
result.should be_true
75-
latch.count.should eq 0
71+
result = latch.wait(1)
72+
result.should be_true
73+
latch.count.should eq 0
7674

77-
end
75+
end
7876

79-
it 'should block until timeout and return false when counter is not set to zero' do
80-
result = latch.wait(0.1)
81-
result.should be_false
82-
latch.count.should eq 3
83-
end
77+
it 'should block until timeout and return false when counter is not set to zero' do
78+
result = latch.wait(0.1)
79+
result.should be_false
80+
latch.count.should eq 3
8481
end
8582
end
83+
end
8684

87-
context 'spurious wake ups' do
85+
context 'spurious wake ups' do
8886

89-
before(:each) do
90-
def latch.simulate_spurious_wake_up
91-
@mutex.synchronize do
92-
@condition.signal
93-
@condition.broadcast
94-
end
87+
before(:each) do
88+
def latch.simulate_spurious_wake_up
89+
@mutex.synchronize do
90+
@condition.signal
91+
@condition.broadcast
9592
end
9693
end
94+
end
9795

98-
it 'should resist to spurious wake ups without timeout' do
99-
@expected = false
100-
Thread.new { latch.wait; @expected = true }
96+
it 'should resist to spurious wake ups without timeout' do
97+
@expected = false
98+
Thread.new { latch.wait; @expected = true }
10199

102-
sleep(0.1)
103-
latch.simulate_spurious_wake_up
100+
sleep(0.1)
101+
latch.simulate_spurious_wake_up
104102

105-
sleep(0.1)
106-
@expected.should be_false
107-
end
103+
sleep(0.1)
104+
@expected.should be_false
105+
end
108106

109-
it 'should resist to spurious wake ups with timeout' do
110-
@expected = false
111-
Thread.new { latch.wait(0.5); @expected = true }
107+
it 'should resist to spurious wake ups with timeout' do
108+
@expected = false
109+
Thread.new { latch.wait(0.5); @expected = true }
112110

113-
sleep(0.1)
114-
latch.simulate_spurious_wake_up
111+
sleep(0.1)
112+
latch.simulate_spurious_wake_up
115113

116-
sleep(0.1)
117-
@expected.should be_false
114+
sleep(0.1)
115+
@expected.should be_false
118116

119-
sleep(0.4)
120-
@expected.should be_true
121-
end
117+
sleep(0.4)
118+
@expected.should be_true
122119
end
120+
end
121+
end
122+
123+
module Concurrent
124+
125+
describe CountDownLatch do
123126

127+
it_should_behave_like :count_down_latch
124128
end
125129
end

0 commit comments

Comments
 (0)