diff --git a/hystrix/pom.xml b/hystrix/pom.xml index 7867bbb955..42828e1c97 100644 --- a/hystrix/pom.xml +++ b/hystrix/pom.xml @@ -62,11 +62,11 @@ hystrix-metrics-event-stream ${hystrix-metrics-event-stream.version} - + com.netflix.rxjava rxjava-core diff --git a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java deleted file mode 100644 index a5303e6c0d..0000000000 --- a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.baeldung.hystrix; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import com.netflix.hystrix.HystrixCommandProperties; -import com.netflix.hystrix.HystrixThreadPoolProperties; -import com.netflix.hystrix.exception.HystrixRuntimeException; -import org.junit.*; -import org.junit.rules.ExpectedException; -import org.junit.runners.MethodSorters; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - -@FixMethodOrder(MethodSorters.JVM) -public class HystrixTimeShortCircuitTest { - - private HystrixCommand.Setter config; - private HystrixCommandProperties.Setter commandProperties; - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Before - public void setup() { - commandProperties = HystrixCommandProperties.Setter(); - config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); - } - - @Test - public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess() - throws InterruptedException { - - commandProperties.withExecutionTimeoutInMilliseconds(1000); - - commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000); - commandProperties.withExecutionIsolationStrategy( - HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); - commandProperties.withCircuitBreakerEnabled(true); - commandProperties.withCircuitBreakerRequestVolumeThreshold(1); - - config.andCommandPropertiesDefaults(commandProperties); - - config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() - .withMaxQueueSize(1) - .withCoreSize(1) - .withQueueSizeRejectionThreshold(1)); - - assertThat(this.invokeRemoteService(10000), equalTo(null)); - assertThat(this.invokeRemoteService(10000), equalTo(null)); - Thread.sleep(5000); - - assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); - assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); - assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); - } - - String invokeRemoteService(long timeout) throws InterruptedException { - String response = null; - try { - response = new RemoteServiceTestCommand(config, - new RemoteServiceTestSimulator(timeout)).execute(); - } catch (HystrixRuntimeException ex) { - System.out.println("ex = " + ex); - } - return response; - } - -} diff --git a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java index 878d7808a0..0922cf59fe 100644 --- a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java +++ b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java @@ -12,24 +12,8 @@ import org.junit.runners.MethodSorters; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; -@FixMethodOrder(MethodSorters.JVM) public class HystrixTimeoutTest { - private HystrixCommand.Setter config; - private HystrixCommandProperties.Setter commandProperties; - - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Before - public void setup() { - commandProperties = HystrixCommandProperties.Setter(); - config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); - } - @Test public void givenInputBobAndDefaultSettings_whenExecuted_thenReturnHelloBob(){ assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!")); @@ -38,24 +22,30 @@ public class HystrixTimeoutTest { @Test public void givenSvcTimeoutOf100AndDefaultSettings_whenExecuted_thenReturnSuccess() throws InterruptedException { - HystrixCommand.Setter config = HystrixCommand .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2")); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), equalTo("Success")); } - @Test + @Test(expected = HystrixRuntimeException.class) public void givenSvcTimeoutOf10000AndDefaultSettings__whenExecuted_thenExpectHRE() throws InterruptedException { - exception.expect(HystrixRuntimeException.class); + HystrixCommand.Setter config = HystrixCommand + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3")); new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute(); } @Test public void givenSvcTimeoutOf5000AndExecTimeoutOf10000__whenExecuted_thenReturnSuccess() throws InterruptedException { + + HystrixCommand.Setter config = HystrixCommand + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4")); + HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); commandProperties.withExecutionTimeoutInMilliseconds(10_000); config.andCommandPropertiesDefaults(commandProperties); @@ -63,10 +53,13 @@ public class HystrixTimeoutTest { equalTo("Success")); } - @Test + @Test(expected = HystrixRuntimeException.class) public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE() throws InterruptedException { - exception.expect(HystrixRuntimeException.class); + HystrixCommand.Setter config = HystrixCommand + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5")); + HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); commandProperties.withExecutionTimeoutInMilliseconds(5_000); config.andCommandPropertiesDefaults(commandProperties); new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(15_000)).execute(); @@ -75,6 +68,11 @@ public class HystrixTimeoutTest { @Test public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess() throws InterruptedException { + + HystrixCommand.Setter config = HystrixCommand + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool")); + HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); commandProperties.withExecutionTimeoutInMilliseconds(10_000); config.andCommandPropertiesDefaults(commandProperties); config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() @@ -85,4 +83,52 @@ public class HystrixTimeoutTest { assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), equalTo("Success")); } + + @Test + public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess() + throws InterruptedException { + + HystrixCommand.Setter config = HystrixCommand + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker")); + HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); + commandProperties.withExecutionTimeoutInMilliseconds(1000); + + commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000); + commandProperties.withExecutionIsolationStrategy( + HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); + commandProperties.withCircuitBreakerEnabled(true); + commandProperties.withCircuitBreakerRequestVolumeThreshold(1); + + config.andCommandPropertiesDefaults(commandProperties); + + config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() + .withMaxQueueSize(1) + .withCoreSize(1) + .withQueueSizeRejectionThreshold(1)); + + assertThat(this.invokeRemoteService(config, 10_000), equalTo(null)); + assertThat(this.invokeRemoteService(config, 10_000), equalTo(null)); + assertThat(this.invokeRemoteService(config, 10_000), equalTo(null)); + Thread.sleep(5000); + + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), + equalTo("Success")); + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), + equalTo("Success")); + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), + equalTo("Success")); + } + + public String invokeRemoteService(HystrixCommand.Setter config, int timeout) + throws InterruptedException { + String response = null; + try { + response = new RemoteServiceTestCommand(config, + new RemoteServiceTestSimulator(timeout)).execute(); + } catch (HystrixRuntimeException ex) { + System.out.println("ex = " + ex); + } + return response; + } }