Skip to content

Commit d33acb3

Browse files
author
runa
committed
support for custom exception classes
1 parent beedf47 commit d33acb3

6 files changed

+17
-16
lines changed

lib/system_timer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class << self
4040
# Executes the method's block. If the block execution terminates before
4141
# +seconds+ seconds has passed, it returns true. If not, it terminates
4242
# the execution and raises a +Timeout::Error+.
43-
def timeout_after(seconds)
43+
def timeout_after(seconds, exception_class = nil)
4444
new_timer = nil # just for scope
4545
@mutex.synchronize do
46-
new_timer = timer_pool.add_timer seconds
46+
new_timer = timer_pool.add_timer seconds, exception_class
4747
timer_interval = timer_pool.next_trigger_interval_in_seconds
4848
debug "==== Install Timer ==== at #{Time.now.to_i}, next interval: #{timer_interval}"
4949
if timer_pool.first_timer?
@@ -104,4 +104,4 @@ def debug(message) #:nodoc
104104

105105
end
106106

107-
require 'system_timer_native'
107+
require 'system_timer_native'

lib/system_timer/concurrent_timer_pool.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ def registered_timers
88
@timers ||= []
99
end
1010

11-
def register_timer(trigger_time, thread)
12-
new_timer = ThreadTimer.new(trigger_time, thread)
11+
def register_timer(trigger_time, thread, exception_class=nil)
12+
new_timer = ThreadTimer.new(trigger_time, thread, exception_class)
1313
registered_timers << new_timer
1414
new_timer
1515
end
1616

17-
def add_timer(interval_in_seconds)
18-
new_timer = register_timer(Time.now.to_i + interval_in_seconds, Thread.current)
17+
def add_timer(interval_in_seconds, exception_class=nil)
18+
new_timer = register_timer(Time.now.to_i + interval_in_seconds, Thread.current, exception_class)
1919
log_registered_timers if SystemTimer.debug_enabled?
2020
new_timer
2121
end
@@ -55,7 +55,7 @@ def trigger_next_expired_timer_at(now_in_seconds_since_epoch)
5555

5656
cancel timer
5757
log_timeout_received(timer) if SystemTimer.debug_enabled?
58-
timer.thread.raise Timeout::Error.new("time's up!")
58+
timer.thread.raise timer.exception_class.new("time's up!")
5959
end
6060

6161
def trigger_next_expired_timer
@@ -80,4 +80,4 @@ def log_registered_timers #:nodoc:
8080

8181
end
8282

83-
end
83+
end

lib/system_timer/thread_timer.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ module SystemTimer
66
# from a Ruby signal handler and Ruby signals are always delivered to
77
# main thread.
88
class ThreadTimer
9-
attr_reader :trigger_time, :thread
9+
attr_reader :trigger_time, :thread, :exception_class
1010

11-
def initialize(trigger_time, thread)
11+
def initialize(trigger_time, thread, exception_class = nil)
1212
@trigger_time = trigger_time
1313
@thread = thread
14+
@exception_class = exception_class || Timeout::Error
1415
end
1516

1617
def to_s
17-
"<ThreadTimer :time => #{trigger_time}, :thread => #{thread}>"
18+
"<ThreadTimer :time => #{trigger_time}, :thread => #{thread}, :exception_class => #{exception_class}>"
1819
end
1920

2021
end

test/system_timer/concurrent_timer_pool_unit_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
now = Time.now
2626
Time.stubs(:now).returns(now)
2727

28-
pool.expects(:register_timer).with(now.to_i + 15, :the_current_thread)
28+
pool.expects(:register_timer).with(now.to_i + 15, :the_current_thread, nil)
2929
pool.add_timer 15
3030
end
3131

test/system_timer/thread_timer_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
test "to_s retruns a human friendly description of the timer" do
16-
assert_match /<ThreadTimer :time => 24, :thread => #<Thread(.*)>>/,
16+
assert_match /<ThreadTimer :time => 24, :thread => #<Thread(.*)>, :exception_class => Timeout::Error>/,
1717
SystemTimer::ThreadTimer.new(24, Thread.current).to_s
1818
end
1919

test/system_timer_unit_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
SystemTimer.stubs(:install_next_timer)
1010
SystemTimer.stubs(:restore_original_configuration)
1111

12-
pool.expects(:add_timer).with(5).returns(stub_everything)
12+
pool.expects(:add_timer).with(5,nil).returns(stub_everything)
1313
SystemTimer.timeout_after(5) {}
1414
end
1515

@@ -93,4 +93,4 @@
9393
end
9494
end
9595

96-
end
96+
end

0 commit comments

Comments
 (0)