@@ -116,30 +116,34 @@ def subject.simulate_spurious_wake_up
116
116
end
117
117
118
118
it 'should resist to spurious wake ups without timeout' do
119
- @expected = true
120
- # would set @expected to false
121
- Thread . new { @expected = subject . acquire }
119
+ actual = Concurrent ::AtomicBoolean . new ( true )
120
+ latch = Concurrent ::CountDownLatch . new
122
121
123
- sleep ( 0.1 )
122
+ # would set actual to false
123
+ t = Thread . new { latch . wait ( 1 ) ; actual . value = subject . acquire }
124
+
125
+ latch . count_down
124
126
subject . simulate_spurious_wake_up
127
+ t . join ( 0.1 )
125
128
126
- sleep ( 0.1 )
127
- expect ( @expected ) . to be_truthy
129
+ expect ( actual . value ) . to be true
130
+ t . kill
128
131
end
129
132
130
133
it 'should resist to spurious wake ups with timeout' do
131
- @expected = true
132
- # sets @expected to false in another thread
133
- t = Thread . new { @expected = subject . try_acquire ( 1 , 0.3 ) }
134
+ actual = Concurrent ::AtomicBoolean . new ( true )
135
+ latch = Concurrent ::CountDownLatch . new
134
136
135
- sleep ( 0.1 )
136
- subject . simulate_spurious_wake_up
137
+ # sets actual to false in another thread
138
+ t = Thread . new { latch . wait ( 1 ) ; actual . value = subject . try_acquire ( 1 , 0.3 ) }
137
139
138
- sleep ( 0.1 )
139
- expect ( @expected ) . to be_truthy
140
+ latch . count_down
141
+ subject . simulate_spurious_wake_up
142
+ t . join ( 0.1 )
140
143
144
+ expect ( actual . value ) . to be true
141
145
t . join
142
- expect ( @expected ) . to be_falsey
146
+ expect ( actual . value ) . to be false
143
147
end
144
148
end
145
149
end
0 commit comments