From 9f993da8451f6e961559aade0aadb58faa4b832b Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 14 Mar 2013 16:26:07 +0100 Subject: [PATCH] timeout(0) -> never timeout (compatible to Timeout) --- lib/timeout_interrupt.rb | 18 +++++++++++------- test/test_ruby-timeout-interrupt.rb | 7 +++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/timeout_interrupt.rb b/lib/timeout_interrupt.rb index 01d09b3..5c46ff8 100644 --- a/lib/timeout_interrupt.rb +++ b/lib/timeout_interrupt.rb @@ -74,9 +74,10 @@ module TimeoutInterruptSingleton # Creates a timeout and calls your block, which has to finish before timeout occurs. # + # @param seconds [0] No timeout, so block can take any time. # @param seconds [Integer] In `seconds` Seconds, it should raise a timeout, if not finished. - # @param seconds [nil] Everything will be ignored and - # it will call {setup} for checking and preparing next known timeout. + # @param seconds [nil] If also no block given, everything will be ignored and + # it will call {setup} for checking and preparing next known timeout. # @param exception [Exception] which will be raised if timed out. # @param exception [nil] `TimeoutInterrupt::Error` will be used to raise. # @param block [Proc] Will be called and should finish its work before it timed out. @@ -102,6 +103,7 @@ module TimeoutInterruptSingleton # @see TimeoutInterrupt#timeout # @raise exception def timeout seconds = nil, exception = nil, &block + return yield( seconds) if seconds.nil? || 0 == seconds if block_given? return setup if seconds.nil? seconds = seconds.to_i exception ||= TimeoutInterrupt::Error @@ -117,7 +119,7 @@ module TimeoutInterruptSingleton begin self.timeouts[key] = [at, bt, exception] setup - yield + yield seconds ensure self.timeouts.delete key setup @@ -140,9 +142,10 @@ module TimeoutInterrupt # Creates a timeout and calls your block, which has to finish before timeout occurs. # + # @param seconds [0] No timeout, so block can take any time. # @param seconds [Integer] In `seconds` Seconds, it should raise a timeout, if not finished. - # @param seconds [nil] Everything will be ignored and - # it will call {TimeoutInterruptSingleton.setup} for checking and preparing next known timeout. + # @param seconds [nil] If also no block given, everything will be ignored and + # it will call {setup} for checking and preparing next known timeout. # @param exception [Exception] which will be raised if timed out. # @param exception [nil] `TimeoutInterrupt::Error` will be used to raise. # @param block [Proc] Will be called and should finish its work before it timed out. @@ -173,9 +176,10 @@ module TimeoutInterrupt # Creates a timeout and calls your block, which has to finish before timeout occurs. # + # @param seconds [0] No timeout, so block can take any time. # @param seconds [Integer] In `seconds` Seconds, it should raise a timeout, if not finished. - # @param seconds [nil] Everything will be ignored and - # it will call {TimeoutInterruptSingleton.setup} for checking and preparing next known timeout. + # @param seconds [nil] If also no block given, everything will be ignored and + # it will call {setup} for checking and preparing next known timeout. # @param exception [Exception] which will be raised if timed out. # @param exception [nil] `TimeoutInterrupt::Error` will be used to raise. # @param block [Proc] Will be called and should finish its work before it timed out. diff --git a/test/test_ruby-timeout-interrupt.rb b/test/test_ruby-timeout-interrupt.rb index 91487ba..7e87cfe 100644 --- a/test/test_ruby-timeout-interrupt.rb +++ b/test/test_ruby-timeout-interrupt.rb @@ -152,4 +152,11 @@ class TestRubyTimeoutInterrupt < Test::Unit::TestCase end end end + + should "not timeout, if timeout is 0" do + assert_nothing_raised TimeoutInterrupt::Error, "Unexpected Timed out." do + # should never timeout (we can not wait infinity seconds, so only 5) + TimeoutInterrupt.timeout( 0) { sleep 5 } + end + end end