Skip to content

Instantly share code, notes, and snippets.

@seancribbs
Forked from myronmarston/rspec_retries.rb
Created February 1, 2012 19:58
Show Gist options
  • Select an option

  • Save seancribbs/1718985 to your computer and use it in GitHub Desktop.

Select an option

Save seancribbs/1718985 to your computer and use it in GitHub Desktop.

Revisions

  1. Sean Cribbs revised this gist Feb 2, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_retries.rb
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ def run_with_retries(example_to_run, retries)
    config.after(:suite) do
    formatter = RSpec.configuration.formatters.first
    color = lambda {|tint, msg| formatter.send(tint, msg) }
    retried_examples = RSpec.world.example_groups.flat_map do |g|
    retried_examples = RSpec.world.example_groups.map do |g|
    g.descendants.map do |d|
    d.filtered_examples.select {|e| e.metadata[:sometimes] && e.metadata[:retried] > 1 }
    end
  2. Sean Cribbs revised this gist Feb 2, 2012. 1 changed file with 40 additions and 10 deletions.
    50 changes: 40 additions & 10 deletions rspec_retries.rb
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,46 @@
    # Emulates the QuickCheck ?SOMETIMES macro.

    RSpec.configure do |config|
    config.around(:each, :max_retries => lambda { |m| !!m }) do |example|
    retries = example.metadata[:max_retries]
    begin
    example.run
    rescue => e
    retries -= 1
    retry if retries >= 0
    raise
    module Sometimes
    def run_with_retries(example_to_run, retries)
    self.example.metadata[:retries] ||= retries
    retries.times do |t|
    self.example.metadata[:retried] = t + 1
    self.example.instance_variable_set(:@exception, nil)
    example_to_run.run
    break unless self.example.exception
    end
    if e = self.example.exception
    new_exception = e.exception(e.message + "[Retried #{retries} times]")
    new_exception.set_backtrace e.backtrace
    self.example.instance_variable_set(:@exception, new_exception)
    end
    end
    end

    RSpec::Core::ExampleGroup.define_example_method :sometimes, :max_retries => 3
    RSpec.configure do |config|
    config.include Sometimes
    config.alias_example_to :sometimes, :sometimes => true
    config.add_setting :sometimes_retry_count, :default => 3

    config.around(:each, :sometimes => true) do |example|
    retries = example.metadata[:retries] || RSpec.configuration.sometimes_retry_count
    run_with_retries(example, retries)
    end

    config.after(:suite) do
    formatter = RSpec.configuration.formatters.first
    color = lambda {|tint, msg| formatter.send(tint, msg) }
    retried_examples = RSpec.world.example_groups.flat_map do |g|
    g.descendants.map do |d|
    d.filtered_examples.select {|e| e.metadata[:sometimes] && e.metadata[:retried] > 1 }
    end
    end.flatten
    formatter.message color[retried_examples.empty? ? :green : :yellow, "\n\nRetried examples: #{retried_examples.count}"]
    unless retried_examples.empty?
    retried_examples.each do |e|
    formatter.message " #{e.full_description}"
    formatter.message(color[:yellow, " [#{e.metadata[:retried]}/#{e.metadata[:retries]}] "] + formatter.class.relative_path(e.location))
    end
    end
    end
    end
  3. Sean Cribbs revised this gist Feb 1, 2012. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions rspec_retries.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Emulates the QuickCheck ?SOMETIMES macro.

    RSpec.configure do |config|
    config.around(:each, :max_retries => lambda { |m| !!m }) do |example|
    @@ -12,8 +13,4 @@
    end
    end

    describe "Integration" do
    it 'does something that fails intermittently', :max_retries => 3 do
    # ...
    end
    end
    RSpec::Core::ExampleGroup.define_example_method :sometimes, :max_retries => 3
  4. @myronmarston myronmarston created this gist Feb 1, 2012.
    19 changes: 19 additions & 0 deletions rspec_retries.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@

    RSpec.configure do |config|
    config.around(:each, :max_retries => lambda { |m| !!m }) do |example|
    retries = example.metadata[:max_retries]
    begin
    example.run
    rescue => e
    retries -= 1
    retry if retries >= 0
    raise
    end
    end
    end

    describe "Integration" do
    it 'does something that fails intermittently', :max_retries => 3 do
    # ...
    end
    end