test/retries_test.rb in retries-0.0.3 vs test/retries_test.rb in retries-0.0.4

- old
+ new

@@ -1,12 +1,19 @@ require "scope" require "minitest/autorun" require "timeout" +require "rr" $:.unshift File.join(File.dirname(__FILE__), "../lib") require "retries" +module Scope + class TestCase + include RR::Adapters::MiniTest + end +end + class CustomErrorA < RuntimeError; end class CustomErrorB < RuntimeError; end class RetriesTest < Scope::TestCase setup do @@ -64,16 +71,31 @@ exception_handler_run_times += 1 # Check that the handler is passed the proper exception and attempt number assert_equal exception_handler_run_times, attempt_number assert exception.is_a?(CustomErrorA) end - with_retries(:max_tries => 4, :base_sleep_seconds => 0, :max_sleep_seconds => 0, :handler => handler, - :rescue => CustomErrorA) do + with_retries(:max_tries => 4, :base_sleep_seconds => 0, :max_sleep_seconds => 0, + :handler => handler, :rescue => CustomErrorA) do tries += 1 raise CustomErrorA.new if tries < 4 end assert_equal 4, tries assert_equal 3, exception_handler_run_times + end + + should "pass total elapsed time to :handler upon each handled exception" do + Retries.sleep_enabled = false + fake_time = -10 + stub(Time).now { fake_time += 10 } + handler = Proc.new do |exception, attempt_number, total_delay| + # Check that the handler is passed the proper total delay time + assert_equal fake_time, total_delay + end + tries = 0 + with_retries(:max_tries => 3, :handler => handler, :rescue => CustomErrorA) do + tries += 1 + raise CustomErrorA.new if tries < 3 + end end should "not sleep if Retries.sleep_enabled is false" do Retries.sleep_enabled = false assert_raises(RuntimeError) do # If we get a Timeout::Error, this won't pass.