|
32 | 32 | import com.netflix.hystrix.strategy.HystrixPlugins;
|
33 | 33 | import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
|
34 | 34 | import rx.Observable;
|
| 35 | +import rx.Subscription; |
35 | 36 |
|
36 | 37 | /**
|
37 | 38 | * These tests each use a different command key to ensure that running them in parallel doesn't allow the state
|
@@ -565,6 +566,56 @@ public void testLowVolumeDoesNotTripCircuit() {
|
565 | 566 | }
|
566 | 567 | }
|
567 | 568 |
|
| 569 | + @Test |
| 570 | + public void testUnsubscriptionDoesNotLeaveCircuitStuckHalfOpen() { |
| 571 | + String key = "cmd-J"; |
| 572 | + try { |
| 573 | + int sleepWindow = 200; |
| 574 | + |
| 575 | + // fail |
| 576 | + HystrixCommand<Boolean> cmd1 = new FailureCommand(key, 1, sleepWindow); |
| 577 | + HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1, sleepWindow); |
| 578 | + HystrixCommand<Boolean> cmd3 = new FailureCommand(key, 1, sleepWindow); |
| 579 | + HystrixCommand<Boolean> cmd4 = new FailureCommand(key, 1, sleepWindow); |
| 580 | + cmd1.execute(); |
| 581 | + cmd2.execute(); |
| 582 | + cmd3.execute(); |
| 583 | + cmd4.execute(); |
| 584 | + |
| 585 | + HystrixCircuitBreaker cb = cmd1.circuitBreaker; |
| 586 | + |
| 587 | + // everything has failed in the test window so we should return false now |
| 588 | + Thread.sleep(100); |
| 589 | + assertFalse(cb.allowRequest()); |
| 590 | + assertTrue(cb.isOpen()); |
| 591 | + |
| 592 | + //this should occur after the sleep window, so get executed |
| 593 | + //however, it is unsubscribed, so never updates state on the circuit-breaker |
| 594 | + HystrixCommand<Boolean> cmd5 = new SuccessCommand(key, 5000, sleepWindow); |
| 595 | + |
| 596 | + //wait for sleep window to pass |
| 597 | + Thread.sleep(sleepWindow + 50); |
| 598 | + |
| 599 | + Observable<Boolean> o = cmd5.observe(); |
| 600 | + Subscription s = o.subscribe(); |
| 601 | + s.unsubscribe(); |
| 602 | + |
| 603 | + //wait for 10 sleep windows, then try a successful command. this should return the circuit to CLOSED |
| 604 | + |
| 605 | + Thread.sleep(10 * sleepWindow); |
| 606 | + HystrixCommand<Boolean> cmd6 = new SuccessCommand(key, 1, sleepWindow); |
| 607 | + cmd6.execute(); |
| 608 | + |
| 609 | + Thread.sleep(100); |
| 610 | + assertTrue(cb.allowRequest()); |
| 611 | + assertFalse(cb.isOpen()); |
| 612 | + |
| 613 | + } catch (Exception e) { |
| 614 | + e.printStackTrace(); |
| 615 | + fail("Error occurred: " + e.getMessage()); |
| 616 | + } |
| 617 | + } |
| 618 | + |
568 | 619 | /**
|
569 | 620 | * Utility method for creating {@link HystrixCommandMetrics} for unit tests.
|
570 | 621 | */
|
|
0 commit comments