From 428ab21416388a08d999a310d20a74f03d033519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Obrok?= Date: Mon, 23 Feb 2015 16:22:19 +0100 Subject: [PATCH] Fix ScheduledTask sometimes not accepting an interval of 0 ScheduledTask measured Time.now in 2 places with some computation in between and it sometimes arrived at an inconsistent result --- lib/concurrent/scheduled_task.rb | 5 +++-- spec/concurrent/scheduled_task_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/concurrent/scheduled_task.rb b/lib/concurrent/scheduled_task.rb index 3fa8c5e3e..dea505c72 100644 --- a/lib/concurrent/scheduled_task.rb +++ b/lib/concurrent/scheduled_task.rb @@ -25,8 +25,9 @@ def initialize(intended_time, opts = {}, &block) # @since 0.5.0 def execute if compare_and_set_state(:pending, :unscheduled) - @schedule_time = TimerSet.calculate_schedule_time(@intended_time) - Concurrent::timer(@schedule_time.to_f - Time.now.to_f) { @executor.post(&method(:process_task)) } + now = Time.now + @schedule_time = TimerSet.calculate_schedule_time(@intended_time, now) + Concurrent::timer(@schedule_time.to_f - now.to_f) { @executor.post(&method(:process_task)) } self end end diff --git a/spec/concurrent/scheduled_task_spec.rb b/spec/concurrent/scheduled_task_spec.rb index caf14b2c5..23b8e484a 100644 --- a/spec/concurrent/scheduled_task_spec.rb +++ b/spec/concurrent/scheduled_task_spec.rb @@ -130,6 +130,10 @@ def trigger_observable(observable) end end + it 'allows setting the execution interval to 0' do + expect { 1000.times { ScheduledTask.execute(0) { } } }.not_to raise_error + end + it 'sets the sate to :pending' do task = ScheduledTask.new(1){ nil } task.execute