/** * {@inheritDoc} * * <p>If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.</p> */ @Override public boolean incrementAndCheckState(final Long increment) { if (threshold == 0) { open(); } final long used = this.used.addAndGet(increment); if (used > threshold) { open(); } return checkState(); }
/** * {@inheritDoc} */ @Override public boolean checkState() { return isOpen(); }
/** * Tests that closing a {@code ThresholdCircuitBreaker} resets the internal counter. */ @Test public void testClosingThresholdCircuitBreaker() { final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold); circuit.incrementAndCheckState(9L); circuit.close(); // now the internal counter is back at zero, not 9 anymore. So it is safe to increment 9 again assertFalse("Internal counter was not reset back to zero", circuit.incrementAndCheckState(9L)); }
/** * Tests that the threshold is working as expected when incremented and no exception is thrown. */ @Test public void testThreshold() { final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold); circuit.incrementAndCheckState(9L); assertFalse("Circuit opened before reaching the threshold", circuit.incrementAndCheckState(1L)); }
/** * Tests that we can get the threshold value correctly. */ @Test public void testGettingThreshold() { final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold); assertEquals("Wrong value of threshold", Long.valueOf(threshold), Long.valueOf(circuit.getThreshold())); }
/** * Tests that exceeding the threshold raises an exception. */ @Test public void testThresholdCircuitBreakingException() { final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold); circuit.incrementAndCheckState(9L); assertTrue("The circuit was supposed to be open after increment above the threshold", circuit.incrementAndCheckState(2L)); }
/** * {@inheritDoc} * * <p>If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.</p> */ @Override public boolean incrementAndCheckState(final Long increment) { if (threshold == 0) { open(); } final long used = this.used.addAndGet(increment); if (used > threshold) { open(); } return checkState(); }
/** * {@inheritDoc} */ @Override public boolean checkState() { return isOpen(); }
/** * Test that when threshold is zero, the circuit breaker is always open. */ @Test public void testThresholdEqualsZero() { final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(zeroThreshold); assertTrue("When the threshold is zero, the circuit is supposed to be always open", circuit.incrementAndCheckState(0L)); }
/** * {@inheritDoc} * * <p>If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.</p> */ @Override public boolean incrementAndCheckState(final Long increment) { if (threshold == 0) { open(); } final long used = this.used.addAndGet(increment); if (used > threshold) { open(); } return checkState(); }
/** * {@inheritDoc} */ @Override public boolean checkState() { return isOpen(); }
/** * {@inheritDoc} * * <p>If the threshold is zero, the circuit breaker will be in a permanent <em>open</em> state.</p> */ @Override public boolean incrementAndCheckState(final Long increment) { if (threshold == 0) { open(); } final long used = this.used.addAndGet(increment); if (used > threshold) { open(); } return checkState(); }
/** * {@inheritDoc} */ @Override public boolean checkState() { return isOpen(); }