Skip to content

Commit fa35813

Browse files
committed
Minor changes after review
1 parent b8e258c commit fa35813

File tree

2 files changed

+77
-46
lines changed

2 files changed

+77
-46
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import com.netflix.hystrix.HystrixCommandProperties;
66
import com.netflix.hystrix.HystrixThreadPoolProperties;
77
import com.netflix.hystrix.exception.HystrixRuntimeException;
8-
import org.junit.Before;
9-
import org.junit.Rule;
10-
import org.junit.Test;
8+
import org.junit.*;
119
import org.junit.rules.ExpectedException;
10+
import org.junit.runners.MethodSorters;
1211

1312
import static org.hamcrest.MatcherAssert.assertThat;
1413
import static org.hamcrest.Matchers.equalTo;
1514

15+
@FixMethodOrder(MethodSorters.JVM)
1616
public class HystrixTimeoutTest {
1717

1818
private HystrixCommand.Setter config;
@@ -85,47 +85,4 @@ public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted
8585
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
8686
equalTo("Success"));
8787
}
88-
89-
@Test
90-
public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess()
91-
throws InterruptedException {
92-
93-
commandProperties.withExecutionTimeoutInMilliseconds(1000);
94-
95-
commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000);
96-
commandProperties.withExecutionIsolationStrategy(
97-
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
98-
commandProperties.withCircuitBreakerEnabled(true);
99-
commandProperties.withCircuitBreakerRequestVolumeThreshold(1);
100-
101-
config.andCommandPropertiesDefaults(commandProperties);
102-
103-
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
104-
.withMaxQueueSize(1)
105-
.withCoreSize(1)
106-
.withQueueSizeRejectionThreshold(1));
107-
108-
assertThat(this.invokeRemoteService(10000), equalTo(null));
109-
assertThat(this.invokeRemoteService(10000), equalTo(null));
110-
Thread.sleep(5000);
111-
112-
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
113-
equalTo("Success"));
114-
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
115-
equalTo("Success"));
116-
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
117-
equalTo("Success"));
118-
}
119-
120-
public String invokeRemoteService(long timeout) throws InterruptedException {
121-
String response = null;
122-
try {
123-
response = new RemoteServiceTestCommand(config,
124-
new RemoteServiceTestSimulator(timeout)).execute();
125-
} catch (HystrixRuntimeException ex) {
126-
System.out.println("ex = " + ex);
127-
}
128-
return response;
129-
}
130-
13188
}

0 commit comments

Comments
 (0)