Skip to content

Commit e7dd185

Browse files
Cosmicoppaimatzbot
authored andcommitted
[ruby/timeout] refactor the change to raise for nil and type-errror and added tests
ruby/timeout@ffc8d7c003
1 parent 0839eae commit e7dd185

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

lib/timeout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def self.ensure_timeout_thread_created
164164
# Timeout</tt> into your classes so they have a #timeout method, as well as
165165
# a module method, so you can call it directly as Timeout.timeout().
166166
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
167-
raise ArgumentError, "Timeout sec must be a positive number" if sec.is_a?(Numeric) && sec < 0
168-
return yield(sec) if sec == nil or sec.zero?
167+
raise ArgumentError, "Timeout sec must be a positive number" unless sec.is_a?(Numeric) && sec >= 0
168+
return yield(sec) if sec.zero?
169169

170170
message ||= "execution expired"
171171

test/test_timeout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_allows_zero_seconds
2626
end
2727

2828
def test_allows_nil_seconds
29-
assert_nothing_raised do
29+
assert_raise(ArgumentError) do
3030
assert_equal :ok, Timeout.timeout(nil){:ok}
3131
end
3232
end
@@ -120,7 +120,7 @@ def test_nested_timeout_which_error_bubbles_up
120120
def test_cannot_convert_into_time_interval
121121
bug3168 = '[ruby-dev:41010]'
122122
def (n = Object.new).zero?; false; end
123-
assert_raise(TypeError, bug3168) {Timeout.timeout(n) { sleep 0.1 }}
123+
assert_raise(ArgumentError, bug3168) {Timeout.timeout(n) { sleep 0.1 }}
124124
end
125125

126126
def test_skip_rescue_standarderror

0 commit comments

Comments
 (0)