Skip to content

Commit b0fbc4e

Browse files
committed
Merge pull request #249 from ruby-concurrency/zero-interval
Fix ScheduledTask sometimes not accepting an interval of 0
2 parents d94339b + 428ab21 commit b0fbc4e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/concurrent/scheduled_task.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def initialize(intended_time, opts = {}, &block)
2525
# @since 0.5.0
2626
def execute
2727
if compare_and_set_state(:pending, :unscheduled)
28-
@schedule_time = TimerSet.calculate_schedule_time(@intended_time)
29-
Concurrent::timer(@schedule_time.to_f - Time.now.to_f) { @executor.post(&method(:process_task)) }
28+
now = Time.now
29+
@schedule_time = TimerSet.calculate_schedule_time(@intended_time, now)
30+
Concurrent::timer(@schedule_time.to_f - now.to_f) { @executor.post(&method(:process_task)) }
3031
self
3132
end
3233
end

spec/concurrent/scheduled_task_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def trigger_observable(observable)
130130
end
131131
end
132132

133+
it 'allows setting the execution interval to 0' do
134+
expect { 1000.times { ScheduledTask.execute(0) { } } }.not_to raise_error
135+
end
136+
133137
it 'sets the sate to :pending' do
134138
task = ScheduledTask.new(1){ nil }
135139
task.execute

0 commit comments

Comments
 (0)