Skip to content

Commit 0f2cd39

Browse files
Cosmicoppaimatzbot
authored andcommitted
[ruby/timeout] refactor the change to keep the compatability with nil and type-errror and added tests
ruby/timeout@8342544979
1 parent 203a023 commit 0f2cd39

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/timeout.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ 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" unless sec.is_a?(Numeric) && sec >= 0
167+
raise ArgumentError, "Timeout sec must be a positive number" if sec.is_a?(Numeric) && sec < 0
168168
return yield(sec) if sec == nil or sec.zero?
169169

170170
message ||= "execution expired"

test/test_timeout.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: false
22
require 'test/unit'
3-
require 'timeout'
3+
require_relative '../lib/timeout'
44

55
class TestTimeout < Test::Unit::TestCase
66

@@ -31,6 +31,12 @@ def test_allows_nil_seconds
3131
end
3232
end
3333

34+
def test_raise_for_neg_second
35+
assert_raise(ArgumentError) do
36+
Timeout.timeout(-1) { sleep(0.01) }
37+
end
38+
end
39+
3440
def test_included
3541
c = Class.new do
3642
include Timeout

0 commit comments

Comments
 (0)