1
1
require 'timecop'
2
+ require_relative 'dereferenceable_shared'
2
3
require_relative 'obligation_shared'
3
4
require_relative 'observable_shared'
4
5
@@ -8,8 +9,6 @@ module Concurrent
8
9
9
10
context 'behavior' do
10
11
11
- # obligation
12
-
13
12
let! ( :fulfilled_value ) { 10 }
14
13
let! ( :rejected_reason ) { StandardError . new ( 'mojo jojo' ) }
15
14
@@ -33,21 +32,30 @@ module Concurrent
33
32
task
34
33
end
35
34
36
- it_should_behave_like :obligation
37
-
38
- # dereferenceable
39
-
40
- specify { expect ( ScheduledTask . ancestors ) . to include ( Dereferenceable ) }
35
+ def dereferenceable_subject ( value , opts = { } )
36
+ task = ScheduledTask . execute ( 0 , opts ) { value } . execute
37
+ task . value
38
+ task
39
+ end
41
40
42
- # observable
41
+ def dereferenceable_observable ( opts = { } )
42
+ ScheduledTask . new ( 0 , opts ) { 'value' }
43
+ end
43
44
44
- subject { ScheduledTask . new ( 0.1 ) { nil } }
45
+ def execute_dereferenceable ( subject )
46
+ subject . execute
47
+ subject . value
48
+ end
45
49
46
50
def trigger_observable ( observable )
47
51
observable . execute
48
52
sleep ( 0.2 )
49
53
end
50
54
55
+ subject { ScheduledTask . new ( 0.1 ) { nil } }
56
+
57
+ it_should_behave_like :obligation
58
+ it_should_behave_like :dereferenceable
51
59
it_should_behave_like :observable
52
60
end
53
61
@@ -107,10 +115,6 @@ def trigger_observable(observable)
107
115
task . execute
108
116
end
109
117
110
- it 'allows setting the execution interval to 0' do
111
- expect { 1000 . times { ScheduledTask . execute ( 0 ) { } } } . not_to raise_error
112
- end
113
-
114
118
it 'sets the sate to :pending' do
115
119
task = ScheduledTask . new ( 1 ) { nil }
116
120
task . execute
@@ -145,6 +149,50 @@ def trigger_observable(observable)
145
149
end
146
150
end
147
151
152
+ context 'execution' do
153
+
154
+ it 'passes :args from the options to the block' do
155
+ expected = [ 1 , 2 , 3 ]
156
+ actual = nil
157
+ latch = Concurrent ::CountDownLatch . new
158
+ task = ScheduledTask . execute ( 0 , args : expected ) do |*args |
159
+ actual = args
160
+ latch . count_down
161
+ end
162
+ latch . wait ( 2 )
163
+ expect ( actual ) . to eq expected
164
+ end
165
+
166
+ it 'uses the :executor from the options' do
167
+ latch = Concurrent ::CountDownLatch . new
168
+ executor = Concurrent ::ImmediateExecutor . new
169
+ expect ( executor ) . to receive ( :post ) . once . with ( any_args ) . and_call_original
170
+ task = ScheduledTask . execute ( 0.1 , executor : executor ) do
171
+ latch . count_down
172
+ end
173
+ latch . wait ( 2 )
174
+ end
175
+
176
+ it 'uses the :timer_set from the options' do
177
+ timer = Concurrent ::TimerSet . new
178
+ expect ( timer ) . to receive ( :post_task ) . once . with ( any_args ) . and_return ( false )
179
+ task = ScheduledTask . execute ( 1 , timer_set : timer ) { nil }
180
+ end
181
+
182
+ it 'sets the state to :processing when the task is running' do
183
+ start_latch = Concurrent ::CountDownLatch . new ( 1 )
184
+ continue_latch = Concurrent ::CountDownLatch . new ( 1 )
185
+ task = ScheduledTask . new ( 0.1 ) {
186
+ start_latch . count_down
187
+ continue_latch . wait ( 2 )
188
+ } . execute
189
+ start_latch . wait ( 2 )
190
+ state = task . state
191
+ continue_latch . count_down
192
+ expect ( state ) . to eq :processing
193
+ end
194
+ end
195
+
148
196
context '#cancel' do
149
197
150
198
it 'returns false if the task has already been performed' do
@@ -171,7 +219,6 @@ def trigger_observable(observable)
171
219
expect ( latch . wait ( 0.3 ) ) . to be_falsey
172
220
end
173
221
174
-
175
222
it 'cancels the task if it has not yet started' do
176
223
latch = Concurrent ::CountDownLatch . new ( 1 )
177
224
task = ScheduledTask . new ( 0.3 ) { latch . count_down } . execute
@@ -195,19 +242,6 @@ def trigger_observable(observable)
195
242
end
196
243
end
197
244
198
- context 'execution' do
199
-
200
- it 'sets the state to :in_progress when the task is running' do
201
- latch = Concurrent ::CountDownLatch . new ( 1 )
202
- task = ScheduledTask . new ( 0.1 ) {
203
- latch . count_down
204
- sleep ( 1 )
205
- } . execute
206
- latch . wait ( 1 )
207
- expect ( task ) . to be_in_progress
208
- end
209
- end
210
-
211
245
context 'observation' do
212
246
213
247
let ( :clazz ) do
@@ -240,7 +274,7 @@ def update(time, value, reason)
240
274
expect ( task . add_observer ( observer ) ) . to be_truthy
241
275
end
242
276
243
- it 'returns true for an observer added while :in_progress ' do
277
+ it 'returns true for an observer added while :processing ' do
244
278
task = ScheduledTask . new ( 0.1 ) { sleep ( 1 ) ; 42 } . execute
245
279
sleep ( 0.2 )
246
280
expect ( task . add_observer ( observer ) ) . to be_truthy
0 commit comments