/**
   * When volume of reporting during a statistical window is lower than a defined threshold the
   * circuit will not trip regardless of whatever statistics are calculated.
   */
  @Test
  public void testLowVolumeDoesNotTripCircuit() {
    String key = "cmd-I";
    try {
      int sleepWindow = 200;
      int lowVolume = 5;

      HystrixCommand<Boolean> cmd1 = new FailureCommand(key, 60, sleepWindow, lowVolume);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1, sleepWindow, lowVolume);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new FailureCommand(key, 1, sleepWindow, lowVolume);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new FailureCommand(key, 1, sleepWindow, lowVolume);
      cmd4.execute();

      // even though it has all failed we won't trip the circuit because the volume is low
      Thread.sleep(100);
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /** Test that if all 'marks' are timeouts that it will trip the circuit. */
  @Test
  public void testTripCircuitOnTimeouts() {
    String key = "cmd-D";
    try {
      HystrixCommand<Boolean> cmd1 = new TimeoutCommand(key);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      // success with high latency
      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new TimeoutCommand(key);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new TimeoutCommand(key);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new TimeoutCommand(key);
      cmd4.execute();

      // everything has been a timeout so we should not allow any requests
      Thread.sleep(100);
      assertFalse(cb.allowRequest());
      assertTrue(cb.isOpen());
    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /**
   * Test that an open circuit is closed after 1 success. This also ensures that the rolling window
   * (containing failures) is cleared after the sleep window Otherwise, the next bucket roll would
   * produce another signal to fail unless it is explicitly cleared (via {@link
   * HystrixCommandMetrics#resetStream()}.
   */
  @Test
  public void testCircuitClosedAfterSuccess() {
    String key = "cmd-G";
    try {
      int sleepWindow = 20;
      HystrixCommand<Boolean> cmd1 = new FailureCommand(key, 1, sleepWindow);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1, sleepWindow);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new FailureCommand(key, 1, sleepWindow);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new TimeoutCommand(key, sleepWindow);
      cmd4.execute();

      // everything has failed in the test window so we should return false now
      Thread.sleep(100);
      System.out.println(
          "ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
      System.out.println("CircuitBreaker state 1 : " + cmd1.getMetrics().getHealthCounts());
      assertFalse(cb.allowRequest());
      assertTrue(cb.isOpen());

      // wait for sleepWindow to pass
      Thread.sleep(sleepWindow + 50);

      // but the circuit should still be open
      assertTrue(cb.isOpen());

      // we should now allow 1 request, and upon success, should cause the circuit to be closed
      HystrixCommand<Boolean> cmd5 = new SuccessCommand(key, 60, sleepWindow);
      Observable<Boolean> asyncResult = cmd5.observe();

      // and further requests are still blocked while the singleTest command is in flight
      assertFalse(cb.allowRequest());

      asyncResult.toBlocking().single();

      // all requests should be open again
      System.out.println("CircuitBreaker state 2 : " + cmd1.getMetrics().getHealthCounts());
      assertTrue(cb.allowRequest());
      assertTrue(cb.allowRequest());
      assertTrue(cb.allowRequest());
      // and the circuit should be closed again
      assertFalse(cb.isOpen());

    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /**
   * Test that on an open circuit that a single attempt will be allowed after a window of time to
   * see if issues are resolved.
   */
  @Test
  public void testSingleTestOnOpenCircuitAfterTimeWindow() {
    String key = "cmd-F";
    try {
      int sleepWindow = 200;
      HystrixCommand<Boolean> cmd1 = new FailureCommand(key, 60);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new FailureCommand(key, 1);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new FailureCommand(key, 1);
      cmd4.execute();

      // everything has failed in the test window so we should return false now
      Thread.sleep(100);
      assertFalse(cb.allowRequest());
      assertTrue(cb.isOpen());

      // wait for sleepWindow to pass
      Thread.sleep(sleepWindow + 50);

      // we should now allow 1 request
      assertTrue(cb.allowRequest());
      // but the circuit should still be open
      assertTrue(cb.isOpen());
      // and further requests are still blocked
      assertFalse(cb.allowRequest());

    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /** Test that if the % of failures is higher than the threshold that the circuit trips. */
  @Test
  public void testCircuitDoesNotTripOnFailuresBelowThreshold() {
    String key = "cmd-C";
    try {
      HystrixCommand<Boolean> cmd1 = new SuccessCommand(key, 60);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      // success with high latency
      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new SuccessCommand(key, 1);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new FailureCommand(key, 1);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new SuccessCommand(key, 1);
      cmd4.execute();
      HystrixCommand<Boolean> cmd5 = new SuccessCommand(key, 1);
      cmd5.execute();
      HystrixCommand<Boolean> cmd6 = new FailureCommand(key, 1);
      cmd6.execute();
      HystrixCommand<Boolean> cmd7 = new SuccessCommand(key, 1);
      cmd7.execute();
      HystrixCommand<Boolean> cmd8 = new FailureCommand(key, 1);
      cmd8.execute();

      // this should remain closed as the failure threshold is below the percentage limit
      Thread.sleep(100);
      System.out.println(
          "ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
      System.out.println("Current CircuitBreaker Status : " + cmd1.getMetrics().getHealthCounts());
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());
    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /**
   * Test that if all 'marks' are successes during the test window that it does NOT trip the
   * circuit. Test that if all 'marks' are failures during the test window that it trips the
   * circuit.
   */
  @Test
  public void testTripCircuit() {
    String key = "cmd-A";
    try {
      HystrixCommand<Boolean> cmd1 = new SuccessCommand(key, 1);
      HystrixCommand<Boolean> cmd2 = new SuccessCommand(key, 1);
      HystrixCommand<Boolean> cmd3 = new SuccessCommand(key, 1);
      HystrixCommand<Boolean> cmd4 = new SuccessCommand(key, 1);

      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      cmd1.execute();
      cmd2.execute();
      cmd3.execute();
      cmd4.execute();

      // this should still allow requests as everything has been successful
      Thread.sleep(100);
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      // fail
      HystrixCommand<Boolean> cmd5 = new FailureCommand(key, 1);
      HystrixCommand<Boolean> cmd6 = new FailureCommand(key, 1);
      HystrixCommand<Boolean> cmd7 = new FailureCommand(key, 1);
      HystrixCommand<Boolean> cmd8 = new FailureCommand(key, 1);
      cmd5.execute();
      cmd6.execute();
      cmd7.execute();
      cmd8.execute();

      // everything has failed in the test window so we should return false now
      Thread.sleep(100);
      assertFalse(cb.allowRequest());
      assertTrue(cb.isOpen());
    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /** Test that if the % of timeouts is higher than the threshold that the circuit trips. */
  @Test
  public void testTripCircuitOnTimeoutsAboveThreshold() {
    String key = "cmd-E";
    try {
      HystrixCommand<Boolean> cmd1 = new SuccessCommand(key, 60);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      // success with high latency
      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new SuccessCommand(key, 1);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new TimeoutCommand(key);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new SuccessCommand(key, 1);
      cmd4.execute();
      HystrixCommand<Boolean> cmd5 = new TimeoutCommand(key);
      cmd5.execute();
      HystrixCommand<Boolean> cmd6 = new TimeoutCommand(key);
      cmd6.execute();
      HystrixCommand<Boolean> cmd7 = new SuccessCommand(key, 1);
      cmd7.execute();
      HystrixCommand<Boolean> cmd8 = new TimeoutCommand(key);
      cmd8.execute();
      HystrixCommand<Boolean> cmd9 = new TimeoutCommand(key);
      cmd9.execute();

      // this should trip the circuit as the error percentage is above the threshold
      Thread.sleep(100);
      assertFalse(cb.allowRequest());
      assertTrue(cb.isOpen());
    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }
  /**
   * Over a period of several 'windows' a single attempt will be made and fail and then finally
   * succeed and close the circuit.
   *
   * <p>Ensure the circuit is kept open through the entire testing period and that only the single
   * attempt in each window is made.
   */
  @Test
  public void testMultipleTimeWindowRetriesBeforeClosingCircuit() {
    String key = "cmd-H";
    try {
      int sleepWindow = 200;
      HystrixCommand<Boolean> cmd1 = new FailureCommand(key, 60);
      HystrixCircuitBreaker cb = cmd1.circuitBreaker;

      // this should start as allowing requests
      assertTrue(cb.allowRequest());
      assertFalse(cb.isOpen());

      cmd1.execute();
      HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1);
      cmd2.execute();
      HystrixCommand<Boolean> cmd3 = new FailureCommand(key, 1);
      cmd3.execute();
      HystrixCommand<Boolean> cmd4 = new TimeoutCommand(key);
      cmd4.execute();

      // everything has failed in the test window so we should return false now
      System.out.println("!!!! 1 4 failures, circuit will open on recalc");
      Thread.sleep(100);

      assertFalse(cb.allowRequest());
      assertTrue(cb.isOpen());

      // wait for sleepWindow to pass
      System.out.println("!!!! 2 Sleep window starting where all commands fail-fast");
      Thread.sleep(sleepWindow + 50);
      System.out.println("!!!! 3 Sleep window over, should allow singleTest()");

      // but the circuit should still be open
      assertTrue(cb.isOpen());

      // we should now allow 1 request, and upon failure, should not affect the circuit breaker,
      // which should remain open
      HystrixCommand<Boolean> cmd5 = new FailureCommand(key, 60);
      Observable<Boolean> asyncResult5 = cmd5.observe();
      System.out.println("!!!! Kicked off the single-test");

      // and further requests are still blocked while the singleTest command is in flight
      assertFalse(cb.allowRequest());
      System.out.println("!!!! Confirmed that no other requests go out during single-test");

      asyncResult5.toBlocking().single();
      System.out.println("!!!! SingleTest just completed");

      // all requests should still be blocked, because the singleTest failed
      assertFalse(cb.allowRequest());
      assertFalse(cb.allowRequest());
      assertFalse(cb.allowRequest());

      // wait for sleepWindow to pass
      System.out.println("!!!! 2nd sleep window START");
      Thread.sleep(sleepWindow + 50);
      System.out.println("!!!! 2nd sleep window over");

      // we should now allow 1 request, and upon failure, should not affect the circuit breaker,
      // which should remain open
      HystrixCommand<Boolean> cmd6 = new FailureCommand(key, 60);
      Observable<Boolean> asyncResult6 = cmd6.observe();
      System.out.println("2nd singleTest just kicked off");

      // and further requests are still blocked while the singleTest command is in flight
      assertFalse(cb.allowRequest());
      System.out.println("confirmed that 2nd singletest only happened once");

      asyncResult6.toBlocking().single();
      System.out.println("2nd singleTest now over");

      // all requests should still be blocked, because the singleTest failed
      assertFalse(cb.allowRequest());
      assertFalse(cb.allowRequest());
      assertFalse(cb.allowRequest());

      // wait for sleepWindow to pass
      Thread.sleep(sleepWindow + 50);

      // but the circuit should still be open
      assertTrue(cb.isOpen());

      // we should now allow 1 request, and upon success, should cause the circuit to be closed
      HystrixCommand<Boolean> cmd7 = new SuccessCommand(key, 60);
      Observable<Boolean> asyncResult7 = cmd7.observe();

      // and further requests are still blocked while the singleTest command is in flight
      assertFalse(cb.allowRequest());

      asyncResult7.toBlocking().single();

      // all requests should be open again
      assertTrue(cb.allowRequest());
      assertTrue(cb.allowRequest());
      assertTrue(cb.allowRequest());
      // and the circuit should be closed again
      assertFalse(cb.isOpen());

      // and the circuit should be closed again
      assertFalse(cb.isOpen());
    } catch (Exception e) {
      e.printStackTrace();
      fail("Error occurred: " + e.getMessage());
    }
  }