Search in sources :

Example 16 with CircuitBreaker

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

the class HalfOpenStateTest method testSuccessWithSuccessAndFailureThresholds.

/**
   * Asserts that the circuit is closed after the success threshold is met. The failure threshold is ignored.
   */
public void testSuccessWithSuccessAndFailureThresholds() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(3).withFailureThreshold(2);
    breaker.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    // When
    state.recordSuccess();
    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 17 with CircuitBreaker

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

the class HalfOpenStateTest method testFailureWithSuccessRatioAndFailureThreshold.

/**
   * Asserts that the circuit is opened after the success ratio fails to be met. The failure threshold is ignored.
   */
public void testFailureWithSuccessRatioAndFailureThreshold() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(2, 4).withFailureThreshold(1);
    breaker.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    // When
    state.recordSuccess();
    state.recordFailure();
    state.recordFailure();
    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 18 with CircuitBreaker

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

the class HalfOpenStateTest method testSuccessWithSuccessRatio.

/**
   * Asserts that the circuit is closed after the success ratio is met.
   */
public void testSuccessWithSuccessRatio() {
    // Given
    CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(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());
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) HalfOpenState(net.jodah.failsafe.internal.HalfOpenState)

Example 19 with CircuitBreaker

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

the class HalfOpenStateTest method testFailureWithDefaultConfig.

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

Example 20 with CircuitBreaker

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

the class HalfOpenStateTest 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.halfOpen();
    HalfOpenState state = new HalfOpenState(breaker);
    // When
    for (int i = 0; i < 3; i++) {
        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)

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