Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests to demonstrate a non-blocking semaphore timeout #669

Merged
merged 2 commits into from
Feb 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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