Sha256: 2cba76d2da1161f92cafc661e63f10db8730ecdaa27b6aa08f731ab84b79230b

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

#####################################################################
# attempt_spec.rb
#
# Tests for the attempt library. You should run this test case via
# the 'rake test' Rakefile task (or just 'rake').
#
# TODO: Test that an Attempt::Warning is raised.
#####################################################################
require 'rspec'
require 'attempt'
require 'stringio'

RSpec.describe Attempt do
  before(:all) do
    $stderr = StringIO.new
  end

  before do
    @tries    = 2
    @interval = 0.1
    @timeout  = 0.1
    @value    = 0
  end

  after do
    @value = 0
  end

  after(:all) do
    $stderr = STDERR
  end

  example 'version constant is set to expected value' do
    expect(Attempt::VERSION).to eq('0.6.3')
    expect(Attempt::VERSION).to be_frozen
  end

  example 'attempt works as expected without arguments' do
    expect{ attempt{ 2 + 2 } }.not_to raise_error
  end

  example 'attempt retries the number of times specified' do
    expect{ attempt(tries: @tries){ @value += 1; raise if @value < 2 } }.not_to raise_error
    expect(@value).to eq(2)
  end

  example 'attempt retries the number of times specified with interval' do
    expect{
      attempt(tries: @tries, interval: @interval){ @value += 1; raise if @value < 2 }
    }.not_to raise_error
    expect(@value).to eq(2)
  end

  example 'attempt retries the number of times specified with interval and timeout' do
    expect{
      attempt(tries: @tries, interval: @interval, timeout: @timeout){ @value += 1; raise if @value < 2 }
    }.not_to raise_error
  end

  example 'attempt raises a timeout error if timeout value is exceeded' do
    expect{ attempt(tries: 1, interval: 1, timeout: @timeout){ sleep 5 } }.to raise_error(Timeout::Error)
  end

  example 'attempt raises exception as expected' do
    expect{ attempt(tries: 2, interval: 2){ raise } }.to raise_error(RuntimeError)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attempt-0.6.3 spec/attempt_spec.rb