use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class HalfOpenStateTest method shouldHandleLateSetFailureRatio.
/**
* Asserts that the late configuration of a failure ratio is handled by resetting the state's internal tracking. Also
* asserts that executions from prior configurations are carried over to a new configuration.
*/
public void shouldHandleLateSetFailureRatio() {
// Given
CircuitBreaker breaker = new CircuitBreaker();
breaker.halfOpen();
HalfOpenState state = Testing.stateFor(breaker);
// When
breaker.withFailureThreshold(2);
state.recordFailure();
assertTrue(breaker.isHalfOpen());
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
// Given
breaker = new CircuitBreaker().withFailureThreshold(3);
breaker.halfOpen();
state = Testing.stateFor(breaker);
// When
state.recordFailure();
state.recordFailure();
breaker.withFailureThreshold(3, 5);
state.recordSuccess();
state.recordSuccess();
assertTrue(breaker.isHalfOpen());
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class HalfOpenStateTest method testFailureWithSuccessThreshold.
/**
* Asserts that the circuit is opened after a single failure.
*/
public void testFailureWithSuccessThreshold() {
// Given
CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(3);
breaker.halfOpen();
HalfOpenState state = new HalfOpenState(breaker);
assertFalse(breaker.isOpen());
assertFalse(breaker.isClosed());
// When
state.recordSuccess();
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class HalfOpenStateTest method testFailureWithSuccessRatio.
/**
* Asserts that the circuit is opened after the success ratio fails to be met.
*/
public void testFailureWithSuccessRatio() {
// Given
CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(2, 3);
breaker.halfOpen();
HalfOpenState state = new HalfOpenState(breaker);
assertFalse(breaker.isOpen());
// When
state.recordFailure();
state.recordSuccess();
assertFalse(breaker.isOpen());
assertFalse(breaker.isClosed());
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class OpenStateTest method testAllowsExecution.
public void testAllowsExecution() throws Throwable {
// Given
CircuitBreaker breaker = new CircuitBreaker().withDelay(100, TimeUnit.MILLISECONDS);
breaker.open();
OpenState state = new OpenState(breaker);
assertTrue(breaker.isOpen());
assertFalse(state.allowsExecution(null));
// When
Thread.sleep(110);
// Then
assertTrue(state.allowsExecution(null));
assertEquals(breaker.getState(), State.HALF_OPEN);
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class Issue75 method testThatFailSafeIsBrokenWithFallback.
@Test
public void testThatFailSafeIsBrokenWithFallback() throws Exception {
CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(10, 100).withSuccessThreshold(2).withDelay(100, TimeUnit.MILLISECONDS);
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
int result = Failsafe.with(breaker).with(service).withFallback((a, b) -> 999).future(() -> CompletableFuture.completedFuture(223)).get();
Assert.assertEquals(result, 223);
}
Aggregations