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-1.12.5 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.4 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.3 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.12.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.8 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.7 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.6 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.5 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.4 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.3 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.11.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.10.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.10.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.9.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.9.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.8.4 spec/vm_shepherd/retry_helper_spec.rb