From c70f4fa466331e4ac5ef45b5481e04d94d3b562a Mon Sep 17 00:00:00 2001 From: Patrick Veith Date: Mon, 7 May 2018 11:49:42 -0400 Subject: [PATCH 1/3] Adds exponential backoff --- lib/rspec/retry.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/rspec/retry.rb b/lib/rspec/retry.rb index 71152f5..fcebfd4 100644 --- a/lib/rspec/retry.rb +++ b/lib/rspec/retry.rb @@ -9,6 +9,7 @@ def self.setup config.add_setting :verbose_retry, :default => false config.add_setting :default_retry_count, :default => 1 config.add_setting :default_sleep_interval, :default => 0 + config.add_setting :exponential_backoff, :default => false config.add_setting :clear_lets_on_failure, :default => true config.add_setting :display_try_failure_messages, :default => false @@ -76,8 +77,12 @@ def clear_lets end def sleep_interval - ex.metadata[:retry_wait] || - RSpec.configuration.default_sleep_interval + if ex.metadata[:exponential_backoff] + 2**(current_example.attempts-1) * ex.metadata[:retry_wait] + else + ex.metadata[:retry_wait] || + RSpec.configuration.default_sleep_interval + end end def exceptions_to_hard_fail From f2c3a83e13f88dad49d82fc2e9a0c796840ad42c Mon Sep 17 00:00:00 2001 From: Patrick Veith Date: Mon, 14 May 2018 16:05:40 -0400 Subject: [PATCH 2/3] Adds test to ensure that retry still works --- spec/lib/rspec/retry_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/lib/rspec/retry_spec.rb b/spec/lib/rspec/retry_spec.rb index 16c6921..77117cc 100644 --- a/spec/lib/rspec/retry_spec.rb +++ b/spec/lib/rspec/retry_spec.rb @@ -83,6 +83,17 @@ class SharedError < StandardError; end end end + context "with exponential backoff enabled", :retry => 3, :retry_wait => 5, :exponential_backoff => true do + context do + before(:all) { set_expectations([false, false, true]) } + + it 'should run example until :retry times', :retry => 3 do + expect(true).to be(shift_expectation) + expect(count).to eq(3) + end + end + end + describe "with a list of exceptions to immediately fail on", :retry => 2, :exceptions_to_hard_fail => [HardFailError] do context "the example throws an exception contained in the hard fail list" do it "does not retry" do From a42546ea2d59a915041db7148081f1285dd560cf Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 15 May 2018 09:20:45 +0200 Subject: [PATCH 3/3] sleep interval supports floats; exponential backoff test is faster --- lib/rspec/retry.rb | 2 +- spec/lib/rspec/retry_spec.rb | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/rspec/retry.rb b/lib/rspec/retry.rb index fcebfd4..caccbc7 100644 --- a/lib/rspec/retry.rb +++ b/lib/rspec/retry.rb @@ -154,7 +154,7 @@ def run example.example_group_instance.instance_exec(example, &RSpec.configuration.retry_callback) end - sleep sleep_interval if sleep_interval.to_i > 0 + sleep sleep_interval if sleep_interval.to_f > 0 end end diff --git a/spec/lib/rspec/retry_spec.rb b/spec/lib/rspec/retry_spec.rb index 77117cc..b528c63 100644 --- a/spec/lib/rspec/retry_spec.rb +++ b/spec/lib/rspec/retry_spec.rb @@ -83,13 +83,17 @@ class SharedError < StandardError; end end end - context "with exponential backoff enabled", :retry => 3, :retry_wait => 5, :exponential_backoff => true do + context "with exponential backoff enabled", :retry => 3, :retry_wait => 0.001, :exponential_backoff => true do context do - before(:all) { set_expectations([false, false, true]) } + before(:all) do + set_expectations([false, false, true]) + @start_time = Time.now + end it 'should run example until :retry times', :retry => 3 do expect(true).to be(shift_expectation) expect(count).to eq(3) + expect(Time.now - @start_time).to be >= (0.001) end end end