@Test public void testFallback() { HystrixCommand<Integer> superCmd = new SuperCommand("cache", false); assertEquals(2, superCmd.execute().intValue()); HystrixCommand<Integer> subNoOverridesCmd = new SubCommandNoOverride("cache", false); assertEquals(2, subNoOverridesCmd.execute().intValue()); HystrixCommand<Integer> subOverriddenFallbackCmd = new SubCommandOverrideFallback("cache", false); assertEquals(3, subOverriddenFallbackCmd.execute().intValue()); }
@Test public void testRequestLogSuperClass() { HystrixCommand<Integer> superCmd = new SuperCommand("cache", true); assertEquals(1, superCmd.execute().intValue()); System.out.println("REQ LOG : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); HystrixRequestLog reqLog = HystrixRequestLog.getCurrentRequest(); assertEquals(1, reqLog.getAllExecutedCommands().size()); HystrixInvokableInfo<?> info = reqLog.getAllExecutedCommands().iterator().next(); assertEquals("SuperCommand", info.getCommandKey().name()); }
@Test public void testRequestLogSubClassNoOverrides() { HystrixCommand<Integer> subCmd = new SubCommandNoOverride("cache", true); assertEquals(1, subCmd.execute().intValue()); System.out.println("REQ LOG : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); HystrixRequestLog reqLog = HystrixRequestLog.getCurrentRequest(); assertEquals(1, reqLog.getAllExecutedCommands().size()); HystrixInvokableInfo<?> info = reqLog.getAllExecutedCommands().iterator().next(); assertEquals("SubCommandNoOverride", info.getCommandKey().name()); }
/** * Test that if all 'marks' are timeouts that it will trip the circuit. */ @Test public void testTripCircuitOnTimeouts() { String key = "cmd-D"; try { HystrixCommand<Boolean> cmd1 = new TimeoutCommand(key); HystrixCircuitBreaker cb = cmd1.circuitBreaker; // this should start as allowing requests assertTrue(cb.allowRequest()); assertFalse(cb.isOpen()); // success with high latency cmd1.execute(); HystrixCommand<Boolean> cmd2 = new TimeoutCommand(key); cmd2.execute(); HystrixCommand<Boolean> cmd3 = new TimeoutCommand(key); cmd3.execute(); HystrixCommand<Boolean> cmd4 = new TimeoutCommand(key); cmd4.execute(); // everything has been a timeout so we should not allow any requests Thread.sleep(100); assertFalse(cb.allowRequest()); assertTrue(cb.isOpen()); } catch (Exception e) { e.printStackTrace(); fail("Error occurred: " + e.getMessage()); } }
@Test public void testRequestCacheSubclassNoOverrides() { HystrixCommand<Integer> subCmd1 = new SubCommandNoOverride("cache", true); assertEquals(1, subCmd1.execute().intValue()); HystrixCommand<Integer> subCmd2 = new SubCommandNoOverride("cache", true); assertEquals(1, subCmd2.execute().intValue()); HystrixCommand<Integer> subCmd3 = new SubCommandNoOverride("no-cache", true); assertEquals(1, subCmd3.execute().intValue()); System.out.println("REQ LOG : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); HystrixRequestLog reqLog = HystrixRequestLog.getCurrentRequest(); assertEquals(3, reqLog.getAllExecutedCommands().size()); List<HystrixInvokableInfo<?>> infos = new ArrayList<HystrixInvokableInfo<?>>(reqLog.getAllExecutedCommands()); HystrixInvokableInfo<?> info1 = infos.get(0); assertEquals("SubCommandNoOverride", info1.getCommandKey().name()); assertEquals(1, info1.getExecutionEvents().size()); HystrixInvokableInfo<?> info2 = infos.get(1); assertEquals("SubCommandNoOverride", info2.getCommandKey().name()); assertEquals(2, info2.getExecutionEvents().size()); assertEquals(HystrixEventType.RESPONSE_FROM_CACHE, info2.getExecutionEvents().get(1)); HystrixInvokableInfo<?> info3 = infos.get(2); assertEquals("SubCommandNoOverride", info3.getCommandKey().name()); assertEquals(1, info3.getExecutionEvents().size()); }
@Test public void testRequestCacheSuperClass() { HystrixCommand<Integer> superCmd1 = new SuperCommand("cache", true); assertEquals(1, superCmd1.execute().intValue()); HystrixCommand<Integer> superCmd2 = new SuperCommand("cache", true); assertEquals(1, superCmd2.execute().intValue()); HystrixCommand<Integer> superCmd3 = new SuperCommand("no-cache", true); assertEquals(1, superCmd3.execute().intValue()); System.out.println("REQ LOG : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); HystrixRequestLog reqLog = HystrixRequestLog.getCurrentRequest(); assertEquals(3, reqLog.getAllExecutedCommands().size()); List<HystrixInvokableInfo<?>> infos = new ArrayList<HystrixInvokableInfo<?>>(reqLog.getAllExecutedCommands()); HystrixInvokableInfo<?> info1 = infos.get(0); assertEquals("SuperCommand", info1.getCommandKey().name()); assertEquals(1, info1.getExecutionEvents().size()); HystrixInvokableInfo<?> info2 = infos.get(1); assertEquals("SuperCommand", info2.getCommandKey().name()); assertEquals(2, info2.getExecutionEvents().size()); assertEquals(HystrixEventType.RESPONSE_FROM_CACHE, info2.getExecutionEvents().get(1)); HystrixInvokableInfo<?> info3 = infos.get(2); assertEquals("SuperCommand", info3.getCommandKey().name()); assertEquals(1, info3.getExecutionEvents().size()); }
@Override protected Boolean run() { assertEquals("OuterCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Outer Thread : " + Thread.currentThread().getName()); //should be a single execution on this thread assertEquals(1, Hystrix.getCommandCount()); if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("BEFORE expected it to run inside a thread"); } HystrixCommand<Boolean> command2 = new HystrixCommand<Boolean>(Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("InnerCommand"))) { @Override protected Boolean run() { assertEquals("InnerCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Inner Thread : " + Thread.currentThread().getName()); //should be a single execution on this thread, since outer/inner are on different threads assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("AFTER expected it to run inside a thread"); } return command2.execute(); }
/** * 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()); }
@Test public void testExecutionTimeoutValue() { HystrixCommand.Setter properties = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(50)); HystrixCommand<String> command = new HystrixCommand<String>(properties) { @Override protected String run() throws Exception { Thread.sleep(3000); // should never reach here return "hello"; } @Override protected String getFallback() { if (isResponseTimedOut()) { return "timed-out"; } else { return "abc"; } } }; String value = command.execute(); assertTrue(command.isResponseTimedOut()); assertEquals("expected fallback value", "timed-out", value); }
@Test public void testInsideHystrixSemaphoreExecute() { HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("SemaphoreIsolatedCommandName")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) { @Override protected Boolean run() { assertEquals("SemaphoreIsolatedCommandName", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread (since it's busy here) assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; // it should be true for semaphore isolation as well assertTrue(command.execute()); // and then be null again once done assertNull(Hystrix.getCurrentThreadExecutingCommand()); }
@Test public void testInsideHystrixThreadViaExecute() { assertNull(Hystrix.getCurrentThreadExecutingCommand()); HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("CommandName"))) { @Override protected Boolean run() { assertEquals("CommandName", Hystrix.getCurrentThreadExecutingCommand().name()); assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; assertTrue(command.execute()); assertNull(Hystrix.getCurrentThreadExecutingCommand()); assertEquals(0, Hystrix.getCommandCount()); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : false * HystrixCommand ** useRequestCache : true ** useRequestLog : true * * OUTCOME: RequestLog not set up in command, not available statically */ @Test public void testCommandRequiresContextConcurrencyStrategyDoesNotProvideItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); //command execution not affected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : true * HystrixCommand ** useRequestCache : true ** useRequestLog : true * * OUTCOME: RequestLog set up properly in command */ @Test public void testCommandRequiresContextConcurrencyStrategyProvidesItContextSetUpCorrectly() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is set up properly HystrixRequestContext context = HystrixRequestContext.initializeContext(); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); printRequestLog(); assertNotNull(HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertNotNull(cmd.currentRequestLog); context.shutdown(); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : true * HystrixCommand ** useRequestCache : true ** useRequestLog : true * * OUTCOME: RequestLog not set up properly in command, static access is null */ @Test public void testCommandRequiresContextConcurrencyStrategyProvidesItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); //command execution not affected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : true * HystrixCommand ** useRequestCache : false ** useRequestLog : false * * OUTCOME: RequestLog not set up in command, static access is null */ @Test public void testCommandDoesNotRequireContextConcurrencyStrategyProvidesItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(false, false); assertTrue(cmd.execute()); //command execution not affected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : false * HystrixCommand ** useRequestCache : false ** useRequestLog : false * * OUTCOME: RequestLog not set up in command, not available statically */ @Test public void testCommandDoesNotRequireContextConcurrencyStrategyDoesNotProvideItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); //command execution unaffected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : false * HystrixCommand ** useRequestCache : false ** useRequestLog : false * * OUTCOME: RequestLog not set up in command, not available statically */ @Test public void testCommandDoesNotRequireContextConcurrencyStrategyDoesNotProvideItContextSetUpCorrectly() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is set up properly HystrixRequestContext context = HystrixRequestContext.initializeContext(); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); context.shutdown(); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : false * HystrixCommand ** useRequestCache : true ** useRequestLog : true * * OUTCOME: RequestLog not set up in command, not available statically */ @Test public void testCommandRequiresContextConcurrencyStrategyDoesNotProvideItContextSetUpCorrectly() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is set up properly HystrixRequestContext context = HystrixRequestContext.initializeContext(); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); context.shutdown(); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : true * HystrixCommand ** useRequestCache : false ** useRequestLog : false * * OUTCOME: RequestLog not set up in command, static access works properly */ @Test public void testCommandDoesNotRequireContextConcurrencyStrategyProvidesItContextSetUpCorrectly() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is set up properly HystrixRequestContext context = HystrixRequestContext.initializeContext(); HystrixCommand<Boolean> cmd = new TestCommand(false, false); assertTrue(cmd.execute()); printRequestLog(); assertNotNull(HystrixRequestLog.getCurrentRequest()); assertNotNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); context.shutdown(); }