コード例 #1
0
  public void testBreakerTripsAfterTimeoutPassedWithBadCommand() throws Exception {

    tripBreaker();
    setTimeoutPassed();

    // Now pass in a bad command
    mockCommand
        .expects(atLeastOnce())
        .method("execute")
        .will(onConsecutiveCalls(throwException(new CommandException()), returnValue(null)));

    try {
      underTest.doOperation(command);
    } catch (Exception e) {
      // ignore
    }

    // Circuit breaker should now be in open state
    boolean exception = false;

    try {
      underTest.doOperation(command);
    } catch (CircuitBreakerOpenException e) {
      exception = true;
    }

    assertTrue("Circuit breaker should have tripped and so should be failing fast", exception);
  }
コード例 #2
0
  public void testBreakerResetsAfterTimeoutPassedWithSuccessfulCommand() throws Exception {

    tripBreaker();
    setTimeoutPassed();

    // Now pass in a working command
    mockCommand.expects(atLeastOnce()).method("execute");
    underTest.doOperation(command);

    // Circuit breaker should now be in closed state
    underTest.doOperation(command);
  }
コード例 #3
0
  public void setUp() {
    clockStub.setNow(System.currentTimeMillis());

    SimpleStateFactory simpleStateFactory = new SimpleStateFactory();
    simpleStateFactory.setClock(clockStub);
    simpleStateFactory.setTimeoutInMillisecs(TEST_TIMEOUT_IN_MILLISECONDS);
    simpleStateFactory.setFailureThreshold(FAILURE_THRESHOLD);

    underTest = new SimpleCircuitBreaker();
    underTest.setStateFactory(simpleStateFactory);
    underTest.initialise();

    mockCommand = mock(Command.class);
    command = (Command) mockCommand.proxy();
  }
コード例 #4
0
  public void testBreakerTripsAfterThresholdPassed() throws Exception {

    tripBreaker();

    boolean exception = false;

    try {
      underTest.doOperation(command);
    } catch (CircuitBreakerOpenException e) {
      exception = true;
    }

    assertTrue("Circuit breaker should have tripped and so should be failing fast", exception);
  }
コード例 #5
0
  /** Sets the breaker into tripped state by sending commands that will fail */
  private void tripBreaker() {
    CommandException commandException = new CommandException();

    Mock badCommand = mock(Command.class, "bad command");

    badCommand.expects(atLeastOnce()).method("execute").will(throwException(commandException));

    for (int i = 0; i < FAILURE_THRESHOLD; i++) {

      try {
        underTest.doOperation((Command) badCommand.proxy());
      } catch (Exception e) {
        assertEquals(commandException, e);
      }
    }
  }
コード例 #6
0
  public void testDefaultOperation() throws Exception {

    mockCommand.expects(once()).method("execute");
    underTest.doOperation(command);
  }