Skip to content

Commit 6239f09

Browse files
committed
Merge pull request #189 from ruby-concurrency/bug/timer-task
Fixed timing interval bug in TimerTask; updated yardoc.
2 parents fb7e0f3 + bc085a7 commit 6239f09

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Please see the [roadmap](https://github.com/ruby-concurrency/concurrent-ruby/iss
1414
* Prevent `Actor` from using an `ImmediateExecutor` (causes deadlock)
1515
* Added missing synchronizations to `TimerSet`
1616
* Fixed bug with return value of `Concurrent::Actor::Utils::Pool#ask`
17+
* Fixed timing bug in `TimerTask`
1718
* Removed confusing warning when not using native extenstions
1819
* Improved documentation
1920

lib/concurrent/timer_task.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ module Concurrent
4545
#
4646
# @example Basic usage
4747
# task = Concurrent::TimerTask.new{ puts 'Boom!' }
48-
# task.run!
48+
# task.execute
4949
#
5050
# task.execution_interval #=> 60 (default)
5151
# task.timeout_interval #=> 30 (default)
5252
#
5353
# # wait 60 seconds...
5454
# #=> 'Boom!'
5555
#
56-
# task.stop #=> true
56+
# task.shutdown #=> true
5757
#
5858
# @example Configuring `:execution_interval` and `:timeout_interval`
5959
# task = Concurrent::TimerTask.new(execution_interval: 5, timeout_interval: 5) do
@@ -65,7 +65,7 @@ module Concurrent
6565
#
6666
# @example Immediate execution with `:run_now`
6767
# task = Concurrent::TimerTask.new(run_now: true){ puts 'Boom!' }
68-
# task.run!
68+
# task.execute
6969
#
7070
# #=> 'Boom!'
7171
#
@@ -75,7 +75,7 @@ module Concurrent
7575
# execution_interval: 5
7676
# ){ Time.now }
7777
#
78-
# task.run!
78+
# task.execute
7979
# Time.now #=> 2013-11-07 18:06:50 -0500
8080
# sleep(10)
8181
# task.value #=> 2013-11-07 18:06:55 -0500
@@ -87,11 +87,11 @@ module Concurrent
8787
# task.execution_interval += 1
8888
# if task.execution_interval > 5
8989
# puts 'Stopping...'
90-
# task.stop
90+
# task.shutdown
9191
# end
9292
# end
9393
#
94-
# timer_task.run # blocking call - this task will stop itself
94+
# timer_task.execute # blocking call - this task will stop itself
9595
# #=> Boom!
9696
# #=> Boom! Boom!
9797
# #=> Boom! Boom! Boom!
@@ -114,30 +114,30 @@ module Concurrent
114114
#
115115
# task = Concurrent::TimerTask.new(execution_interval: 1, timeout_interval: 1){ 42 }
116116
# task.add_observer(TaskObserver.new)
117-
# task.run!
117+
# task.execute
118118
#
119119
# #=> (2013-10-13 19:08:58 -0400) Execution successfully returned 42
120120
# #=> (2013-10-13 19:08:59 -0400) Execution successfully returned 42
121121
# #=> (2013-10-13 19:09:00 -0400) Execution successfully returned 42
122-
# task.stop
122+
# task.shutdown
123123
#
124124
# task = Concurrent::TimerTask.new(execution_interval: 1, timeout_interval: 1){ sleep }
125125
# task.add_observer(TaskObserver.new)
126-
# task.run!
126+
# task.execute
127127
#
128128
# #=> (2013-10-13 19:07:25 -0400) Execution timed out
129129
# #=> (2013-10-13 19:07:27 -0400) Execution timed out
130130
# #=> (2013-10-13 19:07:29 -0400) Execution timed out
131-
# task.stop
131+
# task.shutdown
132132
#
133133
# task = Concurrent::TimerTask.new(execution_interval: 1){ raise StandardError }
134134
# task.add_observer(TaskObserver.new)
135-
# task.run!
135+
# task.execute
136136
#
137137
# #=> (2013-10-13 19:09:37 -0400) Execution failed with error StandardError
138138
# #=> (2013-10-13 19:09:38 -0400) Execution failed with error StandardError
139139
# #=> (2013-10-13 19:09:39 -0400) Execution failed with error StandardError
140-
# task.stop
140+
# task.shutdown
141141
#
142142
# @see http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html
143143
# @see http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html
@@ -316,7 +316,7 @@ def schedule_next_task(interval = execution_interval)
316316
# @!visibility private
317317
def execute_task(completion)
318318
return unless @running.true?
319-
Concurrent::timer(timeout_interval, completion, &method(:timeout_task))
319+
Concurrent::timer(execution_interval, completion, &method(:timeout_task))
320320
success, value, reason = @executor.execute(self)
321321
if completion.try?
322322
self.value = value

spec/concurrent/timer_task_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ def trigger_observable(observable)
119119
expect(task.kill).to be_truthy
120120
end
121121
end
122+
123+
context '#shutdown' do
124+
125+
it 'returns true on success' do
126+
task = TimerTask.execute(run_now: false) { nil }
127+
sleep(0.1)
128+
expect(task.shutdown).to be_truthy
129+
end
130+
end
122131
end
123132

124133
context 'arguments' do

0 commit comments

Comments
 (0)