circuit_breakage 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d59c7f0387657741b8cdb896c9fbc9bdeb130c09
4
- data.tar.gz: d54f9a2a8c87fbb41c6d47d17d89e8c603b28062
3
+ metadata.gz: d643902ba2ada91222c75da26be3426d55eb5db4
4
+ data.tar.gz: f8f3a7c49ec02514e4bd54d54f401c9d257e6430
5
5
  SHA512:
6
- metadata.gz: 36fd7a9668294099a94db646b5337bfa006ac50211113a357b2c37ad6b8be0b2007264e66a342280880630dcae20df289fcbada91f729eb96b2989cd3a5fe8fd
7
- data.tar.gz: 3624cd0493809f40458e82d64aeef20ff3d55be24063eff796ae339896ba0a0ef24c2b2b8c1c1058ef50c043ff07725405beddb673490cbfa0834d3975ff505d
6
+ metadata.gz: b539db6b88213831b37ec97b00c317b088a87558130c6f2533a2360bae3b3e9b1139a3725be313e88c46f27b4eba55fb9b49c23efe5604a17694c5d2690a587f
7
+ data.tar.gz: 78ab5583212c28a9368168cc4425c96b6ccf44b2afe5bfa791aa93846bbff3bb130ed942d4ee919c6f6d47fd64fc547ec040816cd7e31189578b6fcee49ea797
@@ -21,7 +21,7 @@ module CircuitBreakage
21
21
  self.duration = DEFAULT_DURATION
22
22
  self.timeout = DEFAULT_TIMEOUT
23
23
  self.failure_count ||= 0
24
- self.last_failed ||= Time.at(0)
24
+ self.last_failed ||= 0
25
25
  self.state ||= 'closed'
26
26
  end
27
27
 
@@ -58,7 +58,7 @@ module CircuitBreakage
58
58
  end
59
59
 
60
60
  def time_to_retry?
61
- Time.now >= self.last_failed + self.duration
61
+ Time.now.to_i >= self.last_failed + self.duration
62
62
  end
63
63
 
64
64
  def handle_success
@@ -67,7 +67,7 @@ module CircuitBreakage
67
67
  end
68
68
 
69
69
  def handle_failure(error)
70
- self.last_failed = Time.now
70
+ self.last_failed = Time.now.to_i
71
71
  self.failure_count += 1
72
72
  if self.failure_count >= self.failure_threshold
73
73
  self.state = 'open'
@@ -17,16 +17,28 @@ module CircuitBreakage
17
17
  super(block)
18
18
  end
19
19
 
20
- [:state, :failure_count, :last_failed].each do |attr|
21
- attr_key = "#{@key}/attrs/#{attr}"
20
+ def state
21
+ @connection.get("#{@key}/attrs/state")
22
+ end
22
23
 
23
- define_method(attr) do
24
- @connection.get(attr_key)
25
- end
24
+ def state=(value)
25
+ @connection.set("#{@key}/attrs/state", value)
26
+ end
26
27
 
27
- define_method("#{attr}=") do |value|
28
- @connection.set(attr_key, value)
29
- end
28
+ def failure_count
29
+ @connection.get("#{@key}/attrs/failure_count").to_i
30
+ end
31
+
32
+ def failure_count=(value)
33
+ @connection.set("#{@key}/attrs/failure_count", value)
34
+ end
35
+
36
+ def last_failed
37
+ @connection.get("#{@key}/attrs/last_failed").to_i
38
+ end
39
+
40
+ def last_failed=(value)
41
+ @connection.set("#{@key}/attrs/last_failed", value)
30
42
  end
31
43
 
32
44
  private
@@ -1,3 +1,3 @@
1
1
  module CircuitBreakage
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/spec/breaker_spec.rb CHANGED
@@ -64,7 +64,7 @@ module CircuitBreakage
64
64
  before { breaker.state = 'open' }
65
65
 
66
66
  context 'before the retry_time' do
67
- before { breaker.last_failed = Time.now - breaker.duration + 30 }
67
+ before { breaker.last_failed = Time.now.to_i - breaker.duration + 30 }
68
68
 
69
69
  it 'raises CircuitBreakage::CircuitOpen' do
70
70
  expect { breaker.call(arg) }.to raise_error(CircuitOpen)
@@ -72,7 +72,7 @@ module CircuitBreakage
72
72
  end
73
73
 
74
74
  context 'after the retry time' do
75
- before { breaker.last_failed = Time.now - breaker.duration - 30 }
75
+ before { breaker.last_failed = Time.now.to_i - breaker.duration - 30 }
76
76
 
77
77
  it 'calls the block' do
78
78
  expect(breaker.call(arg)).to eq arg
@@ -30,7 +30,7 @@ module CircuitBreakage
30
30
  context 'and the call fails' do
31
31
  let(:block) { ->(_) { raise 'some error' } }
32
32
 
33
- it { is_expected.to change { breaker.failure_count }.by(1) }
33
+ it { is_expected.to change { breaker.failure_count.to_i }.by(1) }
34
34
  it { is_expected.to change { breaker.last_failed } }
35
35
 
36
36
  context 'and the failure count exceeds the failure threshold' do
@@ -45,7 +45,7 @@ module CircuitBreakage
45
45
  before { breaker.timeout = 0.1 }
46
46
 
47
47
  it 'counts as a failure' do
48
- expect { breaker.call(arg) rescue nil }.to change { breaker.failure_count }.by(1)
48
+ expect { breaker.call(arg) rescue nil }.to change { breaker.failure_count.to_i }.by(1)
49
49
  end
50
50
  end
51
51
  end
@@ -54,7 +54,7 @@ module CircuitBreakage
54
54
  before { breaker.state = 'open' }
55
55
 
56
56
  context 'before the retry_time' do
57
- before { breaker.last_failed = Time.now - breaker.duration + 30 }
57
+ before { breaker.last_failed = Time.now.to_i - breaker.duration + 30 }
58
58
 
59
59
  it 'raises CircuitBreakage::CircuitOpen' do
60
60
  expect { breaker.call(arg) }.to raise_error(CircuitOpen)
@@ -63,7 +63,7 @@ module CircuitBreakage
63
63
 
64
64
  context 'after the retry time' do
65
65
  it 'calls the block' do
66
- breaker.last_failed = Time.at(0)
66
+ breaker.last_failed = 0
67
67
  expect(breaker.call(arg)).to eq arg
68
68
  end
69
69
 
@@ -83,7 +83,8 @@ class MockRedis
83
83
  end
84
84
 
85
85
  def get(key)
86
- @stored_data[key]
86
+ # Redis get() always returns values as strings.
87
+ @stored_data[key].to_s
87
88
  end
88
89
 
89
90
  def set(key, val)
@@ -100,8 +101,8 @@ class MockRedis
100
101
  end
101
102
 
102
103
  def getset(key, val)
103
- old_val = @stored_data[key]
104
- @stored_data[key] = val
104
+ old_val = get(key)
105
+ set(key, val)
105
106
  return old_val
106
107
  end
107
108
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuit_breakage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hyland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-02 00:00:00.000000000 Z
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,3 +116,4 @@ test_files:
116
116
  - spec/breaker_spec.rb
117
117
  - spec/redis_backed_breaker_spec.rb
118
118
  - spec/spec_helper.rb
119
+ has_rdoc: