spec/retriable_spec.rb in retriable-2.0.0.beta3 vs spec/retriable_spec.rb in retriable-2.0.0.beta4

- old
+ new

@@ -14,62 +14,66 @@ end end it "stops at first attempt if the block does not raise an exception" do attempts = 0 - subject.retry do + subject.retriable do attempts += 1 end attempts.must_equal 1 end - it "raises a LocalJumpError if retry is not given a block" do + it "raises a LocalJumpError if #retriable is not given a block" do -> do - subject.retry on: EOFError + subject.retriable on: EOFError end.must_raise LocalJumpError + + -> do + subject.retriable on: EOFError, timeout: 2 + end.must_raise LocalJumpError end - describe "retry block of code raising EOFError with no arguments" do + describe "#retriable block of code raising EOFError with no arguments" do before do @attempts = 0 - subject.retry do + subject.retriable do @attempts += 1 raise EOFError.new if @attempts < 3 end end it "uses exponential backoff" do @attempts.must_equal 3 end end - it "retry on custom exception and re-raises the exception" do + it "#retriable on custom exception and re-raises the exception" do -> do - subject.retry on: TestError do + subject.retriable on: TestError do raise TestError.new end end.must_raise TestError end - it "retry with 10 max tries" do + it "#retriable with 10 max tries" do attempts = 0 - subject.retry( + subject.retriable( max_tries: 10 ) do attempts += 1 raise EOFError.new if attempts < 10 end attempts.must_equal 10 end - it "retry will timeout after 1 second" do + it "#retriable will timeout after 1 second" do -> do - subject.retry timeout: 1 do + subject.retriable timeout: 1 do sleep 2 end end.must_raise Timeout::Error end @@ -81,11 +85,11 @@ exception.class.must_equal ArgumentError @time_table[attempt] = next_interval end -> do - Retriable.retry( + Retriable.retriable( on: [EOFError, ArgumentError], on_retry: handler, rand_factor: 0.0, max_tries: 9 ) do @@ -103,22 +107,22 @@ @time_table[7].between?(2.846, 8.538).must_equal true @time_table[8].between?(4.269, 12.807).must_equal true @time_table[9].between?(6.403, 19.210).must_equal true end - describe "retries with an on_retry handler, 6 max retries, and a 0.0 rand_factor" do + describe "retries with an on_#retriable handler, 6 max retries, and a 0.0 rand_factor" do before do max_tries = 6 @attempts = 0 @time_table = {} handler = ->(exception, attempt, elapsed_time, next_interval) do exception.class.must_equal ArgumentError @time_table[attempt] = next_interval end - Retriable.retry( + Retriable.retriable( on: [EOFError, ArgumentError], on_retry: handler, rand_factor: 0.0, max_tries: max_tries ) do @@ -140,20 +144,20 @@ 5 => 2.53125 }) end end - it "retry has a max interval of 1.5 seconds" do + it "#retriable has a max interval of 1.5 seconds" do max_tries = 6 attempts = 0 time_table = {} handler = ->(exception, attempt, elapsed_time, next_interval) do time_table[attempt] = next_interval end - subject.retry( + subject.retriable( on: EOFError, on_retry: handler, rand_factor: 0.0, max_tries: max_tries, max_interval: 1.5 @@ -169,11 +173,11 @@ 4 => 1.5, 5 => 1.5 }) end - it "retries with defined intervals" do + it "#retriable with defined intervals" do intervals = [ 0.5, 0.75, 1.125, 1.5, @@ -184,11 +188,11 @@ handler = ->(exception, attempt, elapsed_time, next_interval) do time_table[attempt] = next_interval end -> do - subject.retry( + subject.retriable( on: EOFError, on_retry: handler, intervals: intervals ) do raise EOFError.new @@ -202,29 +206,70 @@ 4 => 1.5, 5 => 1.5 }) end - it "can call #retriable in the global" do + it "#retriable with a hash exception where the value is an exception message pattern" do + e = -> do + subject.retriable on: { TestError => /something went wrong/ } do + raise TestError.new('something went wrong') + end + end.must_raise TestError + + e.message.must_equal "something went wrong" + end + + it "#retriable with a hash exception list where the values are exception message patterns" do + attempts = 0 + tries = [] + handler = ->(exception, attempt, elapsed_time, next_interval) do + tries[attempt] = exception + end + + e = -> do + subject.retriable max_tries: 4, on: { EOFError => nil, TestError => [/foo/, /bar/] }, on_retry: handler do + attempts += 1 + case attempts + when 1 + raise TestError.new('foo') + when 2 + raise TestError.new('bar') + when 3 + raise EOFError.new + else + raise TestError.new('crash') + end + end + end.must_raise TestError + + e.message.must_equal "crash" + tries[1].class.must_equal TestError + tries[1].message.must_equal "foo" + tries[2].class.must_equal TestError + tries[2].message.must_equal "bar" + tries[3].class.must_equal EOFError + end + + it "#retriable can be called in the global scope" do -> do retriable do puts "should raise NoMethodError" end end.must_raise NoMethodError require_relative "../lib/retriable/core_ext/kernel" - i = 0 + attempts = 0 retriable do - i += 1 - raise EOFError.new if i < 3 + attempts += 1 + raise EOFError.new if attempts < 3 end - i.must_equal 3 + attempts.must_equal 3 end end - it "retry runs for a max elapsed time of 2 seconds" do + it "#retriable runs for a max elapsed time of 2 seconds" do subject.configure do |c| c.sleep_disabled = false end subject.config.sleep_disabled.must_equal false @@ -235,10 +280,10 @@ handler = ->(exception, attempt, elapsed_time, next_interval) do time_table[attempt] = elapsed_time end -> do - subject.retry( + subject.retriable( base_interval: 1.0, multiplier: 1.0, rand_factor: 0.0, max_elapsed_time: 2.0, on_retry: handler