Skip to content

Commit

Permalink
Merge pull request #669 from mattrjacobs/add-semaphore-timeout-test
Browse files Browse the repository at this point in the history
Added unit tests to demonstrate a non-blocking semaphore timeout
  • Loading branch information
mattrjacobs committed Feb 11, 2015
2 parents 2fe5a19 + 61a24a7 commit 19320db
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,29 +580,37 @@ public void testSemaphoreIsolatedObserveTimeoutWithFailureAndFallback() {
*/
@Test
public void testThreadIsolatedObserveTimeoutWithSuccessAndFallback() {
testObserveFailureWithTimeoutAndFallback(ExecutionIsolationStrategy.SEMAPHORE, TestCommandWithTimeout.RESULT_SUCCESS);
testObserveFailureWithTimeoutAndFallback(ExecutionIsolationStrategy.THREAD, TestCommandWithTimeout.RESULT_SUCCESS);
}

/**
* Test a thread command execution that times out with a fallback and eventually fails.
*/
@Test
public void testThreadIsolatedObserveTimeoutWithFailureAndFallback() {
testObserveFailureWithTimeoutAndFallback(ExecutionIsolationStrategy.SEMAPHORE, TestCommandWithTimeout.RESULT_EXCEPTION);
testObserveFailureWithTimeoutAndFallback(ExecutionIsolationStrategy.THREAD, TestCommandWithTimeout.RESULT_EXCEPTION);
}

private void testObserveFailureWithTimeoutAndFallback(ExecutionIsolationStrategy isolationStrategy, int executionResult) {
TestHystrixCommand<Boolean> command = new TestCommandWithTimeout(2000, TestCommandWithTimeout.FALLBACK_SUCCESS, isolationStrategy, executionResult, true);
TestHystrixCommand<Boolean> command = new TestCommandWithTimeout(200, TestCommandWithTimeout.FALLBACK_SUCCESS, isolationStrategy, executionResult, true);
long observedCommandDuration = 0;
try {
long startTime = System.currentTimeMillis();
assertEquals(false, command.observe().toBlocking().single());
observedCommandDuration = System.currentTimeMillis() - startTime;
} catch (Exception e) {
e.printStackTrace();
fail("We should have received a response from the fallback.");
}

assertNull(command.getFailedExecutionException());

assertTrue(command.getExecutionTimeInMilliseconds() > -1);
System.out.println("Command time : " + command.getExecutionTimeInMilliseconds());
System.out.println("Observed command time : " + observedCommandDuration);
assertTrue(command.getExecutionTimeInMilliseconds() >= 200);
assertTrue(observedCommandDuration >= 200);
assertTrue(command.getExecutionTimeInMilliseconds() < 1000);
assertTrue(observedCommandDuration < 1000);
assertFalse(command.isFailedExecution());
assertTrue(command.isResponseFromFallback());

Expand Down Expand Up @@ -7781,7 +7789,11 @@ protected Observable<Boolean> construct() {
public void call(Subscriber<? super Boolean> sub) {
System.out.println("***** running");
try {
Thread.sleep(timeout * 10);
for (int i = 0; i < (timeout * 5); i++) {
if (!sub.isUnsubscribed()) {
Thread.sleep(1);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
// ignore and sleep some more to simulate a dependency that doesn't obey interrupts
Expand Down

0 comments on commit 19320db

Please sign in to comment.