@@ -57,12 +57,18 @@ module Concurrent
57
57
58
58
describe 'reset' do
59
59
it 'should release all waiting threads' do
60
- latch = CountDownLatch . new ( 1 )
60
+ start_latch = CountDownLatch . new ( 1 )
61
+ continue_latch = CountDownLatch . new ( 1 )
61
62
62
- Thread . new { barrier . wait ; latch . count_down }
63
- sleep ( 0.1 )
63
+ Thread . new do
64
+ start_latch . count_down
65
+ barrier . wait
66
+ continue_latch . count_down
67
+ end
68
+
69
+ start_latch . wait ( 1 )
64
70
barrier . reset
65
- expect ( latch . wait ( 0. 1) ) . to be_truthy
71
+ expect ( continue_latch . wait ( 1 ) ) . to be_truthy
66
72
67
73
expect ( barrier ) . not_to be_broken
68
74
expect ( barrier . number_waiting ) . to eq 0
@@ -73,7 +79,7 @@ module Concurrent
73
79
context 'without timeout' do
74
80
it 'should block the thread' do
75
81
t = Thread . new { barrier . wait }
76
- sleep ( 0.1 )
82
+ t . join ( 0.1 )
77
83
78
84
expect ( t . status ) . to eq 'sleep'
79
85
end
@@ -82,7 +88,7 @@ module Concurrent
82
88
latch = CountDownLatch . new ( parties )
83
89
84
90
parties . times { Thread . new { barrier . wait ; latch . count_down } }
85
- expect ( latch . wait ( 0. 1) ) . to be_truthy
91
+ expect ( latch . wait ( 1 ) ) . to be_truthy
86
92
expect ( barrier . number_waiting ) . to eq 0
87
93
expect ( barrier ) . not_to be_broken
88
94
end
@@ -91,7 +97,7 @@ module Concurrent
91
97
latch = CountDownLatch . new ( parties )
92
98
93
99
parties . times { Thread . new { latch . count_down if barrier . wait == true } }
94
- expect ( latch . wait ( 0. 1) ) . to be_truthy
100
+ expect ( latch . wait ( 1 ) ) . to be_truthy
95
101
end
96
102
97
103
it 'executes the block once' do
@@ -101,36 +107,36 @@ module Concurrent
101
107
latch = CountDownLatch . new ( parties )
102
108
103
109
parties . times { Thread . new { latch . count_down if barrier . wait == true } }
104
- expect ( latch . wait ( 0. 1) ) . to be_truthy
110
+ expect ( latch . wait ( 1 ) ) . to be_truthy
105
111
106
112
expect ( counter . value ) . to eq 1
107
113
end
108
114
109
115
it 'can be reused' do
110
116
first_latch = CountDownLatch . new ( parties )
111
117
parties . times { Thread . new { barrier . wait ; first_latch . count_down } }
112
- expect ( first_latch . wait ( 0. 1) ) . to be_truthy
118
+ expect ( first_latch . wait ( 1 ) ) . to be_truthy
113
119
114
120
latch = CountDownLatch . new ( parties )
115
121
parties . times { Thread . new { barrier . wait ; latch . count_down } }
116
- expect ( latch . wait ( 0. 1) ) . to be_truthy
122
+ expect ( latch . wait ( 1 ) ) . to be_truthy
117
123
end
118
124
119
125
it 'return false if barrier has been reset' do
120
126
latch = CountDownLatch . new ( 1 )
121
127
122
- Thread . new { latch . count_down if barrier . wait == false }
123
- sleep ( 0.1 )
128
+ t = Thread . new { latch . count_down if barrier . wait == false }
129
+ t . join ( 0.1 )
124
130
barrier . reset
125
- expect ( latch . wait ( 0. 1) ) . to be_truthy
131
+ expect ( latch . wait ( 1 ) ) . to be_truthy
126
132
end
127
133
end
128
134
129
135
context 'with timeout' do
130
136
context 'timeout not expiring' do
131
137
it 'should block the thread' do
132
138
t = Thread . new { barrier . wait ( 1 ) }
133
- sleep ( 0.1 )
139
+ t . join ( 0.1 )
134
140
135
141
expect ( t . status ) . to eq 'sleep'
136
142
end
@@ -147,7 +153,7 @@ module Concurrent
147
153
latch = CountDownLatch . new ( parties )
148
154
149
155
parties . times { Thread . new { latch . count_down if barrier . wait ( 1 ) == true } }
150
- expect ( latch . wait ( 0. 1) ) . to be_truthy
156
+ expect ( latch . wait ( 1 ) ) . to be_truthy
151
157
end
152
158
end
153
159
@@ -157,7 +163,7 @@ module Concurrent
157
163
latch = CountDownLatch . new ( 1 )
158
164
159
165
Thread . new { latch . count_down if barrier . wait ( 0.1 ) == false }
160
- expect ( latch . wait ( 0.2 ) ) . to be_truthy
166
+ expect ( latch . wait ( 1 ) ) . to be_truthy
161
167
end
162
168
163
169
it 'breaks the barrier and release all other threads' do
@@ -166,7 +172,7 @@ module Concurrent
166
172
Thread . new { barrier . wait ( 0.1 ) ; latch . count_down }
167
173
Thread . new { barrier . wait ; latch . count_down }
168
174
169
- expect ( latch . wait ( 0.2 ) ) . to be_truthy
175
+ expect ( latch . wait ( 1 ) ) . to be_truthy
170
176
expect ( barrier ) . to be_broken
171
177
end
172
178
@@ -192,17 +198,17 @@ module Concurrent
192
198
193
199
context '#broken barrier' do
194
200
it 'should not accept new threads' do
195
- Thread . new { barrier . wait ( 0.1 ) }
196
- sleep ( 0.2 )
201
+ t = Thread . new { barrier . wait ( 0.1 ) }
202
+ t . join ( 0.2 )
197
203
198
204
expect ( barrier ) . to be_broken
199
205
200
206
expect ( barrier . wait ) . to be_falsey
201
207
end
202
208
203
209
it 'can be reset' do
204
- Thread . new { barrier . wait ( 0.1 ) }
205
- sleep ( 0.2 )
210
+ t = Thread . new { barrier . wait ( 0.1 ) }
211
+ t . join ( 0.2 )
206
212
207
213
expect ( barrier ) . to be_broken
208
214
@@ -226,23 +232,23 @@ def barrier.simulate_spurious_wake_up
226
232
227
233
it 'should resist to spurious wake ups without timeout' do
228
234
@expected = false
229
- Thread . new { barrier . wait ; @expected = true }
235
+ t = Thread . new { barrier . wait ; @expected = true }
236
+ t . join ( 0.1 )
230
237
231
- sleep ( 0.1 )
232
238
barrier . simulate_spurious_wake_up
233
239
234
- sleep ( 0.1 )
240
+ t . join ( 0.1 )
235
241
expect ( @expected ) . to be_falsey
236
242
end
237
243
238
244
it 'should resist to spurious wake ups with timeout' do
239
245
@expected = false
240
- Thread . new { barrier . wait ( 0.5 ) ; @expected = true }
246
+ t = Thread . new { barrier . wait ( 0.5 ) ; @expected = true }
241
247
242
- sleep ( 0.1 )
248
+ t . join ( 0.1 )
243
249
barrier . simulate_spurious_wake_up
244
250
245
- sleep ( 0.1 )
251
+ t . join ( 0.1 )
246
252
expect ( @expected ) . to be_falsey
247
253
248
254
end
0 commit comments