From fa3581339cc74d021874d5272967fa2099c53b61 Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Tue, 16 Aug 2016 07:56:54 +0100 Subject: [PATCH] Minor changes after review --- .../hystrix/HystrixTimeShortCircuitTest.java | 74 +++++++++++++++++++ .../baeldung/hystrix/HystrixTimeoutTest.java | 49 +----------- 2 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java diff --git a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java new file mode 100644 index 0000000000..a5303e6c0d --- /dev/null +++ b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeShortCircuitTest.java @@ -0,0 +1,74 @@ +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 e85f3d7199..878d7808a0 100644 --- a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java +++ b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java @@ -5,14 +5,14 @@ import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandProperties; import com.netflix.hystrix.HystrixThreadPoolProperties; import com.netflix.hystrix.exception.HystrixRuntimeException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +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 HystrixTimeoutTest { private HystrixCommand.Setter config; @@ -85,47 +85,4 @@ public class HystrixTimeoutTest { assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), equalTo("Success")); } - - @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")); - } - - public 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; - } - }