Skip to content

Commit

Permalink
Move DEFAULT_THRESHOLD into the data store
Browse files Browse the repository at this point in the history
  • Loading branch information
tfausak committed Aug 26, 2014
1 parent df23afe commit 1730228
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 36 deletions.
9 changes: 0 additions & 9 deletions lib/stoplight.rb
Expand Up @@ -17,9 +17,6 @@ module Stoplight
# @return [Gem::Version]
VERSION = Gem::Version.new('0.2.1')

# @return [Integer]
DEFAULT_THRESHOLD = 3

@data_store = DataStore::Memory.new
@notifiers = [Notifier::StandardError.new]

Expand Down Expand Up @@ -47,11 +44,5 @@ def red?(name)
def yellow?(name)
data_store.color(name) == DataStore::COLOR_YELLOW
end

# @param name [String]
# @return [Integer]
def threshold(name)
data_store.threshold(name) || DEFAULT_THRESHOLD
end
end
end
3 changes: 3 additions & 0 deletions lib/stoplight/data_store.rb
Expand Up @@ -23,5 +23,8 @@ module DataStore

# @return [Integer]
DEFAULT_TIMEOUT = 5 * 60

# @return [Integer]
DEFAULT_THRESHOLD = 3
end
end
2 changes: 1 addition & 1 deletion lib/stoplight/data_store/base.rb
Expand Up @@ -106,7 +106,7 @@ def _color(failures, state, threshold, timeout)
return COLOR_GREEN if state == STATE_LOCKED_GREEN
return COLOR_RED if state == STATE_LOCKED_RED

threshold = threshold ? threshold.to_i : Stoplight::DEFAULT_THRESHOLD
threshold = threshold ? threshold.to_i : DEFAULT_THRESHOLD
return COLOR_GREEN if failures.size < threshold

# If the threshold is 0, the light is always red.
Expand Down
2 changes: 1 addition & 1 deletion lib/stoplight/data_store/memory.rb
Expand Up @@ -71,7 +71,7 @@ def set_state(name, state)
# @group Threshold

def threshold(name)
all_thresholds[name]
all_thresholds[name] || DEFAULT_THRESHOLD
end

def set_threshold(name, threshold)
Expand Down
4 changes: 2 additions & 2 deletions lib/stoplight/data_store/redis.rb
Expand Up @@ -84,8 +84,8 @@ def set_state(name, state)
# @group Threshold

def threshold(name)
value = @redis.hget(thresholds_key, name)
Integer(value) if value
threshold = @redis.hget(thresholds_key, name)
threshold ? threshold.to_i : DEFAULT_THRESHOLD
end

def set_threshold(name, threshold)
Expand Down
2 changes: 1 addition & 1 deletion lib/stoplight/light.rb
Expand Up @@ -85,7 +85,7 @@ def red?
end

def threshold
Stoplight.threshold(name)
Stoplight.data_store.threshold(name)
end

def timeout
Expand Down
5 changes: 3 additions & 2 deletions spec/stoplight/light_spec.rb
Expand Up @@ -27,7 +27,8 @@
subject(:result) { light.run }

it 'syncs settings' do
expect(Stoplight.data_store.threshold(name)).to be nil
expect(Stoplight.data_store.threshold(name)).to eql(
Stoplight::DataStore::DEFAULT_THRESHOLD)
result
expect(Stoplight.data_store.threshold(name)).to eql(
light.threshold)
Expand Down Expand Up @@ -249,7 +250,7 @@
subject(:result) { light.threshold }

it 'uses the default threshold' do
expect(result).to eql(Stoplight::DEFAULT_THRESHOLD)
expect(result).to eql(Stoplight::DataStore::DEFAULT_THRESHOLD)
end
end
end
20 changes: 1 addition & 19 deletions spec/stoplight_spec.rb
Expand Up @@ -101,7 +101,7 @@

context 'with failures' do
before do
described_class.threshold(name).times do
described_class.data_store.threshold(name).times do
described_class.data_store.record_failure(name, nil)
end
end
Expand Down Expand Up @@ -137,22 +137,4 @@
end
end
end

describe '.threshold' do
subject(:result) { described_class.threshold(name) }

it 'uses the default threshold' do
expect(result).to eql(Stoplight::DEFAULT_THRESHOLD)
end

context 'with a custom threshold' do
let(:threshold) { rand(10) }

before { described_class.data_store.set_threshold(name, threshold) }

it 'uses the threshold' do
expect(result).to eql(threshold)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/support/data_store.rb
Expand Up @@ -163,7 +163,7 @@
subject(:result) { data_store.threshold(name) }

it 'returns nil' do
expect(result).to eql(nil)
expect(result).to eql(Stoplight::DataStore::DEFAULT_THRESHOLD)
end

context 'with a threshold' do
Expand Down

0 comments on commit 1730228

Please sign in to comment.