|
| 1 | +package com.baeldung.hystrix; |
| 2 | + |
| 3 | +import com.netflix.hystrix.HystrixCommand; |
| 4 | +import com.netflix.hystrix.HystrixCommandGroupKey; |
| 5 | +import com.netflix.hystrix.HystrixCommandProperties; |
| 6 | +import com.netflix.hystrix.HystrixThreadPoolProperties; |
| 7 | +import com.netflix.hystrix.exception.HystrixRuntimeException; |
| 8 | +import org.junit.*; |
| 9 | +import org.junit.rules.ExpectedException; |
| 10 | +import org.junit.runners.MethodSorters; |
| 11 | + |
| 12 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 13 | +import static org.hamcrest.Matchers.equalTo; |
| 14 | + |
| 15 | +@FixMethodOrder(MethodSorters.JVM) |
| 16 | +public class HystrixTimeShortCircuitTest { |
| 17 | + |
| 18 | + private HystrixCommand.Setter config; |
| 19 | + private HystrixCommandProperties.Setter commandProperties; |
| 20 | + |
| 21 | + @Rule |
| 22 | + public final ExpectedException exception = ExpectedException.none(); |
| 23 | + |
| 24 | + @Before |
| 25 | + public void setup() { |
| 26 | + commandProperties = HystrixCommandProperties.Setter(); |
| 27 | + config = HystrixCommand |
| 28 | + .Setter |
| 29 | + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess() |
| 34 | + throws InterruptedException { |
| 35 | + |
| 36 | + commandProperties.withExecutionTimeoutInMilliseconds(1000); |
| 37 | + |
| 38 | + commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000); |
| 39 | + commandProperties.withExecutionIsolationStrategy( |
| 40 | + HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); |
| 41 | + commandProperties.withCircuitBreakerEnabled(true); |
| 42 | + commandProperties.withCircuitBreakerRequestVolumeThreshold(1); |
| 43 | + |
| 44 | + config.andCommandPropertiesDefaults(commandProperties); |
| 45 | + |
| 46 | + config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() |
| 47 | + .withMaxQueueSize(1) |
| 48 | + .withCoreSize(1) |
| 49 | + .withQueueSizeRejectionThreshold(1)); |
| 50 | + |
| 51 | + assertThat(this.invokeRemoteService(10000), equalTo(null)); |
| 52 | + assertThat(this.invokeRemoteService(10000), equalTo(null)); |
| 53 | + Thread.sleep(5000); |
| 54 | + |
| 55 | + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), |
| 56 | + equalTo("Success")); |
| 57 | + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), |
| 58 | + equalTo("Success")); |
| 59 | + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), |
| 60 | + equalTo("Success")); |
| 61 | + } |
| 62 | + |
| 63 | + String invokeRemoteService(long timeout) throws InterruptedException { |
| 64 | + String response = null; |
| 65 | + try { |
| 66 | + response = new RemoteServiceTestCommand(config, |
| 67 | + new RemoteServiceTestSimulator(timeout)).execute(); |
| 68 | + } catch (HystrixRuntimeException ex) { |
| 69 | + System.out.println("ex = " + ex); |
| 70 | + } |
| 71 | + return response; |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments