HystrixCollapser implements HystrixExecutable<ResponseType>, HystrixObservable<ResponseType> { public Future<ResponseType> queue() { return toObservable() .toBlocking() .toFuture(); } protected  HystrixCollapser(); protected  HystrixCollapser(HystrixCollapserKey collapserKey); protected  HystrixCollapser(Setter setter);   HystrixCollapser(HystrixCollapserKey collapserKey, Scope scope, CollapserTimer timer, HystrixCollapserProperties.Setter propertiesBuilder);   HystrixCollapser(HystrixCollapserKey collapserKey, Scope scope, CollapserTimer timer, HystrixCollapserProperties.Setter propertiesBuilder, HystrixCollapserMetrics metrics); HystrixCollapserKey getCollapserKey(); Scope getScope(); HystrixCollapserMetrics getMetrics(); abstract RequestArgumentType getRequestArgument(); Observable<ResponseType> observe(); Observable<ResponseType> toObservable(); Observable<ResponseType> toObservable(Scheduler observeOn); ResponseType execute(); Future<ResponseType> queue();  }
@Test public void testMultipleBatches() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestRequestCollapser(timer, 2).queue(); timer.incrementTime(10); assertEquals("1", response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS)); Future<String> response3 = new TestRequestCollapser(timer, 3).queue(); timer.incrementTime(10); assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS)); assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testMaxRequestsInBatch() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1, 2, 10); HystrixCollapser<List<String>, String, String> collapser2 = new TestRequestCollapser(timer, 2, 2, 10); HystrixCollapser<List<String>, String, String> collapser3 = new TestRequestCollapser(timer, 3, 2, 10); System.out.println("*** " + System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Constructed the collapsers"); Future<String> response1 = collapser1.queue(); Future<String> response2 = collapser2.queue(); Future<String> response3 = collapser3.queue(); System.out.println("*** " +System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " queued the collapsers"); timer.incrementTime(10); System.out.println("*** " +System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " incremented the virtual timer"); assertEquals("1", response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS)); System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestsOverTime() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1); Future<String> response1 = collapser1.queue(); timer.incrementTime(5); Future<String> response2 = new TestRequestCollapser(timer, 2).queue(); timer.incrementTime(8); Future<String> response3 = new TestRequestCollapser(timer, 3).queue(); timer.incrementTime(6); Future<String> response4 = new TestRequestCollapser(timer, 4).queue(); timer.incrementTime(8); Future<String> response5 = new TestRequestCollapser(timer, 5).queue(); timer.incrementTime(10); assertEquals("1", response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS)); assertEquals("4", response4.get(1000, TimeUnit.MILLISECONDS)); assertEquals("5", response5.get(1000, TimeUnit.MILLISECONDS)); assertEquals(3, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testUnsubscribeOnOneDoesntKillBatch() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestRequestCollapser(timer, 2).queue(); response1.cancel(true); timer.incrementTime(10); try { response1.get(1000, TimeUnit.MILLISECONDS); fail("expect CancellationException after cancelling"); } catch (CancellationException e) { } assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testShardedRequests() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestShardedRequestCollapser(timer, "1a"); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestShardedRequestCollapser(timer, "2b").queue(); Future<String> response3 = new TestShardedRequestCollapser(timer, "3b").queue(); Future<String> response4 = new TestShardedRequestCollapser(timer, "4a").queue(); timer.incrementTime(10); assertEquals("1a", response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("2b", response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("3b", response3.get(1000, TimeUnit.MILLISECONDS)); assertEquals("4a", response4.get(1000, TimeUnit.MILLISECONDS)); assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestScope() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, "1"); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestRequestCollapser(timer, "2").queue(); RequestCollapserFactory.resetRequest(); Future<String> response3 = new TestRequestCollapser(timer, "3").queue(); Future<String> response4 = new TestRequestCollapser(timer, "4").queue(); timer.incrementTime(10); assertEquals("1", response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS)); assertEquals("4", response4.get(1000, TimeUnit.MILLISECONDS)); assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testGlobalScope() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestGloballyScopedRequestCollapser(timer, "1"); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestGloballyScopedRequestCollapser(timer, "2").queue(); RequestCollapserFactory.resetRequest(); Future<String> response3 = new TestGloballyScopedRequestCollapser(timer, "3").queue(); Future<String> response4 = new TestGloballyScopedRequestCollapser(timer, "4").queue(); timer.incrementTime(10); assertEquals("1", response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS)); assertEquals("4", response4.get(1000, TimeUnit.MILLISECONDS)); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(4, cmdIterator.next().getNumberCollapsed()); }
@Test public void testErrorHandlingViaFutureException() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapserWithFaultyCreateCommand(timer, "1"); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestRequestCollapserWithFaultyCreateCommand(timer, "2").queue(); timer.incrementTime(10); try { response1.get(1000, TimeUnit.MILLISECONDS); fail("we should have received an exception"); } catch (ExecutionException e) { } try { response2.get(1000, TimeUnit.MILLISECONDS); fail("we should have received an exception"); } catch (ExecutionException e) { } assertEquals(0, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); }
@Test public void testErrorHandlingWhenMapToResponseFails() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapserWithFaultyMapToResponse(timer, "1"); Future<String> response1 = collapser1.queue(); Future<String> response2 = new TestRequestCollapserWithFaultyMapToResponse(timer, "2").queue(); timer.incrementTime(10); try { response1.get(1000, TimeUnit.MILLISECONDS); fail("we should have received an exception"); } catch (ExecutionException e) { } try { response2.get(1000, TimeUnit.MILLISECONDS); fail("we should have received an exception"); } catch (ExecutionException e) { } assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestCache1() { final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, "A", true); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, "A", true); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); timer.incrementTime(15); try { assertEquals("A", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("A", f2.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } Future<String> f3 = command1.queue(); timer.incrementTime(15); try { assertEquals("A", f3.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[1])[0]; System.out.println("command.getExecutionEvents(): " + command.getExecutionEvents()); assertEquals(2, command.getExecutionEvents().size()); assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestCache2() { final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, "A", true); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, "B", true); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); timer.incrementTime(15); try { assertEquals("A", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f2.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } Future<String> f3 = command1.queue(); Future<String> f4 = command2.queue(); timer.incrementTime(15); try { assertEquals("A", f3.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f4.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[1])[0]; assertEquals(2, command.getExecutionEvents().size()); assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestCache3() { final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, "A", true); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, "B", true); SuccessfulCacheableCollapsedCommand command3 = new SuccessfulCacheableCollapsedCommand(timer, "B", true); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); Future<String> f3 = command3.queue(); timer.incrementTime(15); try { assertEquals("A", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f3.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } Future<String> f4 = command1.queue(); Future<String> f5 = command2.queue(); Future<String> f6 = command3.queue(); timer.incrementTime(15); try { assertEquals("A", f4.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f5.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f6.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[1])[0]; assertEquals(2, command.getExecutionEvents().size()); assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testNoRequestCache3() { final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, "A", false); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, "B", false); SuccessfulCacheableCollapsedCommand command3 = new SuccessfulCacheableCollapsedCommand(timer, "B", false); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); Future<String> f3 = command3.queue(); timer.incrementTime(15); try { assertEquals("A", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f2.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f3.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } Future<String> f4 = command1.queue(); Future<String> f5 = command2.queue(); Future<String> f6 = command3.queue(); timer.incrementTime(15); try { assertEquals("A", f4.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f5.get(1000, TimeUnit.MILLISECONDS)); assertEquals("B", f6.get(1000, TimeUnit.MILLISECONDS)); } catch (Exception e) { throw new RuntimeException(e); } assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); HystrixInvokableInfo<?> commandA = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[2])[0]; assertEquals(2, commandA.getExecutionEvents().size()); assertTrue(commandA.getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(commandA.getExecutionEvents().contains(HystrixEventType.COLLAPSED)); HystrixInvokableInfo<?> commandB = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[2])[1]; assertEquals(2, commandB.getExecutionEvents().size()); assertTrue(commandB.getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(commandB.getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestCacheWithNullRequestArgument() throws Exception { ConcurrentLinkedQueue<HystrixCommand<List<String>>> commands = new ConcurrentLinkedQueue<HystrixCommand<List<String>>>(); final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, null, true, commands); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, null, true, commands); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); timer.incrementTime(15); assertEquals("NULL", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("NULL", f2.get(1000, TimeUnit.MILLISECONDS)); assertEquals(1, commands.size()); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Future<String> f3 = command1.queue(); timer.incrementTime(15); assertEquals("NULL", f3.get(1000, TimeUnit.MILLISECONDS)); assertEquals(1, commands.size()); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestCacheWithCommandError() { ConcurrentLinkedQueue<HystrixCommand<List<String>>> commands = new ConcurrentLinkedQueue<HystrixCommand<List<String>>>(); final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, "FAILURE", true, commands); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, "FAILURE", true, commands); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); timer.incrementTime(15); try { assertEquals("A", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("A", f2.get(1000, TimeUnit.MILLISECONDS)); fail("exception should have been thrown"); } catch (Exception e) { } assertEquals(1, commands.size()); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.FAILURE)); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Future<String> f3 = command1.queue(); timer.incrementTime(15); try { assertEquals("A", f3.get(1000, TimeUnit.MILLISECONDS)); fail("exception should have been thrown"); } catch (Exception e) { } assertEquals(1, commands.size()); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testRequestCacheWithCommandTimeout() { ConcurrentLinkedQueue<HystrixCommand<List<String>>> commands = new ConcurrentLinkedQueue<HystrixCommand<List<String>>>(); final TestCollapserTimer timer = new TestCollapserTimer(); SuccessfulCacheableCollapsedCommand command1 = new SuccessfulCacheableCollapsedCommand(timer, "TIMEOUT", true, commands); SuccessfulCacheableCollapsedCommand command2 = new SuccessfulCacheableCollapsedCommand(timer, "TIMEOUT", true, commands); Future<String> f1 = command1.queue(); Future<String> f2 = command2.queue(); timer.incrementTime(15); try { assertEquals("A", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("A", f2.get(1000, TimeUnit.MILLISECONDS)); fail("exception should have been thrown"); } catch (Exception e) { } assertEquals(1, commands.size()); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.TIMEOUT)); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Future<String> f3 = command1.queue(); timer.incrementTime(15); try { assertEquals("A", f3.get(1000, TimeUnit.MILLISECONDS)); fail("exception should have been thrown"); } catch (Exception e) { } assertEquals(1, commands.size()); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
@Test public void testVoidResponseTypeFireAndForgetCollapsing1() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); TestCollapserWithVoidResponseType collapser1 = new TestCollapserWithVoidResponseType(timer, 1); Future<Void> response1 = collapser1.queue(); Future<Void> response2 = new TestCollapserWithVoidResponseType(timer, 2).queue(); timer.incrementTime(100); assertEquals(null, response1.get(1000, TimeUnit.MILLISECONDS)); assertEquals(null, response2.get(1000, TimeUnit.MILLISECONDS)); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }
@Test public void testVoidResponseTypeFireAndForgetCollapsing2() throws Exception { TestCollapserTimer timer = new TestCollapserTimer(); TestCollapserWithVoidResponseTypeAndMissingMapResponseToRequests collapser1 = new TestCollapserWithVoidResponseTypeAndMissingMapResponseToRequests(timer, 1); Future<Void> response1 = collapser1.queue(); new TestCollapserWithVoidResponseTypeAndMissingMapResponseToRequests(timer, 2).queue(); timer.incrementTime(100); try { assertEquals(null, response1.get(1000, TimeUnit.MILLISECONDS)); fail("expected an error as mapResponseToRequests did not set responses"); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof IllegalStateException); assertTrue(e.getCause().getMessage().startsWith("No response set by")); } assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(2, cmdIterator.next().getNumberCollapsed()); }