Module: Timeout

Included in:
BeanstalkIntegrationTest
Defined in:
lib/beanstalk_integration_tests/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#timeout(sec, klass = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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