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);
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class ClosedStateTest method testFailureWithDefaultConfig.
/**
* Asserts that the the circuit is opened after a single failure.
*/
public void testFailureWithDefaultConfig() {
// Given
CircuitBreaker breaker = new CircuitBreaker();
breaker.close();
ClosedState state = new ClosedState(breaker);
assertFalse(breaker.isOpen());
// When
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class ClosedStateTest method testFailureWithFailureRatio.
/**
* Asserts that the the circuit is opened after the failure ratio is met.
*/
public void testFailureWithFailureRatio() {
// Given
CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(2, 3);
breaker.close();
ClosedState state = new ClosedState(breaker);
// When
state.recordFailure();
state.recordSuccess();
assertTrue(breaker.isClosed());
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class ClosedStateTest method testFailureWithFailureThreshold.
/**
* Asserts that the the circuit is opened after the failure threshold is met.
*/
public void testFailureWithFailureThreshold() {
// Given
CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(3);
breaker.close();
ClosedState state = new ClosedState(breaker);
// When
state.recordFailure();
state.recordSuccess();
state.recordFailure();
state.recordFailure();
assertTrue(breaker.isClosed());
state.recordFailure();
// Then
assertTrue(breaker.isOpen());
}
use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.
the class HalfOpenStateTest method testSuccessWithFailureRatio.
/**
* Asserts that the the circuit is closed after the failure ratio fails to be met.
*/
public void testSuccessWithFailureRatio() {
// Given
CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(2, 3);
breaker.halfOpen();
HalfOpenState state = new HalfOpenState(breaker);
// When
state.recordFailure();
state.recordSuccess();
assertFalse(breaker.isOpen());
assertFalse(breaker.isClosed());
state.recordSuccess();
// Then
assertTrue(breaker.isClosed());
}
Aggregations