|
| 1 | +package com.baeldung.hystrix; |
| 2 | + |
| 3 | +import com.netflix.hystrix.*; |
| 4 | +import com.netflix.hystrix.exception.HystrixRuntimeException; |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Rule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.rules.ExpectedException; |
| 10 | + |
| 11 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | +import static org.hamcrest.Matchers.equalTo; |
| 13 | + |
| 14 | +public class HystrixTimeoutTest { |
| 15 | + |
| 16 | + private static HystrixCommand.Setter config; |
| 17 | + private static HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); |
| 18 | + |
| 19 | + |
| 20 | + @Rule |
| 21 | + public final ExpectedException exception = ExpectedException.none(); |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setup() { |
| 25 | + config = HystrixCommand |
| 26 | + .Setter |
| 27 | + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess() throws InterruptedException { |
| 32 | + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), equalTo("Success")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenTimeoutEqualTo10000_andDefaultSettings_thenExpectHystrixRuntimeException() throws InterruptedException { |
| 37 | + exception.expect(HystrixRuntimeException.class); |
| 38 | + new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute(); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void givenTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess() throws InterruptedException { |
| 44 | + commandProperties.withExecutionTimeoutInMilliseconds(10_000); |
| 45 | + config.andCommandPropertiesDefaults(commandProperties); |
| 46 | + assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(5_000)).execute(), equalTo("Success")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenTimeoutEqualTo15000_andExecutionTimeoutEqualTo10000_thenExpectHystrixRuntimeException() throws InterruptedException { |
| 51 | + exception.expect(HystrixRuntimeException.class); |
| 52 | + commandProperties.withExecutionTimeoutInMilliseconds(10_000); |
| 53 | + config.andCommandPropertiesDefaults(commandProperties); |
| 54 | + new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(15_000)).execute(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +} |
0 commit comments