timeout(0) -> never timeout (compatible to Timeout)
This commit is contained in:
parent
0a0f47650e
commit
9f993da845
|
@ -74,9 +74,10 @@ module TimeoutInterruptSingleton
|
||||||
|
|
||||||
# Creates a timeout and calls your block, which has to finish before timeout occurs.
|
# 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 [Integer] In `seconds` Seconds, it should raise a timeout, if not finished.
|
||||||
# @param seconds [nil] Everything will be ignored and
|
# @param seconds [nil] If also no block given, everything will be ignored and
|
||||||
# it will call {setup} for checking and preparing next known timeout.
|
# it will call {setup} for checking and preparing next known timeout.
|
||||||
# @param exception [Exception] which will be raised if timed out.
|
# @param exception [Exception] which will be raised if timed out.
|
||||||
# @param exception [nil] `TimeoutInterrupt::Error` will be used to raise.
|
# @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.
|
# @param block [Proc] Will be called and should finish its work before it timed out.
|
||||||
|
@ -102,6 +103,7 @@ module TimeoutInterruptSingleton
|
||||||
# @see TimeoutInterrupt#timeout
|
# @see TimeoutInterrupt#timeout
|
||||||
# @raise exception
|
# @raise exception
|
||||||
def timeout seconds = nil, exception = nil, &block
|
def timeout seconds = nil, exception = nil, &block
|
||||||
|
return yield( seconds) if seconds.nil? || 0 == seconds if block_given?
|
||||||
return setup if seconds.nil?
|
return setup if seconds.nil?
|
||||||
seconds = seconds.to_i
|
seconds = seconds.to_i
|
||||||
exception ||= TimeoutInterrupt::Error
|
exception ||= TimeoutInterrupt::Error
|
||||||
|
@ -117,7 +119,7 @@ module TimeoutInterruptSingleton
|
||||||
begin
|
begin
|
||||||
self.timeouts[key] = [at, bt, exception]
|
self.timeouts[key] = [at, bt, exception]
|
||||||
setup
|
setup
|
||||||
yield
|
yield seconds
|
||||||
ensure
|
ensure
|
||||||
self.timeouts.delete key
|
self.timeouts.delete key
|
||||||
setup
|
setup
|
||||||
|
@ -140,9 +142,10 @@ module TimeoutInterrupt
|
||||||
|
|
||||||
# Creates a timeout and calls your block, which has to finish before timeout occurs.
|
# 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 [Integer] In `seconds` Seconds, it should raise a timeout, if not finished.
|
||||||
# @param seconds [nil] Everything will be ignored and
|
# @param seconds [nil] If also no block given, everything will be ignored and
|
||||||
# it will call {TimeoutInterruptSingleton.setup} for checking and preparing next known timeout.
|
# it will call {setup} for checking and preparing next known timeout.
|
||||||
# @param exception [Exception] which will be raised if timed out.
|
# @param exception [Exception] which will be raised if timed out.
|
||||||
# @param exception [nil] `TimeoutInterrupt::Error` will be used to raise.
|
# @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.
|
# @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.
|
# 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 [Integer] In `seconds` Seconds, it should raise a timeout, if not finished.
|
||||||
# @param seconds [nil] Everything will be ignored and
|
# @param seconds [nil] If also no block given, everything will be ignored and
|
||||||
# it will call {TimeoutInterruptSingleton.setup} for checking and preparing next known timeout.
|
# it will call {setup} for checking and preparing next known timeout.
|
||||||
# @param exception [Exception] which will be raised if timed out.
|
# @param exception [Exception] which will be raised if timed out.
|
||||||
# @param exception [nil] `TimeoutInterrupt::Error` will be used to raise.
|
# @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.
|
# @param block [Proc] Will be called and should finish its work before it timed out.
|
||||||
|
|
|
@ -152,4 +152,11 @@ class TestRubyTimeoutInterrupt < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue