/** * Test command that uses a null request argument */ @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(); // increment past batch time so it executes timer.incrementTime(15); assertEquals("NULL", f1.get(1000, TimeUnit.MILLISECONDS)); assertEquals("NULL", f2.get(1000, TimeUnit.MILLISECONDS)); // it should have executed 1 command assertEquals(1, commands.size()); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.SUCCESS)); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.COLLAPSED)); Future<String> f3 = command1.queue(); // increment past batch time so it executes timer.incrementTime(15); assertEquals("NULL", f3.get(1000, TimeUnit.MILLISECONDS)); // it should still be 1 ... no new executions assertEquals(1, commands.size()); assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator(); assertEquals(1, cmdIterator.next().getNumberCollapsed()); }
assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.TIMEOUT)); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.COLLAPSED));
assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.FAILURE)); assertTrue(commands.peek().getExecutionEvents().contains(HystrixEventType.COLLAPSED));
/** * Tests that the circuit-breaker reports itself as "OPEN" if set as forced-open */ @Test public void testCircuitBreakerReportsOpenIfForcedOpen() { HystrixCommand<Boolean> cmd = new HystrixCommand<Boolean>(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")).andCommandPropertiesDefaults(new HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(true))) { @Override protected Boolean run() throws Exception { return true; } @Override protected Boolean getFallback() { return false; } }; assertFalse(cmd.execute()); //fallback should fire System.out.println("RESULT : " + cmd.getExecutionEvents()); assertTrue(cmd.isCircuitBreakerOpen()); }
/** * Tests that the circuit-breaker reports itself as "CLOSED" if set as forced-closed */ @Test public void testCircuitBreakerReportsClosedIfForcedClosed() { HystrixCommand<Boolean> cmd = new HystrixCommand<Boolean>(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")).andCommandPropertiesDefaults( new HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(false).withCircuitBreakerForceClosed(true))) { @Override protected Boolean run() throws Exception { return true; } @Override protected Boolean getFallback() { return false; } }; assertTrue(cmd.execute()); System.out.println("RESULT : " + cmd.getExecutionEvents()); assertFalse(cmd.isCircuitBreakerOpen()); }
/** * Copies various parameters like execution events,time and any exception after command execution.<br/><br/> * <b>Should be called only after execution of command completes</b> * @param executor Executor used to execute this request. * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public Builder withCommandData(Executor executor) { HystrixCommand command = (HystrixCommand) executor; withEventList(command.getExecutionEvents()) .withExecutionTime(command.getExecutionTimeInMilliseconds()) .withException((Exception) command.getFailedExecutionException()); return this; }