Skip to content

Commit

Permalink
support for custom exception classes
Browse files Browse the repository at this point in the history
  • Loading branch information
runa committed Oct 22, 2009
1 parent beedf47 commit d33acb3
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/system_timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class << self
# Executes the method's block. If the block execution terminates before
# +seconds+ seconds has passed, it returns true. If not, it terminates
# the execution and raises a +Timeout::Error+.
def timeout_after(seconds)
def timeout_after(seconds, exception_class = nil)
new_timer = nil # just for scope
@mutex.synchronize do
new_timer = timer_pool.add_timer seconds
new_timer = timer_pool.add_timer seconds, exception_class
timer_interval = timer_pool.next_trigger_interval_in_seconds
debug "==== Install Timer ==== at #{Time.now.to_i}, next interval: #{timer_interval}"
if timer_pool.first_timer?
Expand Down Expand Up @@ -104,4 +104,4 @@ def debug(message) #:nodoc

end

require 'system_timer_native'
require 'system_timer_native'
12 changes: 6 additions & 6 deletions lib/system_timer/concurrent_timer_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def registered_timers
@timers ||= []
end

def register_timer(trigger_time, thread)
new_timer = ThreadTimer.new(trigger_time, thread)
def register_timer(trigger_time, thread, exception_class=nil)
new_timer = ThreadTimer.new(trigger_time, thread, exception_class)
registered_timers << new_timer
new_timer
end

def add_timer(interval_in_seconds)
new_timer = register_timer(Time.now.to_i + interval_in_seconds, Thread.current)
def add_timer(interval_in_seconds, exception_class=nil)
new_timer = register_timer(Time.now.to_i + interval_in_seconds, Thread.current, exception_class)
log_registered_timers if SystemTimer.debug_enabled?
new_timer
end
Expand Down Expand Up @@ -55,7 +55,7 @@ def trigger_next_expired_timer_at(now_in_seconds_since_epoch)

cancel timer
log_timeout_received(timer) if SystemTimer.debug_enabled?
timer.thread.raise Timeout::Error.new("time's up!")
timer.thread.raise timer.exception_class.new("time's up!")
end

def trigger_next_expired_timer
Expand All @@ -80,4 +80,4 @@ def log_registered_timers #:nodoc:

end

end
end
7 changes: 4 additions & 3 deletions lib/system_timer/thread_timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ module SystemTimer
# from a Ruby signal handler and Ruby signals are always delivered to
# main thread.
class ThreadTimer
attr_reader :trigger_time, :thread
attr_reader :trigger_time, :thread, :exception_class

def initialize(trigger_time, thread)
def initialize(trigger_time, thread, exception_class = nil)
@trigger_time = trigger_time
@thread = thread
@exception_class = exception_class || Timeout::Error
end

def to_s
"<ThreadTimer :time => #{trigger_time}, :thread => #{thread}>"
"<ThreadTimer :time => #{trigger_time}, :thread => #{thread}, :exception_class => #{exception_class}>"
end

end
Expand Down
2 changes: 1 addition & 1 deletion test/system_timer/concurrent_timer_pool_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
now = Time.now
Time.stubs(:now).returns(now)

pool.expects(:register_timer).with(now.to_i + 15, :the_current_thread)
pool.expects(:register_timer).with(now.to_i + 15, :the_current_thread, nil)
pool.add_timer 15
end

Expand Down
2 changes: 1 addition & 1 deletion test/system_timer/thread_timer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

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

Expand Down
4 changes: 2 additions & 2 deletions test/system_timer_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
SystemTimer.stubs(:install_next_timer)
SystemTimer.stubs(:restore_original_configuration)

pool.expects(:add_timer).with(5).returns(stub_everything)
pool.expects(:add_timer).with(5,nil).returns(stub_everything)
SystemTimer.timeout_after(5) {}
end

Expand Down Expand Up @@ -93,4 +93,4 @@
end
end

end
end

0 comments on commit d33acb3

Please sign in to comment.