diff --git a/lib/timeout.rb b/lib/timeout.rb index 63a6299..80c0d0c 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -59,6 +59,8 @@ module Timeout # value of 0 or +nil+ will execute the block without any timeout. # +klass+:: Exception Class to raise if the block fails to terminate # in +sec+ seconds. Omitting will use the default, Timeout::Error + # +message+:: Error message to rasise with Exception Class. + # Omitting will use the default, "execution expired" # # Returns the result of the block *if* the block completed before # +sec+ seconds, otherwise throws an exception, based on the value of +klass+. @@ -69,9 +71,9 @@ module Timeout # Note that this is both a method of module Timeout, so you can include # 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) #:yield: +sec+ + def timeout(sec, klass = nil, message = nil) #:yield: +sec+ return yield(sec) if sec == nil or sec.zero? - message = "execution expired".freeze + message ||= "execution expired".freeze e = Error bl = proc do |exception| begin diff --git a/test/test_timeout.rb b/test/test_timeout.rb index 88a9b82..47053db 100644 --- a/test/test_timeout.rb +++ b/test/test_timeout.rb @@ -65,6 +65,11 @@ class TestTimeout < Test::Unit::TestCase sleep 3 end end + assert_raise_with_message(err, /connection to rubylang.org expired/) do + Timeout.timeout 0.01, err, "connection to rubylang.org expired" do + sleep 3 + end + end end def test_exit_exception