Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose CircuitBreaker#name and CircuitBreaker#state methods #198

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/stoplight/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Builder
def_delegator :build, :with_error_handler
def_delegator :build, :with_fallback
def_delegator :build, :color
def_delegator :build, :name
def_delegator :build, :state
def_delegator :build, :run
def_delegator :build, :lock
def_delegator :build, :unlock
Expand Down
10 changes: 10 additions & 0 deletions lib/stoplight/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def with_fallback(&fallback)
raise NotImplementedError
end

# @return [String] one of +locked_green+, +locked_red+, and +unlocked+
def state
raise NotImplementedError
end

# @return [String] the light's name
def name
raise NotImplementedError
end

# Returns current color:
# * +Stoplight::Color::GREEN+ -- circuit breaker is closed
# * +Stoplight::Color::RED+ -- circuit breaker is open
Expand Down
6 changes: 6 additions & 0 deletions lib/stoplight/light/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
module Stoplight
class Light
module Runnable # rubocop:disable Style/Documentation
# @return [String]
def state
_, state = failures_and_state
state
end

# @return [String]
def color
failures, state = failures_and_state
Expand Down
8 changes: 8 additions & 0 deletions spec/stoplight/circuit_breaker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
expect { circuit_breaker.color }.to raise_error(NotImplementedError)
end

specify '#state' do
expect { circuit_breaker.state }.to raise_error(NotImplementedError)
end

specify '#name' do
expect { circuit_breaker.name }.to raise_error(NotImplementedError)
end

specify '#run' do
expect { circuit_breaker.run {} }.to raise_error(NotImplementedError)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/stoplight/light/runnable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ def random_string
context 'with memory data store' do
let(:data_store) { Stoplight::DataStore::Memory.new }

it_behaves_like 'Stoplight::Light::Runnable#state'
it_behaves_like 'Stoplight::Light::Runnable#color'
it_behaves_like 'Stoplight::Light::Runnable#run'
end

context 'with redis data store', :redis do
let(:data_store) { Stoplight::DataStore::Redis.new(redis) }

it_behaves_like 'Stoplight::Light::Runnable#state'
it_behaves_like 'Stoplight::Light::Runnable#color'
it_behaves_like 'Stoplight::Light::Runnable#run'
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/light/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require_relative 'runnable/color'
require_relative 'runnable/run'
require_relative 'runnable/state'
31 changes: 31 additions & 0 deletions spec/support/light/runnable/state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

RSpec.shared_examples 'Stoplight::Light::Runnable#state' do
subject(:light) { Stoplight::Light.new(name) }

let(:name) { random_string }

it 'is initially unlocked' do
expect(light.state).to eql(Stoplight::State::UNLOCKED)
end

context 'when its locked green' do
before do
light.data_store.set_state(light, Stoplight::State::LOCKED_GREEN)
end

it 'is locked green' do
expect(light.state).to eql(Stoplight::State::LOCKED_GREEN)
end
end

context 'when its locked red' do
before do
light.data_store.set_state(light, Stoplight::State::LOCKED_RED)
end

it 'is locked red' do
expect(light.state).to eql(Stoplight::State::LOCKED_RED)
end
end
end