public static class UnitTest {
@Test
public void testCollapser() throws Exception {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
Future<String> f1 = new CommandCollapserGetValueForKey(1).queue();
Future<String> f2 = new CommandCollapserGetValueForKey(2).queue();
Future<String> f3 = new CommandCollapserGetValueForKey(3).queue();
Future<String> f4 = new CommandCollapserGetValueForKey(4).queue();
assertEquals("ValueForKey: 1", f1.get());
assertEquals("ValueForKey: 2", f2.get());
assertEquals("ValueForKey: 3", f3.get());
assertEquals("ValueForKey: 4", f4.get());
int numExecuted = HystrixRequestLog.getCurrentRequest().getExecutedCommands().size();
System.err.println("num executed: " + numExecuted);
// assert that the batch command 'GetValueForKey' was in fact executed and that it executed only
// once or twice (due to non-determinism of scheduler since this example uses the real timer)
if (numExecuted > 2) {
fail("some of the commands should have been collapsed");
}
System.err.println("HystrixRequestLog.getCurrentRequest().getExecutedCommands(): " + HystrixRequestLog.getCurrentRequest().getExecutedCommands());
System.err.println("HystrixRequestLog.getCurrentRequest().getAllExecutedCommands(): " + HystrixRequestLog.getCurrentRequest().getAllExecutedCommands());
int numLogs = 0;
for (HystrixCommand<?> command : HystrixRequestLog.getCurrentRequest().getExecutedCommands()) {
numLogs++;
// assert the command is the one we're expecting
assertEquals("GetValueForKey", command.getCommandKey().name());
System.err.println(command.getCommandKey().name() + " => command.getExecutionEvents(): " + command.getExecutionEvents());
// confirm that it was a COLLAPSED command execution
assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
}
assertEquals(numExecuted, numLogs);
} finally {
context.shutdown();
}
}