mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
timeout.rb: custom error message
* lib/timeout.rb (Timeout#timeout): add custom error message argument. [Feature #11650] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56089 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
62e8ceae11
commit
047ca4ba1a
3 changed files with 14 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Wed Sep 7 17:21:55 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/timeout.rb (Timeout#timeout): add custom error message
|
||||||
|
argument. [Feature #11650]
|
||||||
|
|
||||||
Wed Sep 7 17:13:05 2016 Martin Duerst <duerst@it.aoyama.ac.jp>
|
Wed Sep 7 17:13:05 2016 Martin Duerst <duerst@it.aoyama.ac.jp>
|
||||||
|
|
||||||
* common.mk: Updated Unicode version to 9.0.0 [Feature #12513]
|
* common.mk: Updated Unicode version to 9.0.0 [Feature #12513]
|
||||||
|
|
|
@ -60,6 +60,8 @@ module Timeout
|
||||||
# value of 0 or +nil+ will execute the block without any timeout.
|
# value of 0 or +nil+ will execute the block without any timeout.
|
||||||
# +klass+:: Exception Class to raise if the block fails to terminate
|
# +klass+:: Exception Class to raise if the block fails to terminate
|
||||||
# in +sec+ seconds. Omitting will use the default, Timeout::Error
|
# 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
|
# Returns the result of the block *if* the block completed before
|
||||||
# +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
|
# +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
|
||||||
|
@ -70,9 +72,9 @@ module Timeout
|
||||||
# Note that this is both a method of module Timeout, so you can <tt>include
|
# Note that this is both a method of module Timeout, so you can <tt>include
|
||||||
# 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) #:yield: +sec+
|
def timeout(sec, klass = nil, message = nil) #:yield: +sec+
|
||||||
return yield(sec) if sec == nil or sec.zero?
|
return yield(sec) if sec == nil or sec.zero?
|
||||||
message = "execution expired".freeze
|
message ||= "execution expired".freeze
|
||||||
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
|
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
|
||||||
e = Error
|
e = Error
|
||||||
bl = proc do |exception|
|
bl = proc do |exception|
|
||||||
|
|
|
@ -66,6 +66,11 @@ class TestTimeout < Test::Unit::TestCase
|
||||||
sleep 3
|
sleep 3
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
def test_exit_exception
|
def test_exit_exception
|
||||||
|
|
Loading…
Add table
Reference in a new issue