Search in sources :

Example 1 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)

Example 2 with CircuitBreaker

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

Example 3 with CircuitBreaker

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

Example 4 with CircuitBreaker

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

Example 5 with CircuitBreaker

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());
}
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