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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/breaker/test_cases.rb', line 168 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
160 161 162 163 164 165 166 |
# File 'lib/breaker/test_cases.rb', line 160 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
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/breaker/test_cases.rb', line 134 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
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/breaker/test_cases.rb', line 122 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/breaker/test_cases.rb', line 145 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
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/breaker/test_cases.rb', line 111 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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/breaker/test_cases.rb', line 78 def test_failures_in_half_open_state_push_retry_timeout_back clock = Time.now circuit = Breaker.circuit 'test', failure_threshold: 1, retry_timeout: 15 (circuit.failure_threshold + 1).times do begin circuit.run clock do raise DummyError end rescue DummyError ; end end 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 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/breaker/test_cases.rb', line 20 def test_goes_into_open_state_when_failure_threshold_reached circuit = Breaker.circuit 'test', failure_threshold: 5, retry_timeout: 30 assert circuit.closed? circuit.failure_threshold.times do begin circuit.run do raise DummyError end rescue DummyError ; end end assert_equal circuit.failure_count, circuit.failure_threshold refute circuit.open? 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/breaker/test_cases.rb', line 51 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.failure_threshold + 1).times do begin circuit.run clock do raise DummyError end rescue DummyError ; end end 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 |