Created
July 17, 2015 19:58
-
-
Save jbodah/ea80905417f37c43862a to your computer and use it in GitHub Desktop.
assert_completed_in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @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