|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 |
| -module Concurrent |
| 3 | +share_examples_for :count_down_latch do |
4 | 4 |
|
5 |
| - describe CountDownLatch do |
| 5 | + let(:latch) { described_class.new(3) } |
| 6 | + let(:zero_count_latch) { described_class.new(0) } |
6 | 7 |
|
7 |
| - let(:latch) { CountDownLatch.new(3) } |
8 |
| - let(:zero_count_latch) { CountDownLatch.new(0) } |
| 8 | + context '#initialize' do |
9 | 9 |
|
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 |
11 | 15 |
|
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 |
17 | 22 |
|
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 |
23 | 32 | end
|
24 | 33 |
|
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 |
26 | 39 |
|
27 |
| - it 'should be the value passed to the constructor' do |
28 |
| - latch.count.should eq 3 |
29 |
| - end |
| 40 | + describe '#wait' do |
30 | 41 |
|
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 |
34 | 46 | end
|
35 | 47 |
|
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 |
39 | 51 | end
|
40 | 52 | end
|
41 | 53 |
|
42 |
| - describe '#wait' do |
| 54 | + context 'non zero count' do |
43 | 55 |
|
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 } |
48 | 59 | end
|
49 | 60 |
|
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 |
54 | 64 | end
|
55 | 65 |
|
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 } |
66 | 69 | end
|
67 | 70 |
|
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 |
76 | 74 |
|
77 |
| - end |
| 75 | + end |
78 | 76 |
|
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 |
84 | 81 | end
|
85 | 82 | end
|
| 83 | + end |
86 | 84 |
|
87 |
| - context 'spurious wake ups' do |
| 85 | + context 'spurious wake ups' do |
88 | 86 |
|
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 |
95 | 92 | end
|
96 | 93 | end
|
| 94 | + end |
97 | 95 |
|
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 } |
101 | 99 |
|
102 |
| - sleep(0.1) |
103 |
| - latch.simulate_spurious_wake_up |
| 100 | + sleep(0.1) |
| 101 | + latch.simulate_spurious_wake_up |
104 | 102 |
|
105 |
| - sleep(0.1) |
106 |
| - @expected.should be_false |
107 |
| - end |
| 103 | + sleep(0.1) |
| 104 | + @expected.should be_false |
| 105 | + end |
108 | 106 |
|
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 } |
112 | 110 |
|
113 |
| - sleep(0.1) |
114 |
| - latch.simulate_spurious_wake_up |
| 111 | + sleep(0.1) |
| 112 | + latch.simulate_spurious_wake_up |
115 | 113 |
|
116 |
| - sleep(0.1) |
117 |
| - @expected.should be_false |
| 114 | + sleep(0.1) |
| 115 | + @expected.should be_false |
118 | 116 |
|
119 |
| - sleep(0.4) |
120 |
| - @expected.should be_true |
121 |
| - end |
| 117 | + sleep(0.4) |
| 118 | + @expected.should be_true |
122 | 119 | end
|
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +module Concurrent |
| 124 | + |
| 125 | + describe CountDownLatch do |
123 | 126 |
|
| 127 | + it_should_behave_like :count_down_latch |
124 | 128 | end
|
125 | 129 | end
|
0 commit comments