Sha256: 18ddcc098b26c5fe6d82b46c290f695e2eb2bc36980b64f238056bf598fc6c76

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require 'spec_helper'
require 'circuit_breaker-ruby'

CircuitBreaker.configure do |cb|
  cb.invocation_timeout = 1
  cb.retry_timeout = 1
  cb.failure_threshold = 1
end

describe CircuitBreaker::Shield do
  let(:circuit_breaker_shield) { CircuitBreaker::Shield.new }

  it 'goes to closed state' do
    circuit_breaker_shield.protect { sleep(0.1) }

    expect(circuit_breaker_shield.send :state).to be(CircuitBreaker::Shield::States::CLOSED)
  end

  it 'goes to open state' do
    no_of_tries = circuit_breaker_shield.failure_threshold * 2
    no_of_failures = no_of_tries * circuit_breaker_shield.config.failure_threshold_percentage
    no_of_success = no_of_tries - no_of_failures
    no_of_success.to_i.times { circuit_breaker_shield.protect { sleep(0.1) } }
    no_of_failures.to_i.times { circuit_breaker_shield.protect { sleep(2) } }

    expect(circuit_breaker_shield.send :state).to be(CircuitBreaker::Shield::States::OPEN)
  end

  it 'goes to half open state' do
    no_of_tries = circuit_breaker_shield.failure_threshold * 2
    no_of_failures = no_of_tries * circuit_breaker_shield.config.failure_threshold_percentage
    no_of_success = no_of_tries - no_of_failures
    no_of_success.to_i.times { circuit_breaker_shield.protect { sleep(0.1) } }
    no_of_failures.to_i.times { circuit_breaker_shield.protect { sleep(2) } }
    sleep(1)

    expect(circuit_breaker_shield.send :state).to be(CircuitBreaker::Shield::States::HALF_OPEN)
  end

  it 'raises CircuitBreaker::Shield::Open' do
     no_of_tries = circuit_breaker_shield.failure_threshold * 2
    no_of_failures = no_of_tries * circuit_breaker_shield.config.failure_threshold_percentage
    no_of_success = no_of_tries - no_of_failures
    no_of_success.to_i.times { circuit_breaker_shield.protect { sleep(0.1) } }

    expect { (no_of_failures.to_i + 1).times { circuit_breaker_shield.protect { sleep(2) } } }.to(
      raise_error(CircuitBreaker::Open)
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
circuit_breaker-ruby-0.1.1 spec/circuit_breaker/shield_spec.rb
circuit_breaker-ruby-0.1 spec/circuit_breaker/shield_spec.rb