Skip to content

Instantly share code, notes, and snippets.

@darrenboyd
Created June 7, 2009 22:45
Show Gist options
  • Save darrenboyd/125523 to your computer and use it in GitHub Desktop.
Save darrenboyd/125523 to your computer and use it in GitHub Desktop.
require 'timeout'
def test_timeout(timeout_timeout, count, &block)
pass = timeout = error = 0
count.times do |i|
begin
Timeout::timeout(timeout_timeout, &block)
pass += 1
rescue Timeout::Error => e
timeout += 1
rescue Timeout::ExitException => e
error += 1
end
end
[pass, timeout, error]
end
# With JRuby version 1.3, I started seeing odd errors from
# Timeout::timeout. The output from this little app was
# inconsistent. It would usually pass 90-95% of the time.
#
# I played around with setting the timeout value (0.9999
# below) to 1. After that, I could no longer get anything
# but 'pass' from this test for any value.
#
puts "Simple quick loop test..."
pass, timeout, error = test_timeout(0.9999, 1000) { 1.times { |i| i }; true }
puts "Pass: #{pass}"
puts "Timeout: #{timeout}"
puts "Error: #{error}"
puts "Simply return true test..."
pass, timeout, error = test_timeout(0.9999, 1000) { true }
puts "Pass: #{pass}"
puts "Timeout: #{timeout}"
puts "Error: #{error}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment