Skip to content

Instantly share code, notes, and snippets.

@jbodah
Created July 17, 2015 19:58
Show Gist options
  • Save jbodah/ea80905417f37c43862a to your computer and use it in GitHub Desktop.
Save jbodah/ea80905417f37c43862a to your computer and use it in GitHub Desktop.
assert_completed_in
# @example
# assert_completed_in 3.seconds do
# MyJob.perform
# end
def assert_completed_in(seconds, message: nil, strategy: :timeout)
case strategy
when :timeout
begin
Timeout.timeout(seconds) { yield }
rescue Timeout::Error
fail message || "took longer than #{seconds} seconds"
end
when :benchmark
b = Benchmark.measure { yield }.real
assert b < seconds, "took longer than #{seconds} seconds (actual: #{b})"
else
raise 'invalid strategy'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment