Project

General

Profile

Feature #11650 ยป feature.diff

kirs (Kir Shatrov), 11/03/2015 10:26 AM

View differences:

lib/timeout.rb
59 59
  #         value of 0 or +nil+ will execute the block without any timeout.
60 60
  # +klass+:: Exception Class to raise if the block fails to terminate
61 61
  #           in +sec+ seconds.  Omitting will use the default, Timeout::Error
62
  # +message+:: Error message to rasise with Exception Class.
63
  #             Omitting will use the default, "execution expired"
62 64
  #
63 65
  # Returns the result of the block *if* the block completed before
64 66
  # +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
......
69 71
  # Note that this is both a method of module Timeout, so you can <tt>include
70 72
  # Timeout</tt> into your classes so they have a #timeout method, as well as
71 73
  # a module method, so you can call it directly as Timeout.timeout().
72
  def timeout(sec, klass = nil)   #:yield: +sec+
74
  def timeout(sec, klass = nil, message = nil)   #:yield: +sec+
73 75
    return yield(sec) if sec == nil or sec.zero?
74
    message = "execution expired".freeze
76
    message ||= "execution expired".freeze
75 77
    e = Error
76 78
    bl = proc do |exception|
77 79
      begin
test/test_timeout.rb
65 65
        sleep 3
66 66
      end
67 67
    end
68
    assert_raise_with_message(err, /connection to rubylang.org expired/) do
69
      Timeout.timeout 0.01, err, "connection to rubylang.org expired" do
70
        sleep 3
71
      end
72
    end
68 73
  end
69 74

  
70 75
  def test_exit_exception