From 0f2cd39e680c9b15ddd94d34770a8a17d0c5a0ae Mon Sep 17 00:00:00 2001 From: CosmicOppai Date: Mon, 14 Oct 2024 16:23:53 +0530 Subject: [PATCH] [ruby/timeout] refactor the change to keep the compatability with nil and type-errror and added tests https://github.com/ruby/timeout/commit/8342544979 --- lib/timeout.rb | 2 +- test/test_timeout.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/timeout.rb b/lib/timeout.rb index c8c3d501b6..59437f490e 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -164,7 +164,7 @@ module Timeout # Timeout 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" diff --git a/test/test_timeout.rb b/test/test_timeout.rb index 34966f92a4..888489f430 100644 --- a/test/test_timeout.rb +++ b/test/test_timeout.rb @@ -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