Skip to content

Commit

Permalink
Failing unit tests for timeouts on HystrixCommands not causing thread…
Browse files Browse the repository at this point in the history
… interruptions

Conflicts:
	hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java
  • Loading branch information
Matt Jacobs committed Feb 6, 2015
1 parent 10c221a commit 24c01e8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,6 @@ public void tick() {

timeoutRunnable.run();
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5273,7 +5273,7 @@ public void call(Throwable t1) {
}

@Test
public void testExceptionConvertedToBadRequestExceptionInExecutionHookBypassesCircuitBreaker(){
public void testExceptionConvertedToBadRequestExceptionInExecutionHookBypassesCircuitBreaker() {
TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
try {
new ExceptionToBadRequestByExecutionHookCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD).execute();
Expand All @@ -5292,6 +5292,33 @@ public void testExceptionConvertedToBadRequestExceptionInExecutionHookBypassesCi
assertEquals(1, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.BAD_REQUEST));
}

@Test
public void testInterruptFutureOnTimeout() throws InterruptedException, ExecutionException {
// given
InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker());

// when
Future<Boolean> f = cmd.queue();

// then
Thread.sleep(3000);
System.out.println("RESULT : " + f.get());
assertTrue(cmd.hasBeenInterrupted());
}

@Test
public void testInterruptObservableOnTimeout() throws InterruptedException {
// given
InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker());

// when
cmd.observe().subscribe();

// then
Thread.sleep(3000);
assertTrue(cmd.hasBeenInterrupted());
}

/* ******************************************************************************** */
/* ******************************************************************************** */
/* private HystrixCommand class implementations for unit testing */
Expand Down Expand Up @@ -6288,6 +6315,39 @@ protected Boolean run() throws Exception {

}

private static class InterruptibleCommand extends TestHystrixCommand<Boolean> {

public InterruptibleCommand(TestCircuitBreaker circuitBreaker) {
super(testPropsBuilder()
.setCircuitBreaker(circuitBreaker).setMetrics(circuitBreaker.metrics)
.setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter()
.withExecutionIsolationThreadInterruptOnTimeout(true)
.withExecutionIsolationThreadTimeoutInMilliseconds(100)));
}

private volatile boolean hasBeenInterrupted;

public boolean hasBeenInterrupted()
{
return hasBeenInterrupted;
}

@Override
protected Boolean run() throws Exception
{
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
System.out.println("Interrupted!");
e.printStackTrace();
hasBeenInterrupted = true;
}

return hasBeenInterrupted;
}
}

enum CommandKeyForUnitTest implements HystrixCommandKey {
KEY_ONE, KEY_TWO
}
Expand Down

0 comments on commit 24c01e8

Please sign in to comment.