supervision 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,4 +3,25 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe Supervision::CircuitMonitor do
6
+ let(:object) { described_class }
7
+
8
+ subject(:monitor) { object.new }
9
+
10
+ describe "#record_success" do
11
+ it "records success" do
12
+ monitor.record_success
13
+ expect(monitor.total_success_calls).to eq(1)
14
+ expect(monitor.total_failed_calls).to eq(0)
15
+ expect(monitor.total_calls).to eq(1)
16
+ end
17
+ end
18
+
19
+ describe "#record_failure" do
20
+ it "records failure" do
21
+ monitor.record_failure
22
+ expect(monitor.total_success_calls).to eq(0)
23
+ expect(monitor.total_failed_calls).to eq(1)
24
+ expect(monitor.total_calls).to eq(1)
25
+ end
26
+ end
6
27
  end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Supervision::CircuitSystem do
6
+ let(:object) { described_class }
7
+
8
+ let(:circuit) { Supervision.supervise { } }
9
+
10
+ subject(:system) { object.new }
11
+
12
+ it { expect(system).to respond_to(:register) }
13
+
14
+ it { expect(system).to respond_to(:delete) }
15
+
16
+ it { expect(system).to respond_to(:registered?) }
17
+
18
+ it { expect(system).to respond_to(:empty?) }
19
+
20
+ it { expect(system).to respond_to(:names) }
21
+
22
+ describe "#shutdown" do
23
+ it "shuts down system" do
24
+ expect(system.registry).to receive(:clear)
25
+ system.shutdown
26
+ end
27
+ end
28
+
29
+ describe "#to_s" do
30
+ it 'prints object info' do
31
+ system.register(:danger, circuit)
32
+ expect(system.to_s).to include("@names=[:danger]")
33
+ end
34
+ end
35
+
36
+ describe "#inspect" do
37
+ it 'prints object info' do
38
+ system.register(:danger, circuit)
39
+ expect(system.inspect).to include("@names=[:danger]")
40
+ end
41
+ end
42
+ end
@@ -3,12 +3,38 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Supervision::Configuration do
6
+ let(:object) { described_class }
6
7
 
7
8
  subject(:config) { described_class.new }
8
9
 
9
- it { expect(config.max_failures).to eql(5) }
10
+ it "fails fast with unknown config option" do
11
+ expect {
12
+ object.new max_fail: 2
13
+ }.to raise_error(Supervision::InvalidParameterError)
14
+ end
10
15
 
11
- it { expect(config.call_timeout).to eql(0.01) }
16
+ context 'when default' do
17
+ it { expect(config.max_failures).to eql(5) }
12
18
 
13
- it { expect(config.reset_timeout).to eql(0.1) }
19
+ it { expect(config.call_timeout).to eql(0.01) }
20
+
21
+ it { expect(config.reset_timeout).to eql(0.1) }
22
+ end
23
+
24
+ context 'when setting' do
25
+ it "sets maximum failures" do
26
+ config.max_failures(3)
27
+ expect(config.max_failures).to eq(3)
28
+ end
29
+
30
+ it "sets call timeout" do
31
+ config.call_timeout(1.sec)
32
+ expect(config.call_timeout).to eq(1.sec)
33
+ end
34
+
35
+ it "sets reset timeout" do
36
+ config.reset_timeout(10.sec)
37
+ expect(config.reset_timeout).to eq(10.sec)
38
+ end
39
+ end
14
40
  end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Supervision::Counter do
6
+
7
+ let(:object) { described_class }
8
+
9
+ subject(:counter) { object.new }
10
+
11
+ it "initializes to 0" do
12
+ expect(counter.value).to eq(0)
13
+ end
14
+
15
+ it "increments default value" do
16
+ counter.increment
17
+ expect(counter.value).to eq(1)
18
+ end
19
+
20
+ it "increments by value" do
21
+ counter.increment(10)
22
+ expect(counter.value).to eq(10)
23
+ end
24
+
25
+ it "increments in threads" do
26
+ spawn(10) { counter.increment }
27
+ expect(counter.value).to eq(10)
28
+ end
29
+
30
+ it "increments in threads by value" do
31
+ spawn(10) { counter.increment(10) }
32
+ expect(counter.value).to eq(100)
33
+ end
34
+ end
@@ -5,6 +5,8 @@ require 'spec_helper'
5
5
  describe Supervision do
6
6
 
7
7
  context "when used as instance" do
8
+ before { Supervision.circuit_system.shutdown }
9
+
8
10
  it "permits options configuration" do
9
11
  supervision = Supervision.new { }
10
12
  supervision.configure do
@@ -15,23 +17,78 @@ describe Supervision do
15
17
  expect(supervision.control.call_timeout).to eql(1.sec)
16
18
  end
17
19
 
18
- it "allows to supervise call" do
19
- called = []
20
- supervision = Supervision.supervise { called << 'method_call'}
21
- supervision.call
22
- expect(called).to eql(['method_call'])
20
+ describe "#supervise" do
21
+ it "supervises call" do
22
+ called = []
23
+ supervision = Supervision.supervise { |arg| called << "method_call_#{arg}"}
24
+ supervision.call(:foo)
25
+ expect(called).to match_array(['method_call_foo'])
26
+ end
27
+
28
+ it "accepts circuit options and exposes them" do
29
+ supervision = Supervision.supervise(max_failures: 4,
30
+ call_timeout: 0.1.sec,
31
+ reset_timeout: 0.4.sec) { }
32
+ expect(supervision.max_failures).to eq(4)
33
+ expect(supervision.call_timeout).to eq(0.1.sec)
34
+ expect(supervision.reset_timeout).to eq(0.4.sec)
35
+ end
36
+ end
37
+
38
+ describe "#supervise_as" do
39
+ it "registers named supervision" do
40
+ called = []
41
+ expect(Supervision[:danger]).to be_nil
42
+ supervision = Supervision.supervise_as(:danger) { |arg|
43
+ called << "method_call_#{arg}"
44
+ }
45
+ supervision.call(:foo)
46
+ expect(Supervision[:danger]).to eql(supervision)
47
+ expect(called).to match_array(['method_call_foo'])
48
+ end
49
+
50
+ it "accepts circuit options and exposes them" do
51
+ Supervision.supervise_as(:danger, max_failures: 4,
52
+ call_timeout: 0.1.sec,
53
+ reset_timeout: 0.4.sec) { }
54
+ expect(Supervision[:danger].max_failures).to eq(4)
55
+ expect(Supervision[:danger].call_timeout).to eq(0.1.sec)
56
+ expect(Supervision[:danger].reset_timeout).to eq(0.4.sec)
57
+ end
58
+
59
+ it "calls registered circuit by name" do
60
+ called = []
61
+ expect(Supervision[:danger]).to be_nil
62
+ Supervision.supervise_as(:danger) { |arg| called << "method_call_#{arg}" }
63
+ Supervision.danger(:foo)
64
+ expect(called).to match_array(['method_call_foo'])
65
+ end
66
+
67
+ it "saves the name on circuit" do
68
+ Supervision.supervise_as(:danger) { }
69
+ circuit = Supervision[:danger]
70
+ expect(circuit.name).to eql(:danger)
71
+ end
72
+ end
73
+
74
+ describe "#circuit_system" do
75
+ it "caches system circuits" do
76
+ system = Supervision.circuit_system
77
+ 2.times { expect(Supervision.circuit_system).to eq(system) }
78
+ end
23
79
  end
24
80
 
25
- it "registers named supervision" do
26
- called = []
27
- supervision = Supervision.supervise_as(:danger) { called << 'method_call'}
28
- supervision.call
29
- expect(called).to eql(['method_call'])
30
- expect(Supervision.circuit_system[:danger]).to eql(supervision)
81
+ describe "#configuration" do
82
+ it "caches configuration" do
83
+ config = Supervision.configuration
84
+ 2.times { expect(Supervision.configuration).to eq(config) }
85
+ end
31
86
  end
32
87
  end
33
88
 
34
89
  context "when included as module" do
90
+ before { Supervision.circuit_system.shutdown }
91
+
35
92
  class RemoteApi
36
93
  include Supervision
37
94
 
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Supervision::Registry do
@@ -6,7 +8,7 @@ describe Supervision::Registry do
6
8
 
7
9
  subject(:registry) { described_class.new }
8
10
 
9
- it "registers" do
11
+ it "registers a circuit" do
10
12
  registry[:danger] = circuit
11
13
  expect(registry[:danger]).to eql(circuit)
12
14
  end
@@ -17,14 +19,43 @@ describe Supervision::Registry do
17
19
  }.to raise_error(Supervision::TypeError)
18
20
  end
19
21
 
20
- it "" do
22
+ it "refuses to add duplicate entry" do
21
23
  registry[:danger] = circuit
22
- expect(registry.names).to eql([:danger])
24
+ expect {
25
+ registry[:danger] = circuit
26
+ }.to raise_error(Supervision::DuplicateEntryError)
23
27
  end
24
28
 
25
- it "" do
26
- registry[:danger] = circuit
27
- registry.delete(:danger)
28
- expect(registry.names).to be_empty
29
+ it "returns nil for unregistered circuit" do
30
+ expect(registry[:danger]).to be_nil
31
+ end
32
+
33
+ it "gets a circuit by name" do
34
+ registry.register :danger, circuit
35
+ expect(registry[:danger]).to eq(circuit)
36
+ end
37
+
38
+ it "retrieves registered circuit names" do
39
+ registry.register :danger, circuit
40
+ registry.register :fragile, circuit
41
+ expect(registry.names).to match_array([:danger, :fragile])
42
+ end
43
+
44
+ it "deletes circuit from registry" do
45
+ registry.register :danger, circuit
46
+ registry.delete :danger
47
+ expect(registry.empty?).to be_true
48
+ end
49
+
50
+ it "checks if circuit is registered" do
51
+ registry.register :danger, circuit
52
+ expect(registry.registered?(:danger)).to be_true
53
+ end
54
+
55
+ it "clears all circuits" do
56
+ registry.register :danger, circuit
57
+ expect(registry.empty?).to be_false
58
+ registry.clear
59
+ expect(registry.empty?).to be_true
29
60
  end
30
61
  end
@@ -18,6 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "finite_machine", "~> 0.4"
21
+ spec.add_dependency "finite_machine", "~> 0.6.1"
22
22
  spec.add_development_dependency "bundler", "~> 1.6"
23
23
  end
@@ -4,7 +4,7 @@ desc 'Load gem inside irb console'
4
4
  task :console do
5
5
  require 'irb'
6
6
  require 'irb/completion'
7
- require File.join(__FILE__, '../lib/finite_machine')
7
+ require File.join(__FILE__, '../../lib/supervision')
8
8
  ARGV.clear
9
9
  IRB.start
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supervision
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-13 00:00:00.000000000 Z
11
+ date: 2014-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: finite_machine
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '0.4'
19
+ version: 0.6.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '0.4'
26
+ version: 0.6.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,7 @@ files:
52
52
  - .ruby-gemset
53
53
  - .ruby-version
54
54
  - .travis.yml
55
+ - CHANGELOG.md
55
56
  - Gemfile
56
57
  - LICENSE.txt
57
58
  - README.md
@@ -63,6 +64,7 @@ files:
63
64
  - lib/supervision/circuit_monitor.rb
64
65
  - lib/supervision/circuit_system.rb
65
66
  - lib/supervision/configuration.rb
67
+ - lib/supervision/counter.rb
66
68
  - lib/supervision/factory.rb
67
69
  - lib/supervision/registry.rb
68
70
  - lib/supervision/time_dsl.rb
@@ -72,7 +74,9 @@ files:
72
74
  - spec/unit/circuit_breaker_spec.rb
73
75
  - spec/unit/circuit_control_spec.rb
74
76
  - spec/unit/circuit_monitor_spec.rb
77
+ - spec/unit/circuit_system_spec.rb
75
78
  - spec/unit/configuration_spec.rb
79
+ - spec/unit/counter_spec.rb
76
80
  - spec/unit/initialize_spec.rb
77
81
  - spec/unit/registry_spec.rb
78
82
  - spec/unit/time_dsl_spec.rb
@@ -110,7 +114,9 @@ test_files:
110
114
  - spec/unit/circuit_breaker_spec.rb
111
115
  - spec/unit/circuit_control_spec.rb
112
116
  - spec/unit/circuit_monitor_spec.rb
117
+ - spec/unit/circuit_system_spec.rb
113
118
  - spec/unit/configuration_spec.rb
119
+ - spec/unit/counter_spec.rb
114
120
  - spec/unit/initialize_spec.rb
115
121
  - spec/unit/registry_spec.rb
116
122
  - spec/unit/time_dsl_spec.rb