Skip to content

Commit c99c515

Browse files
committed
Merge pull request Netflix#1065 from mattrjacobs/deflake-rejected-hc-test
Made HystrixCommandTest.testRejectedThreadWithFallbackFailure more repeatable
2 parents 7b54e5f + bd40f20 commit c99c515

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,27 +1062,16 @@ public void testRejectedThreadWithFallback() {
10621062
public void testRejectedThreadWithFallbackFailure() {
10631063
TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
10641064
SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(1);
1065-
// fill up the queue
1066-
pool.queue.add(new Runnable() {
1067-
1068-
@Override
1069-
public void run() {
1070-
System.out.println("**** queue filler1 ****");
1071-
try {
1072-
Thread.sleep(500);
1073-
} catch (InterruptedException e) {
1074-
e.printStackTrace();
1075-
}
1076-
}
1077-
1078-
});
10791065

1080-
TestCommandRejection command1 = new TestCommandRejection(circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_FAILURE);
1081-
TestCommandRejection command2 = new TestCommandRejection(circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_FAILURE);
1066+
TestCommandRejection command1 = new TestCommandRejection(circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_FAILURE); //this should pass through the queue and sit in the pool
1067+
TestCommandRejection command2 = new TestCommandRejection(circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_SUCCESS); //this should sit in the queue
1068+
TestCommandRejection command3 = new TestCommandRejection(circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_FAILURE); //this should observe full queue and get rejected
10821069
Future<Boolean> f1 = null;
1070+
Future<Boolean> f2 = null;
10831071
try {
10841072
f1 = command1.queue();
1085-
assertEquals(false, command2.queue().get());
1073+
f2 = command2.queue();
1074+
assertEquals(false, command3.queue().get()); //should get thread-pool rejected
10861075
fail("we shouldn't get here");
10871076
} catch (Exception e) {
10881077
e.printStackTrace();
@@ -1099,17 +1088,20 @@ public void run() {
10991088
}
11001089

11011090
assertCommandExecutionEvents(command1); //still in-flight, no events yet
1102-
assertCommandExecutionEvents(command2, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_FAILURE);
1091+
assertCommandExecutionEvents(command2); //still in-flight, no events yet
1092+
assertCommandExecutionEvents(command3, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_FAILURE);
11031093
assertEquals(1, circuitBreaker.metrics.getCurrentConcurrentExecutionCount()); //pool-filler still going
1104-
//This is a case where we knowingly walk away from an executing Hystrix thread (the pool-filler). It should have an in-flight status ("Executed"). You should avoid this in a production environment
1094+
//This is a case where we knowingly walk away from executing Hystrix threads. They should have an in-flight status ("Executed"). You should avoid this in a production environment
11051095
HystrixRequestLog requestLog = HystrixRequestLog.getCurrentRequest();
1106-
assertEquals(2, requestLog.getAllExecutedCommands().size());
1096+
assertEquals(3, requestLog.getAllExecutedCommands().size());
11071097
assertTrue(requestLog.getExecutedCommandsAsString().contains("Executed"));
11081098

11091099
try {
1110-
//block on the outstanding work, so we don't inadvertently afect any other tests
1100+
//block on the outstanding work, so we don't inadvertently affect any other tests
11111101
long startTime = System.currentTimeMillis();
11121102
f1.get();
1103+
f2.get();
1104+
assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
11131105
System.out.println("Time blocked : " + (System.currentTimeMillis() - startTime));
11141106
} catch (Exception ex) {
11151107
fail("Exception while blocking on Future");

0 commit comments

Comments
 (0)