Search in sources :

Example 26 with CircuitBreaker

use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.

the class ClosedStateTest method testSuccessWithFailureRatio.

/**
   * Asserts that the the circuit stays closed after the failure ratio fails to be met.
   */
public void testSuccessWithFailureRatio() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(3, 4);
    breaker.close();
    ClosedState state = new ClosedState(breaker);
    assertTrue(breaker.isClosed());
    // When / Then
    for (int i = 0; i < 20; i++) {
        state.recordSuccess();
        state.recordFailure();
        assertTrue(breaker.isClosed());
    }
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker)

Example 27 with CircuitBreaker

use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.

the class ClosedStateTest method testSuccessWithFailureThreshold.

/**
   * Asserts that the the circuit stays closed after the failure ratio fails to be met.
   */
public void testSuccessWithFailureThreshold() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(2);
    breaker.close();
    ClosedState state = new ClosedState(breaker);
    assertTrue(breaker.isClosed());
    // When / Then
    for (int i = 0; i < 20; i++) {
        state.recordSuccess();
        state.recordFailure();
        assertTrue(breaker.isClosed());
    }
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker)

Example 28 with CircuitBreaker

use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.

the class ClosedStateTest 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();
    ClosedState state = Testing.stateFor(breaker);
    // When
    state.recordSuccess();
    assertTrue(breaker.isClosed());
    breaker.withFailureThreshold(2);
    state.recordFailure();
    assertTrue(breaker.isClosed());
    state.recordFailure();
    // Then
    assertTrue(breaker.isOpen());
    // Given
    breaker = new CircuitBreaker();
    state = Testing.stateFor(breaker);
    // When
    state.recordSuccess();
    assertTrue(breaker.isClosed());
    breaker.withFailureThreshold(2, 3);
    state.recordFailure();
    assertTrue(breaker.isClosed());
    state.recordFailure();
    // Then
    assertTrue(breaker.isOpen());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker)

Example 29 with CircuitBreaker

use of net.jodah.failsafe.CircuitBreaker in project failsafe by jhalterman.

the class HalfOpenStateTest method testSuccessWithSuccessThresholdAndFailureRatio.

/**
   * Asserts that the circuit is closed after the success threshold is met. The failure ratio is ignored.
   */
public void testSuccessWithSuccessThresholdAndFailureRatio() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(3, 5).withSuccessThreshold(2);
    breaker.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    // When success threshold exceeded
    state.recordSuccess();
    assertFalse(breaker.isOpen());
    assertFalse(breaker.isClosed());
    state.recordSuccess();
    // Then
    assertTrue(breaker.isClosed());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

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