Sha256: d8c47ae57d2d68d0b8874076d3f1b393ae064417965b19ffb6fc185b5c8db0e0

Contents?: true

Size: 1.97 KB

Versions: 16

Compression:

Stored size: 1.97 KB

Contents

require_relative "../../spec_helper"
require "logstash/inputs/beats_support/circuit_breaker"

class DummyErrorTest < StandardError; end

describe LogStash::Inputs::BeatsSupport::CircuitBreaker do
  let(:error_threshold) { 1 }
  let(:options) do 
    {
      :exceptions => [DummyErrorTest],
      :error_threshold => error_threshold 
    }
  end

  subject { described_class.new("testing", options) }

  context "when the breaker is closed" do
    it "closed by default" do
      expect(subject.closed?).to eq(true)
    end

    it "always raise an exception if an errors occur" do
      expect {
        subject.execute do
          raise DummyErrorTest
        end
      }.to raise_error(LogStash::Inputs::BeatsSupport::CircuitBreaker::HalfOpenBreaker)
    end

    it "open if we pass the errors threadshold" do
      expect {
        subject.execute do
          raise DummyErrorTest
        end
      }.to raise_error(LogStash::Inputs::BeatsSupport::CircuitBreaker::HalfOpenBreaker)

      expect {
        subject.execute do
          raise DummyErrorTest
        end
      }.to raise_error(LogStash::Inputs::BeatsSupport::CircuitBreaker::OpenBreaker)
    end
  end

  context "When the breaker is open" do
    let(:retry_time) { 2 }
    let(:options) { super.merge(:time_before_retry => retry_time) }

    before do
      # trip the breaker
      (error_threshold + 1).times do
        begin
          subject.execute do
            raise DummyErrorTest
          end
        rescue
        end
      end
    end

    it "#closed? should return false" do
      expect(subject.closed?).to eq(false)
    end

    it "resets the breaker after the time before retry" do
      sleep(retry_time + 1)
      expect(subject.closed?).to eq(true)
    end

    it "doesnt run the command" do
      runned = false

      begin
        subject.execute do
          runned = true
        end
      rescue LogStash::Inputs::BeatsSupport::CircuitBreaker::OpenBreaker 
      end

      expect(runned).to eq(false)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
logstash-input-beats-3.0.4 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.9 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-3.0.3 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-3.0.2 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-3.0.1 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-3.0.0 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.8 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.7 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.5 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.3 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.2 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.2.0 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.1.4 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.1.3 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.1.2 spec/inputs/beats_support/circuit_breaker_spec.rb
logstash-input-beats-2.1.1 spec/inputs/beats_support/circuit_breaker_spec.rb