99import org .junit .Test ;
1010import org .junit .rules .ExpectedException ;
1111
12+ import java .util .concurrent .ExecutionException ;
13+
1214import static org .hamcrest .MatcherAssert .assertThat ;
1315import static org .hamcrest .Matchers .equalTo ;
1416
1517public class HystrixTimeoutTest {
1618
17- private static HystrixCommand .Setter config ;
18- private static HystrixCommandProperties .Setter commandProperties = HystrixCommandProperties . Setter () ;
19+ private HystrixCommand .Setter config ;
20+ private HystrixCommandProperties .Setter commandProperties ;
1921
2022
2123 @ Rule
2224 public final ExpectedException exception = ExpectedException .none ();
2325
2426 @ Before
2527 public void setup () {
28+ commandProperties = HystrixCommandProperties .Setter ();
2629 config = HystrixCommand
2730 .Setter
2831 .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroup1" ));
@@ -34,29 +37,87 @@ public void givenInputBob_andDefaultSettings_thenReturnHelloBob(){
3437 }
3538
3639 @ Test
37- public void givenTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess () throws InterruptedException {
40+ public void givenServiceTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess () throws InterruptedException {
3841 assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (100 )).execute (), equalTo ("Success" ));
3942 }
4043
4144 @ Test
42- public void givenTimeoutEqualTo10000_andDefaultSettings_thenExpectHystrixRuntimeException () throws InterruptedException {
45+ public void givenServiceTimeoutEqualTo10000_andDefaultSettings_thenExpectHRE () throws InterruptedException {
4346 exception .expect (HystrixRuntimeException .class );
4447 new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (10_000 )).execute ();
4548 }
4649
4750 @ Test
48- public void givenTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess () throws InterruptedException {
51+ public void givenServiceTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess ()
52+ throws InterruptedException {
4953 commandProperties .withExecutionTimeoutInMilliseconds (10_000 );
5054 config .andCommandPropertiesDefaults (commandProperties );
51- assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (5_000 )).execute (), equalTo ("Success" ));
55+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (5_000 )).execute (),
56+ equalTo ("Success" ));
5257 }
5358
5459 @ Test
55- public void givenTimeoutEqualTo15000_andExecutionTimeoutEqualTo10000_thenExpectHystrixRuntimeException () throws InterruptedException {
60+ public void givenServiceTimeoutEqualTo15000_andExecutionTimeoutEqualTo5000_thenExpectHRE ()
61+ throws InterruptedException {
5662 exception .expect (HystrixRuntimeException .class );
57- commandProperties .withExecutionTimeoutInMilliseconds (10_000 );
63+ commandProperties .withExecutionTimeoutInMilliseconds (5_000 );
5864 config .andCommandPropertiesDefaults (commandProperties );
5965 new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (15_000 )).execute ();
6066 }
6167
68+ @ Test
69+ public void givenServiceTimeoutEqual_andExecutionTimeout_andThreadPool_thenReturnSuccess ()
70+ throws InterruptedException {
71+ commandProperties .withExecutionTimeoutInMilliseconds (10_000 );
72+ config .andCommandPropertiesDefaults (commandProperties );
73+ config .andThreadPoolPropertiesDefaults (HystrixThreadPoolProperties .Setter ()
74+ .withMaxQueueSize (10 )
75+ .withCoreSize (3 )
76+ .withQueueSizeRejectionThreshold (10 ));
77+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (9_000 )).execute (),
78+ equalTo ("Success" ));
79+ }
80+
81+ @ Test
82+ public void givenCircuitBreakerSetup_thenReturnSuccess () throws InterruptedException {
83+
84+ commandProperties .withExecutionTimeoutInMilliseconds (1000 );
85+
86+ commandProperties .withCircuitBreakerSleepWindowInMilliseconds (4000 );
87+ commandProperties .withExecutionIsolationStrategy (
88+ HystrixCommandProperties .ExecutionIsolationStrategy .THREAD );
89+ commandProperties .withCircuitBreakerEnabled (true );
90+ commandProperties .withCircuitBreakerRequestVolumeThreshold (1 );
91+
92+ config .andCommandPropertiesDefaults (commandProperties );
93+
94+ config .andThreadPoolPropertiesDefaults (HystrixThreadPoolProperties .Setter ()
95+ .withMaxQueueSize (1 )
96+ .withCoreSize (1 )
97+ .withQueueSizeRejectionThreshold (1 ));
98+
99+ assertThat (this .invokeRemoteService (10000 ), equalTo (null ));
100+ assertThat (this .invokeRemoteService (10000 ), equalTo (null ));
101+ assertThat (this .invokeRemoteService (500 ), equalTo (null ));
102+ Thread .sleep (5000 );
103+
104+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
105+ equalTo ("Success" ));
106+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
107+ equalTo ("Success" ));
108+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
109+ equalTo ("Success" ));
110+ }
111+
112+ public String invokeRemoteService (long timeout ) throws InterruptedException {
113+ String response = null ;
114+ try {
115+ response = new RemoteServiceTestCommand (config ,
116+ new RemoteServiceTestSimulator (timeout )).execute ();
117+ }catch (HystrixRuntimeException ex ){
118+ System .out .println ("ex = " + ex );
119+ }
120+ return response ;
121+ }
122+
62123}
0 commit comments