Sha256: 71ce98d5b4517624732188c4cd598ccd23383879b1516f82f6c20d15abe293c9

Contents?: true

Size: 1.58 KB

Versions: 15

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 60 times by default' do
      sixty_times = [true] + [false] * 59
      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

15 entries across 15 versions & 1 rubygems

Version Path
vm_shepherd-1.7.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.7.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.6.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.6.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.5.4 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.5.3 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.5.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.5.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.5.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.4.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.3.2 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.3.1 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.3.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.2.0 spec/vm_shepherd/retry_helper_spec.rb
vm_shepherd-1.1.1 spec/vm_shepherd/retry_helper_spec.rb