Thanks. This applies to a bunch of similar tests on the same module, and there are a bunch of different assertions that seem to be flaky in this case, so I’ve just cooked up a macro like this:
require "spec"
macro with_retries(max_attempts, &block)
{{max_attempts}}.times do |%attempt|
begin
{{ block.body }}
break
rescue ex : Spec::AssertionFailed
if (%attempt + 1) == {{max_attempts}}
puts "Attempt ##{%attempt + 1} failed. FAIL."
raise ex
else
puts "Attempt ##{%attempt + 1} failed. Retrying..."
end
end
end
end
describe "Flaky Test" do
it "works 10% of the time" do
with_retries(5) do
Random.rand.should be < 0.1 # assertion is true 10% of the time
end
end
end
By catching Spec::AssertionFailed
, this is pretty close to what I want!
My issue now is that this does not play nicely with the truncate_db
tag that I have to clear the Redis state before each example. (Because all of these attempts are within a single around_each
run.) But this might point to the value of the with_clean_db
helper method you suggested for me here, since I could do with retries(5) { with_clean_db { ... } }
. Thanks @Blacksmoke16