@@ -50,8 +50,15 @@ module Concurrent
50
50
end
51
51
52
52
describe '#broken?' do
53
- it 'should not be broken when created'
54
- it 'should not be broken when reset is called without waiting thread'
53
+ it 'should not be broken when created' do
54
+ barrier . broken? . should eq false
55
+ end
56
+
57
+ it 'should not be broken when reset is called without waiting thread' do
58
+ barrier . reset
59
+ barrier . broken? . should eq false
60
+ end
61
+
55
62
it 'should be broken when at least one thread timed out'
56
63
it 'should be restored when reset is called'
57
64
end
@@ -74,18 +81,61 @@ module Concurrent
74
81
latch = CountDownLatch . new ( parties )
75
82
76
83
parties . times { Thread . new { barrier . wait ; latch . count_down } }
77
- latch . wait ( 0.2 ) . should be_true
84
+ latch . wait ( 0.1 ) . should be_true
78
85
barrier . number_waiting . should eq 0
79
86
end
80
87
81
- it 'executes the block'
88
+ it 'returns true when released' do
89
+ latch = CountDownLatch . new ( parties )
90
+
91
+ parties . times { Thread . new { latch . count_down if barrier . wait == true } }
92
+ latch . wait ( 0.1 ) . should be_true
93
+ end
94
+
95
+ it 'executes the block once' do
96
+ counter = AtomicFixnum . new
97
+ barrier = described_class . new ( parties ) { counter . increment }
98
+
99
+ latch = CountDownLatch . new ( parties )
100
+
101
+ parties . times { Thread . new { latch . count_down if barrier . wait == true } }
102
+ latch . wait ( 0.1 ) . should be_true
103
+
104
+ counter . value . should eq 1
105
+ end
82
106
end
83
107
84
108
context 'with timeout' do
85
- it 'should block the thread'
86
- it 'should release all threads when their number matches the desired one'
87
- it 'can return early and break the barrier'
88
- it 'does not execute the block on timeout'
109
+ context 'timeout not expiring' do
110
+ it 'should block the thread' do
111
+ t = Thread . new { barrier . wait ( 1 ) }
112
+ sleep ( 0.1 )
113
+
114
+ t . status . should eq 'sleep'
115
+ end
116
+
117
+ it 'should release all threads when their number matches the desired one' do
118
+ latch = CountDownLatch . new ( parties )
119
+
120
+ parties . times { Thread . new { barrier . wait ( 1 ) ; latch . count_down } }
121
+ latch . wait ( 0.2 ) . should be_true
122
+ barrier . number_waiting . should eq 0
123
+ end
124
+
125
+ it 'returns true when released' do
126
+ latch = CountDownLatch . new ( parties )
127
+
128
+ parties . times { Thread . new { latch . count_down if barrier . wait ( 1 ) == true } }
129
+ latch . wait ( 0.1 ) . should be_true
130
+ end
131
+ end
132
+
133
+ context 'timeout expiring' do
134
+
135
+ it 'returns false'
136
+ it 'can return early and break the barrier'
137
+ it 'does not execute the block on timeout'
138
+ end
89
139
end
90
140
end
91
141
0 commit comments