1212import static org .hamcrest .MatcherAssert .assertThat ;
1313import static org .hamcrest .Matchers .equalTo ;
1414
15- @ FixMethodOrder (MethodSorters .JVM )
1615public class HystrixTimeoutTest {
1716
18- private HystrixCommand .Setter config ;
19- private HystrixCommandProperties .Setter commandProperties ;
20-
21-
22- @ Rule
23- public final ExpectedException exception = ExpectedException .none ();
24-
25- @ Before
26- public void setup () {
27- commandProperties = HystrixCommandProperties .Setter ();
28- config = HystrixCommand
29- .Setter
30- .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroup1" ));
31- }
32-
3317 @ Test
3418 public void givenInputBobAndDefaultSettings_whenExecuted_thenReturnHelloBob (){
3519 assertThat (new CommandHelloWorld ("Bob" ).execute (), equalTo ("Hello Bob!" ));
@@ -38,35 +22,44 @@ public void givenInputBobAndDefaultSettings_whenExecuted_thenReturnHelloBob(){
3822 @ Test
3923 public void givenSvcTimeoutOf100AndDefaultSettings_whenExecuted_thenReturnSuccess ()
4024 throws InterruptedException {
41-
4225 HystrixCommand .Setter config = HystrixCommand
4326 .Setter
44- .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroup1 " ));
27+ .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroup2 " ));
4528
4629 assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (100 )).execute (),
4730 equalTo ("Success" ));
4831 }
4932
50- @ Test
33+ @ Test ( expected = HystrixRuntimeException . class )
5134 public void givenSvcTimeoutOf10000AndDefaultSettings__whenExecuted_thenExpectHRE () throws InterruptedException {
52- exception .expect (HystrixRuntimeException .class );
35+ HystrixCommand .Setter config = HystrixCommand
36+ .Setter
37+ .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroupTest3" ));
5338 new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (10_000 )).execute ();
5439 }
5540
5641 @ Test
5742 public void givenSvcTimeoutOf5000AndExecTimeoutOf10000__whenExecuted_thenReturnSuccess ()
5843 throws InterruptedException {
44+
45+ HystrixCommand .Setter config = HystrixCommand
46+ .Setter
47+ .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroupTest4" ));
48+ HystrixCommandProperties .Setter commandProperties = HystrixCommandProperties .Setter ();
5949 commandProperties .withExecutionTimeoutInMilliseconds (10_000 );
6050 config .andCommandPropertiesDefaults (commandProperties );
6151
6252 assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
6353 equalTo ("Success" ));
6454 }
6555
66- @ Test
56+ @ Test ( expected = HystrixRuntimeException . class )
6757 public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE ()
6858 throws InterruptedException {
69- exception .expect (HystrixRuntimeException .class );
59+ HystrixCommand .Setter config = HystrixCommand
60+ .Setter
61+ .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroupTest5" ));
62+ HystrixCommandProperties .Setter commandProperties = HystrixCommandProperties .Setter ();
7063 commandProperties .withExecutionTimeoutInMilliseconds (5_000 );
7164 config .andCommandPropertiesDefaults (commandProperties );
7265 new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (15_000 )).execute ();
@@ -75,6 +68,11 @@ public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectH
7568 @ Test
7669 public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess ()
7770 throws InterruptedException {
71+
72+ HystrixCommand .Setter config = HystrixCommand
73+ .Setter
74+ .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroupThreadPool" ));
75+ HystrixCommandProperties .Setter commandProperties = HystrixCommandProperties .Setter ();
7876 commandProperties .withExecutionTimeoutInMilliseconds (10_000 );
7977 config .andCommandPropertiesDefaults (commandProperties );
8078 config .andThreadPoolPropertiesDefaults (HystrixThreadPoolProperties .Setter ()
@@ -85,4 +83,52 @@ public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted
8583 assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
8684 equalTo ("Success" ));
8785 }
86+
87+ @ Test
88+ public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess ()
89+ throws InterruptedException {
90+
91+ HystrixCommand .Setter config = HystrixCommand
92+ .Setter
93+ .withGroupKey (HystrixCommandGroupKey .Factory .asKey ("RemoteServiceGroupCircuitBreaker" ));
94+ HystrixCommandProperties .Setter commandProperties = HystrixCommandProperties .Setter ();
95+ commandProperties .withExecutionTimeoutInMilliseconds (1000 );
96+
97+ commandProperties .withCircuitBreakerSleepWindowInMilliseconds (4000 );
98+ commandProperties .withExecutionIsolationStrategy (
99+ HystrixCommandProperties .ExecutionIsolationStrategy .THREAD );
100+ commandProperties .withCircuitBreakerEnabled (true );
101+ commandProperties .withCircuitBreakerRequestVolumeThreshold (1 );
102+
103+ config .andCommandPropertiesDefaults (commandProperties );
104+
105+ config .andThreadPoolPropertiesDefaults (HystrixThreadPoolProperties .Setter ()
106+ .withMaxQueueSize (1 )
107+ .withCoreSize (1 )
108+ .withQueueSizeRejectionThreshold (1 ));
109+
110+ assertThat (this .invokeRemoteService (config , 10_000 ), equalTo (null ));
111+ assertThat (this .invokeRemoteService (config , 10_000 ), equalTo (null ));
112+ assertThat (this .invokeRemoteService (config , 10_000 ), equalTo (null ));
113+ Thread .sleep (5000 );
114+
115+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
116+ equalTo ("Success" ));
117+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
118+ equalTo ("Success" ));
119+ assertThat (new RemoteServiceTestCommand (config , new RemoteServiceTestSimulator (500 )).execute (),
120+ equalTo ("Success" ));
121+ }
122+
123+ public String invokeRemoteService (HystrixCommand .Setter config , int timeout )
124+ throws InterruptedException {
125+ String response = null ;
126+ try {
127+ response = new RemoteServiceTestCommand (config ,
128+ new RemoteServiceTestSimulator (timeout )).execute ();
129+ } catch (HystrixRuntimeException ex ) {
130+ System .out .println ("ex = " + ex );
131+ }
132+ return response ;
133+ }
88134}
0 commit comments