Module: Breaker::TestCases
- Defined in:
- lib/breaker/test_cases.rb
Constant Summary collapse
- DummyError =
Class.new RuntimeError
Instance Method Summary collapse
- #repo ⇒ Object
- #setup ⇒ Object
- #test_breaker_query_methods ⇒ Object
- #test_circuit_breaker_factory_can_run_code_through_the_circuit ⇒ Object
- #test_circuit_factory_creates_new_fuses_with_sensible_defaults ⇒ Object
- #test_circuit_factory_persists_fuses ⇒ Object
- #test_circuit_factory_updates_existing_fuses ⇒ Object
- #test_counts_timeouts_as_trips ⇒ Object
- #test_failures_in_half_open_state_push_retry_timeout_back ⇒ Object
- #test_goes_into_open_state_when_failure_threshold_reached ⇒ Object
- #test_new_fuses_start_off_clean ⇒ Object
- #test_success_in_half_open_state_moves_circuit_into_closed ⇒ Object
Instance Method Details
#repo ⇒ Object
5 6 7 |
# File 'lib/breaker/test_cases.rb', line 5 def repo flunk "Test must define a repo to use" end |
#setup ⇒ Object
9 10 11 |
# File 'lib/breaker/test_cases.rb', line 9 def setup Breaker.repo = repo end |
#test_breaker_query_methods ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/breaker/test_cases.rb', line 142 def test_breaker_query_methods circuit = Breaker.circuit 'test' circuit.close assert Breaker.closed?('test') assert Breaker.up?('test') refute Breaker.open?('test') refute Breaker.down?('test') circuit.open assert Breaker.open?('test') assert Breaker.down?('test') refute Breaker.closed?('test') refute Breaker.up?('test') end |
#test_circuit_breaker_factory_can_run_code_through_the_circuit ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/breaker/test_cases.rb', line 134 def test_circuit_breaker_factory_can_run_code_through_the_circuit assert_raises DummyError do Breaker.circuit 'test' do raise DummyError end end end |
#test_circuit_factory_creates_new_fuses_with_sensible_defaults ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/breaker/test_cases.rb', line 108 def test_circuit_factory_creates_new_fuses_with_sensible_defaults circuit = Breaker.circuit 'test' assert_equal 1, Breaker.repo.count fuse = Breaker.repo.first assert_equal 10, fuse.failure_threshold, "Failure Theshold should have a default" assert_equal 60, fuse.retry_timeout, "Retry timeout should have a default" assert_equal 5, fuse.timeout, "Timeout should have a default" end |
#test_circuit_factory_persists_fuses ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/breaker/test_cases.rb', line 96 def test_circuit_factory_persists_fuses circuit_a = Breaker.circuit 'test' circuit_b = Breaker.circuit 'test' assert_equal circuit_a, circuit_b, "Multiple calls to `circuit` should return the same circuit" assert_equal 1, Breaker.repo.count fuse = Breaker.repo.first assert_equal 'test', fuse.name end |
#test_circuit_factory_updates_existing_fuses ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/breaker/test_cases.rb', line 119 def test_circuit_factory_updates_existing_fuses Breaker.circuit 'test' assert_equal 1, Breaker.repo.count Breaker.circuit 'test', failure_threshold: 1, retry_timeout: 2, timeout: 3 assert_equal 1, Breaker.repo.count fuse = Breaker.repo.first assert_equal 1, fuse.failure_threshold assert_equal 2, fuse.retry_timeout assert_equal 3, fuse.timeout end |
#test_counts_timeouts_as_trips ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/breaker/test_cases.rb', line 85 def test_counts_timeouts_as_trips circuit = Breaker.circuit 'test', retry_timeout: 15, timeout: 0.01 assert circuit.closed? assert_raises TimeoutError do circuit.run do sleep circuit.timeout * 2 end end end |
#test_failures_in_half_open_state_push_retry_timeout_back ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/breaker/test_cases.rb', line 59 def test_failures_in_half_open_state_push_retry_timeout_back clock = Time.now circuit = Breaker.circuit 'test', failure_threshold: 2, retry_timeout: 15 circuit.open clock assert circuit.open? assert_raises DummyError do circuit.run clock + circuit.retry_timeout do raise DummyError end end assert_raises Breaker::CircuitOpenError do circuit.run clock + circuit.retry_timeout do assert false, "Block should not be run while in this state" end end assert_raises DummyError do circuit.run clock + circuit.retry_timeout * 2 do raise DummyError end end end |
#test_goes_into_open_state_when_failure_threshold_reached ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/breaker/test_cases.rb', line 20 def test_goes_into_open_state_when_failure_threshold_reached circuit = Breaker.circuit 'test', failure_threshold: 1, retry_timeout: 30 assert circuit.closed? assert_raises DummyError do circuit.run do raise DummyError end end assert circuit.open? assert_raises Breaker::CircuitOpenError do circuit.run do assert false, "Block should not run in this state" end end end |
#test_new_fuses_start_off_clean ⇒ Object
13 14 15 16 17 18 |
# File 'lib/breaker/test_cases.rb', line 13 def test_new_fuses_start_off_clean circuit = Breaker.circuit 'test' assert circuit.closed?, "New circuits should be closed" assert_equal 0, circuit.failure_count end |
#test_success_in_half_open_state_moves_circuit_into_closed ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/breaker/test_cases.rb', line 39 def test_success_in_half_open_state_moves_circuit_into_closed clock = Time.now circuit = Breaker.circuit 'test', failure_threshold: 2, retry_timeout: 15 circuit.open clock assert circuit.open? assert_raises Breaker::CircuitOpenError do circuit.run clock do # nothing end end circuit.run clock + circuit.retry_timeout do # do nothing, this works and flips the circuit back closed end assert circuit.closed? end |