[ruby/timeout] refactor the change to keep the compatability with nil and type-errror and added tests

https://github.com/ruby/timeout/commit/8342544979
This commit is contained in:
CosmicOppai 2024-10-14 16:23:53 +05:30 committed by git
parent 203a023447
commit 0f2cd39e68
2 changed files with 8 additions and 2 deletions

View File

@ -164,7 +164,7 @@ module Timeout
# 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().
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
raise ArgumentError, "Timeout sec must be a positive number" unless sec.is_a?(Numeric) && sec >= 0
raise ArgumentError, "Timeout sec must be a positive number" if sec.is_a?(Numeric) && sec < 0
return yield(sec) if sec == nil or sec.zero?
message ||= "execution expired"

View File

@ -1,6 +1,6 @@
# frozen_string_literal: false
require 'test/unit'
require 'timeout'
require_relative '../lib/timeout'
class TestTimeout < Test::Unit::TestCase
@ -31,6 +31,12 @@ class TestTimeout < Test::Unit::TestCase
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