module Timeout

Public Instance Methods

timeout(sec, klass=nil) { |sec| ... } click to toggle source
# File lib/beanstalk_integration_tests/test_helper.rb, line 14
def timeout(sec, klass=nil)
  return yield(sec) if sec == nil or sec.zero?
  thread = Thread.new { yield(sec) }

  if thread.join(sec).nil?
    java_thread = JRuby.reference(thread)
    thread.kill
    java_thread.native_thread.interrupt
    thread.join(0.15)
    raise (klass || Error), 'execution expired'
  else
    thread.value
  end
end