Skip to content

Commit a1f3d4f

Browse files
committed
Implemented TimerSet#reset
1 parent a1a62fe commit a1f3d4f

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/concurrent/executor/timer_set.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,18 @@ def <=>(other)
4242
self.schedule_time <=> other.schedule_time
4343
end
4444

45+
# Has the task been cancelled?
46+
#
47+
# @return [Boolean] true if the task is in the given state else false
4548
def cancelled?
46-
state == :cancelled
49+
synchronize { ns_check_state?(:cancelled) }
50+
end
51+
52+
# In the task execution in progress?
53+
#
54+
# @return [Boolean] true if the task is in the given state else false
55+
def processing?
56+
synchronize { ns_check_state?(:processing) }
4757
end
4858

4959
def cancel
@@ -57,17 +67,12 @@ def cancel
5767
end
5868
end
5969

60-
#def reset
61-
# reschedule(synchronize{ @delay })
62-
#end
70+
def reset
71+
synchronize{ ns_reschedule(@delay) }
72+
end
6373

6474
def reschedule(delay)
65-
synchronize do
66-
return false unless ns_check_state?(:pending)
67-
ns_set_delay_and_time!(delay)
68-
return false unless @parent.send(:remove_task, self)
69-
@parent.send(:ns_post_task, self)
70-
end
75+
synchronize{ ns_reschedule(delay) }
7176
end
7277

7378
# @!visibility private
@@ -83,6 +88,13 @@ def ns_set_delay_and_time!(delay)
8388
@delay = TimerSet.calculate_delay!(delay)
8489
@time = Concurrent.monotonic_time + @delay
8590
end
91+
92+
def ns_reschedule(delay)
93+
return false unless ns_check_state?(:pending)
94+
ns_set_delay_and_time!(delay)
95+
return false unless @parent.send(:remove_task, self)
96+
@parent.send(:ns_post_task, self)
97+
end
8698
end
8799

88100
# Create a new set of timed tasks.

spec/concurrent/executor/timer_set_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ module Concurrent
320320
end
321321
end
322322

323+
context 'task resetting' do
324+
325+
it 'calls #reschedule with the original delay' do
326+
original_delay = 10
327+
task = subject.post(original_delay){ nil }
328+
expect(task).to receive(:ns_reschedule).with(original_delay)
329+
task.reset
330+
end
331+
end
332+
323333
context 'termination' do
324334

325335
it 'cancels all pending tasks on #shutdown' do

0 commit comments

Comments
 (0)