Search in sources :

Example 11 with CircuitBreaker

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());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 12 with CircuitBreaker

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());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 13 with CircuitBreaker

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());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 14 with CircuitBreaker

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);
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) OpenState(net.jodah.failsafe.internal.OpenState)

Example 15 with CircuitBreaker

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);
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.testng.annotations.Test)

Aggregations

CircuitBreaker (net.jodah.failsafe.CircuitBreaker)29 HalfOpenState (net.jodah.failsafe.internal.HalfOpenState)20 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 OpenState (net.jodah.failsafe.internal.OpenState)1 Test (org.testng.annotations.Test)1