Search in sources :

Example 21 with CircuitBreaker

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

the class HalfOpenStateTest method testSuccessWithDefaultConfig.

/**
   * Asserts that the the circuit is closed after a single success.
   */
public void testSuccessWithDefaultConfig() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker();
    breaker.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    assertFalse(breaker.isOpen());
    assertFalse(breaker.isClosed());
    // When
    state.recordSuccess();
    // Then
    assertTrue(breaker.isClosed());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 22 with CircuitBreaker

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

the class HalfOpenStateTest method testSuccessWithSuccessAndFailureRatios.

/**
   * Asserts that the circuit is closed after the success ratio is met. The failure ratio is ignored.
   */
public void testSuccessWithSuccessAndFailureRatios() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(3, 5).withSuccessThreshold(3, 4);
    breaker.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    // When
    state.recordSuccess();
    state.recordFailure();
    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)

Example 23 with CircuitBreaker

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

the class HalfOpenStateTest method testSuccessWithFailureThreshold.

/**
   * Asserts that the the circuit is closed after a single success.
   */
public void testSuccessWithFailureThreshold() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(3);
    breaker.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    assertFalse(breaker.isOpen());
    assertFalse(breaker.isClosed());
    // When
    state.recordFailure();
    state.recordSuccess();
    // Then
    assertTrue(breaker.isClosed());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 24 with CircuitBreaker

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

the class HalfOpenStateTest method shouldHandleLateSetSucessRatio.

/**
   * Asserts that the late configuration of a success 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 shouldHandleLateSetSucessRatio() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker();
    breaker.halfOpen();
    HalfOpenState state = Testing.stateFor(breaker);
    // When
    breaker.withSuccessThreshold(2);
    state.recordSuccess();
    assertTrue(breaker.isHalfOpen());
    state.recordSuccess();
    // Then
    assertTrue(breaker.isClosed());
    // Given
    breaker = new CircuitBreaker().withFailureThreshold(3);
    breaker.halfOpen();
    state = Testing.stateFor(breaker);
    // When
    state.recordFailure();
    state.recordFailure();
    breaker.withSuccessThreshold(2, 4);
    state.recordSuccess();
    assertTrue(breaker.isHalfOpen());
    state.recordSuccess();
    // Then
    assertTrue(breaker.isClosed());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 25 with CircuitBreaker

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

the class ClosedStateTest method testSuccessWithDefaultConfig.

/**
   * Asserts that the the circuit is still closed after a single success.
   */
public void testSuccessWithDefaultConfig() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker();
    breaker.close();
    ClosedState state = new ClosedState(breaker);
    assertTrue(breaker.isClosed());
    // When
    state.recordSuccess();
    // Then
    assertTrue(breaker.isClosed());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker)

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