Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ def self.ensure_timeout_thread_created
# Perform an operation in a block, raising an error if it takes longer than
# +sec+ seconds to complete.
#
# +sec+:: Number of seconds to wait for the block to terminate. Any number
# may be used, including Floats to specify fractional seconds. A
# +sec+:: Number of seconds to wait for the block to terminate. Any non-negative number
# or nil may be used, including Floats to specify fractional seconds. A
# value of 0 or +nil+ will execute the block without any timeout.
# Any negative number will raise an ArgumentError.
# +klass+:: Exception Class to raise if the block fails to terminate
# in +sec+ seconds. Omitting will use the default, Timeout::Error
# +message+:: Error message to raise with Exception Class.
Expand All @@ -165,6 +166,7 @@ def self.ensure_timeout_thread_created
# a module method, so you can call it directly as Timeout.timeout().
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
return yield(sec) if sec == nil or sec.zero?
raise ArgumentError, "Timeout sec must be a non-negative number" if 0 > sec

message ||= "execution expired"

Expand Down
8 changes: 7 additions & 1 deletion test/test_timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_allows_nil_seconds
end
end

def test_raise_for_neg_second
assert_raise(ArgumentError) do
Timeout.timeout(-1) { sleep(0.01) }
end
end

def test_included
c = Class.new do
include Timeout
Expand Down Expand Up @@ -114,7 +120,7 @@ def test_nested_timeout_which_error_bubbles_up
def test_cannot_convert_into_time_interval
bug3168 = '[ruby-dev:41010]'
def (n = Object.new).zero?; false; end
assert_raise(TypeError, bug3168) {Timeout.timeout(n) { sleep 0.1 }}
assert_raise(ArgumentError, bug3168) {Timeout.timeout(n) { sleep 0.1 }}
end

def test_skip_rescue_standarderror
Expand Down