spec/logstash/circuit_breaker_spec.rb in logstash-input-lumberjack-1.0.3 vs spec/logstash/circuit_breaker_spec.rb in logstash-input-lumberjack-1.0.4

- old
+ new

@@ -12,47 +12,69 @@ } end subject { LogStash::CircuitBreaker.new("testing", options) } + context "when the breaker is closed" do + it "closed by default" do + expect(subject.closed?).to eq(true) + end - it "closed by default" do - expect(subject.closed?).to eq(true) - end - - context "when having too many errors" do - let(:future_time) { Time.now + 3600 } - before do - subject.execute do - raise DummyErrorTest - end + it "always raise an exception if an errors occur" do + expect { + subject.execute do + raise DummyErrorTest + end + }.to raise_error(LogStash::CircuitBreaker::HalfOpenBreaker) end - it "raised an exception if we have too many errors" do + it "open if we pass the errors threadshold" do expect { subject.execute do raise DummyErrorTest end + }.to raise_error(LogStash::CircuitBreaker::HalfOpenBreaker) + + expect { + subject.execute do + raise DummyErrorTest + end }.to raise_error(LogStash::CircuitBreaker::OpenBreaker) end + end - it "sets the breaker to open" do + context "When the breaker is open" do + let(:future_time) { Time.now + 3600 } + + 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 - expect(Time).to receive(:now).at_least(1).and_return(future_time) + expect(Time).to receive(:now).at_least(2).and_return(future_time) expect(subject.closed?).to eq(true) end it "doesnt run the command" do runned = false begin subject.execute do runned = true end - rescue LogStash::CircuitBreaker::OpenBreaker + rescue LogStash::CircuitBreaker::OpenBreaker end expect(runned).to eq(false) end end