aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-09-07 08:21:56 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-09-07 08:21:56 +0000
commitf23c04aee3419d1081b9609e32166660a27565fe (patch)
tree8421da14e206306449166ff9b82750db207fa9f4
parent16e825005ca3b1266b4e1f92d4d2b8b95cc92b42 (diff)
downloadruby-f23c04aee3419d1081b9609e32166660a27565fe.tar.gz
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
-rw-r--r--ChangeLog5
-rw-r--r--lib/timeout.rb6
-rw-r--r--test/test_timeout.rb5
3 files changed, 14 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index e0ad4ac82f..31a06aa998 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
* common.mk: Updated Unicode version to 9.0.0 [Feature #12513]
diff --git a/lib/timeout.rb b/lib/timeout.rb
index 792a7c1093..8d81b596ca 100644
--- a/lib/timeout.rb
+++ b/lib/timeout.rb
@@ -60,6 +60,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+.
@@ -70,9 +72,9 @@ module Timeout
# 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
# 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
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
e = Error
bl = proc do |exception|
diff --git a/test/test_timeout.rb b/test/test_timeout.rb
index 1af61b7f8b..56b2469978 100644
--- a/test/test_timeout.rb
+++ b/test/test_timeout.rb
@@ -66,6 +66,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