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

Add more unit tests for HystrixRequestLog to HystrixObservableCommandTest #454

Merged
merged 2 commits into from
Jan 7, 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 @@ -193,6 +193,7 @@ public void testExecutionMultipleTimes() {

// semaphore isolated
assertFalse(command.isExecutedInThread());
assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -870,6 +871,8 @@ public void onNext(Boolean args) {
// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());

assertFalse(command.isResponseFromFallback());
}

Expand Down Expand Up @@ -2184,6 +2187,8 @@ public void testDynamicOwnerFails() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(0, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
} catch (Exception e) {
// success if we get here
}
Expand All @@ -2205,6 +2210,8 @@ public void testDynamicKey() {

// semaphore isolated
assertFalse(command1.isExecutedInThread());

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
} catch (Exception e) {
e.printStackTrace();
fail("We received an exception.");
Expand Down Expand Up @@ -2447,8 +2454,6 @@ public void testRequestCacheWithSlowExecution() {

assertEquals(4, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());

System.out.println("HystrixRequestLog: " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());

// semaphore isolated
assertFalse(command1.isExecutedInThread());
assertFalse(command2.isExecutedInThread());
Expand Down Expand Up @@ -3025,7 +3030,7 @@ public void testRequestCacheOnThreadRejectionThrowsException() throws Exception

assertEquals(100, circuitBreaker.metrics.getHealthCounts().getErrorPercentage());

// assertEquals(4, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size());
assertEquals(4, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3054,6 +3059,13 @@ public void testBasicExecutionWorksWithoutRequestVariable() {
e.printStackTrace();
fail("We received an exception => " + e.getMessage());
}

try {
HystrixRequestLog.getCurrentRequest();
fail("Should not have a RequestLog when RequestContext not initialized");
} catch (IllegalStateException ise) {
//expected
}
}

/**
Expand All @@ -3080,6 +3092,13 @@ public void testCacheKeyExecutionRequiresRequestVariable() {
} catch (Exception e) {
e.printStackTrace();
}

try {
HystrixRequestLog.getCurrentRequest();
fail("Should not have a RequestLog when RequestContext not initialized");
} catch (IllegalStateException ise) {
//expected
}
}

/**
Expand All @@ -3102,6 +3121,8 @@ public void testBadRequestExceptionViaExecuteInThread() {
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand All @@ -3128,6 +3149,8 @@ public void testBadRequestExceptionViaQueueInThread() {
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3162,6 +3185,8 @@ public void testBadRequestExceptionViaQueueInThreadOnResponseFromCache() {
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand All @@ -3184,6 +3209,8 @@ public void testBadRequestExceptionViaExecuteInSemaphore() {
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand All @@ -3210,6 +3237,8 @@ public void testBadRequestExceptionViaQueueInSemaphore() {
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand All @@ -3234,6 +3263,8 @@ public void testCheckedExceptionViaExecute() {
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(1, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));
assertEquals(1, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3289,6 +3320,8 @@ public void onNext(Boolean args) {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3319,6 +3352,8 @@ public void testErrorThrownViaExecute() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3350,6 +3385,8 @@ public void testErrorThrownViaQueue() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3407,6 +3444,8 @@ public void onNext(Boolean args) {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3478,6 +3517,8 @@ public void testExecutionHookSuccessfulCommand() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3531,6 +3572,8 @@ public void testExecutionHookSuccessfulCommandViaFireAndForget() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3578,6 +3621,8 @@ public void testExecutionHookSuccessfulCommandWithMultipleGetsOnFuture() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3660,6 +3705,7 @@ public void testExecutionHookRunFailureWithoutFallback() {
// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3731,6 +3777,8 @@ public void testExecutionHookRunFailureWithFallback() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3812,6 +3860,8 @@ public void testExecutionHookRunFailureWithFallbackFailure() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3851,19 +3901,10 @@ public void testExecutionHookTimeoutWithoutFallback() {
// timeout failure
assertEquals(FailureType.TIMEOUT, command.builder.executionHook.endExecuteFailureType);

// thread execution
// assertEquals(1, command.builder.executionHook.threadStart.get());

// we need to wait for the thread to complete before the onThreadComplete hook will be called
// try {
// Thread.sleep(400);
// } catch (InterruptedException e) {
// // ignore
// }
// assertEquals(1, command.builder.executionHook.threadComplete.get());

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -3912,6 +3953,8 @@ public void testExecutionHookTimeoutWithFallback() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -4013,6 +4056,8 @@ public void testExecutionHookShortCircuitedWithFallbackViaQueue() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -4061,6 +4106,8 @@ public void testExecutionHookShortCircuitedWithFallbackViaExecute() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -4136,6 +4183,8 @@ public void testExecutionHookSuccessfulCommandWithSemaphoreIsolation() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -4187,6 +4236,8 @@ public void testExecutionHookFailureWithSemaphoreIsolation() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down Expand Up @@ -4228,13 +4279,13 @@ public void testExecutionFailureWithFallbackImplementedButDisabled() {

assertEquals(100, commandDisabled.builder.metrics.getHealthCounts().getErrorPercentage());

// assertEquals(2, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size());
assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
* Test that we can still use thread isolation if desired.
*/
@Test(timeout = 500)
@Test
public void testSynchronousExecutionTimeoutValueViaExecute() {
HystrixObservableCommand.Setter properties = HystrixObservableCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey"))
Expand All @@ -4247,6 +4298,7 @@ public void testSynchronousExecutionTimeoutValueViaExecute() {
HystrixObservableCommand<String> command = new HystrixObservableCommand<String>(properties) {
@Override
protected Observable<String> construct() {

return Observable.create(new OnSubscribe<String>() {

@Override
Expand Down Expand Up @@ -4281,9 +4333,11 @@ protected Observable<String> resumeWithFallback() {

// Thread isolated
assertTrue(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

@Test(timeout = 500)
@Test
public void testSynchronousExecutionUsingThreadIsolationTimeoutValueViaObserve() {
HystrixObservableCommand.Setter properties = HystrixObservableCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey"))
Expand Down Expand Up @@ -4326,9 +4380,11 @@ protected Observable<String> resumeWithFallback() {

// Thread isolated
assertTrue(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

@Test(timeout = 500)
@Test
public void testAsyncExecutionTimeoutValueViaObserve() {
HystrixObservableCommand.Setter properties = HystrixObservableCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey"))
Expand Down Expand Up @@ -4371,6 +4427,8 @@ protected Observable<String> resumeWithFallback() {

// semaphore isolated
assertFalse(command.isExecutedInThread());

assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

/**
Expand Down