Skip to content

Commit

Permalink
Merge pull request #95 from NoRedInk/patveith-pv-exponential-backoff
Browse files Browse the repository at this point in the history
Patveith pv exponential backoff
  • Loading branch information
michaelglass authored May 15, 2018
2 parents e14650b + a42546e commit 36f2260
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/rspec/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -149,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

Expand Down
15 changes: 15 additions & 0 deletions spec/lib/rspec/retry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ class SharedError < StandardError; end
end
end

context "with exponential backoff enabled", :retry => 3, :retry_wait => 0.001, :exponential_backoff => true do
context do
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

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
Expand Down

0 comments on commit 36f2260

Please sign in to comment.