Sha256: b85ab2372e19d135950dfeae2f02be9b8b8d726e3d785c02480da082a2fc21bc

Contents?: true

Size: 1.58 KB

Versions: 66

Compression:

Stored size: 1.58 KB

Contents

require 'vm_shepherd/retry_helper'

module VmShepherd
  RSpec.describe RetryHelper do
    subject(:retry_helper) { TestRetryHelper.new }

    class TestRetryHelper
      include RetryHelper
    end

    before do
      allow(retry_helper).to receive(:sleep) # speed up retry logic
    end

    it 'calls the given block 10 times by default' do
      sixty_times = [true] + [false] * 9
      expect {
        retry_helper.retry_until { sixty_times.pop }
      }.not_to raise_error

      sixty_one_times = [true] + [false] * 60
      expect {
        retry_helper.retry_until { sixty_one_times.pop }
      }.to raise_error(RetryHelper::RetryLimitExceeded)
    end

    it 'throws an error if the block never returns true' do
      expect {
        retry_helper.retry_until { false }
      }.to raise_error(RetryHelper::RetryLimitExceeded)
    end

    it 'returns early if the block returns a truthy value' do
      counter = 0
      retry_helper.retry_until do
        counter += 1
        counter == 6
      end
      expect(counter).to eq(6)
    end

    it 'returns the value of the block' do
      expect(retry_helper.retry_until { 'retrying' }).to eq('retrying')
    end

    it 'calls the given block the given number of times' do
      forty_two_times = [true] + [false] * 41
      expect {
        retry_helper.retry_until(retry_limit: 42) { forty_two_times.pop }
      }.not_to raise_error

      forty_three_times = [true] + [false] * 42
      expect {
        retry_helper.retry_until(retry_limit: 42) { forty_three_times.pop }
      }.to raise_error(RetryHelper::RetryLimitExceeded)
    end
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
vm_shepherd-3.1.4 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.1.3 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.1.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.1.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.1.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.8 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.7 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.6 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.5 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.4 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.7 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.3 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.6 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-3.0.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-2.0.3 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-2.0.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-2.0.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-2.0.0 spec/vm_shepherd/retry_helper_spec.rb