11require 'timecop'
2+ require_relative 'dereferenceable_shared'
23require_relative 'obligation_shared'
34require_relative 'observable_shared'
45
@@ -8,8 +9,6 @@ module Concurrent
89
910 context 'behavior' do
1011
11- # obligation
12-
1312 let! ( :fulfilled_value ) { 10 }
1413 let! ( :rejected_reason ) { StandardError . new ( 'mojo jojo' ) }
1514
@@ -33,21 +32,30 @@ module Concurrent
3332 task
3433 end
3534
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
4140
42- # observable
41+ def dereferenceable_observable ( opts = { } )
42+ ScheduledTask . new ( 0 , opts ) { 'value' }
43+ end
4344
44- subject { ScheduledTask . new ( 0.1 ) { nil } }
45+ def execute_dereferenceable ( subject )
46+ subject . execute
47+ subject . value
48+ end
4549
4650 def trigger_observable ( observable )
4751 observable . execute
4852 sleep ( 0.2 )
4953 end
5054
55+ subject { ScheduledTask . new ( 0.1 ) { nil } }
56+
57+ it_should_behave_like :obligation
58+ it_should_behave_like :dereferenceable
5159 it_should_behave_like :observable
5260 end
5361
@@ -107,10 +115,6 @@ def trigger_observable(observable)
107115 task . execute
108116 end
109117
110- it 'allows setting the execution interval to 0' do
111- expect { 1000 . times { ScheduledTask . execute ( 0 ) { } } } . not_to raise_error
112- end
113-
114118 it 'sets the sate to :pending' do
115119 task = ScheduledTask . new ( 1 ) { nil }
116120 task . execute
@@ -145,6 +149,50 @@ def trigger_observable(observable)
145149 end
146150 end
147151
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+
148196 context '#cancel' do
149197
150198 it 'returns false if the task has already been performed' do
@@ -171,7 +219,6 @@ def trigger_observable(observable)
171219 expect ( latch . wait ( 0.3 ) ) . to be_falsey
172220 end
173221
174-
175222 it 'cancels the task if it has not yet started' do
176223 latch = Concurrent ::CountDownLatch . new ( 1 )
177224 task = ScheduledTask . new ( 0.3 ) { latch . count_down } . execute
@@ -195,19 +242,6 @@ def trigger_observable(observable)
195242 end
196243 end
197244
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-
211245 context 'observation' do
212246
213247 let ( :clazz ) do
@@ -240,7 +274,7 @@ def update(time, value, reason)
240274 expect ( task . add_observer ( observer ) ) . to be_truthy
241275 end
242276
243- it 'returns true for an observer added while :in_progress ' do
277+ it 'returns true for an observer added while :processing ' do
244278 task = ScheduledTask . new ( 0.1 ) { sleep ( 1 ) ; 42 } . execute
245279 sleep ( 0.2 )
246280 expect ( task . add_observer ( observer ) ) . to be_truthy
0 commit comments