[ruby/timeout] refactor the change to raise for nil and type-errror and added tests

https://github.com/ruby/timeout/commit/ffc8d7c003
This commit is contained in:
CosmicOppai 2024-10-15 06:21:18 +05:30 committed by git
parent 0839eae2d3
commit e7dd185e21
2 changed files with 4 additions and 4 deletions

View File

@ -164,8 +164,8 @@ module Timeout
# Timeout</tt> into your classes so they have a #timeout method, as well as # Timeout</tt> into your classes so they have a #timeout method, as well as
# a module method, so you can call it directly as Timeout.timeout(). # a module method, so you can call it directly as Timeout.timeout().
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+ def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
raise ArgumentError, "Timeout sec must be a positive number" if sec.is_a?(Numeric) && sec < 0 raise ArgumentError, "Timeout sec must be a positive number" unless sec.is_a?(Numeric) && sec >= 0
return yield(sec) if sec == nil or sec.zero? return yield(sec) if sec.zero?
message ||= "execution expired" message ||= "execution expired"

View File

@ -26,7 +26,7 @@ class TestTimeout < Test::Unit::TestCase
end end
def test_allows_nil_seconds def test_allows_nil_seconds
assert_nothing_raised do assert_raise(ArgumentError) do
assert_equal :ok, Timeout.timeout(nil){:ok} assert_equal :ok, Timeout.timeout(nil){:ok}
end end
end end
@ -120,7 +120,7 @@ class TestTimeout < Test::Unit::TestCase
def test_cannot_convert_into_time_interval def test_cannot_convert_into_time_interval
bug3168 = '[ruby-dev:41010]' bug3168 = '[ruby-dev:41010]'
def (n = Object.new).zero?; false; end 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 end
def test_skip_rescue_standarderror def test_skip_rescue_standarderror