Java Code Examples for net.jodah.failsafe.CircuitBreaker#close()

The following examples show how to use net.jodah.failsafe.CircuitBreaker#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosedStateTest.java    From failsafe with Apache License 2.0 6 votes vote down vote up
/**
 * 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, getInternals(breaker));

  // When
  state.recordFailure(null);
  state.recordSuccess();
  assertTrue(breaker.isClosed());
  state.recordFailure(null);

  // Then
  assertTrue(breaker.isOpen());
}
 
Example 2
Source File: ClosedStateTest.java    From failsafe with Apache License 2.0 6 votes vote down vote up
/**
 * 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, getInternals(breaker));

  // When
  state.recordFailure(null);
  state.recordSuccess();
  state.recordFailure(null);
  state.recordFailure(null);
  assertTrue(breaker.isClosed());
  state.recordFailure(null);

  // Then
  assertTrue(breaker.isOpen());
}
 
Example 3
Source File: ClosedStateTest.java    From failsafe with Apache License 2.0 5 votes vote down vote up
/**
 * 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, getInternals(breaker));
  assertFalse(breaker.isOpen());

  // When
  state.recordFailure(null);

  // Then
  assertTrue(breaker.isOpen());
}
 
Example 4
Source File: ClosedStateTest.java    From failsafe with Apache License 2.0 5 votes vote down vote up
/**
 * 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, getInternals(breaker));
  assertTrue(breaker.isClosed());

  // When
  state.recordSuccess();

  // Then
  assertTrue(breaker.isClosed());
}
 
Example 5
Source File: ClosedStateTest.java    From failsafe with Apache License 2.0 5 votes vote down vote up
/**
 * 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, getInternals(breaker));
  assertTrue(breaker.isClosed());

  // When / Then
  for (int i = 0; i < 20; i++) {
    state.recordSuccess();
    state.recordFailure(null);
    assertTrue(breaker.isClosed());
  }
}
 
Example 6
Source File: ClosedStateTest.java    From failsafe with Apache License 2.0 5 votes vote down vote up
/**
 * 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, getInternals(breaker));
  assertTrue(breaker.isClosed());

  // When / Then
  for (int i = 0; i < 20; i++) {
    state.recordSuccess();
    state.recordFailure(null);
    assertTrue(breaker.isClosed());
  }
}