Skip to content

Commit 584f362

Browse files
committed
iss993: added tests for default command properties
1 parent 2a1b71d commit 584f362

File tree

3 files changed

+89
-18
lines changed

3 files changed

+89
-18
lines changed

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/BasicHystrixTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
*/
1616
package com.netflix.hystrix.contrib.javanica.test.common;
1717

18+
import com.google.common.base.Throwables;
1819
import com.hystrix.junit.HystrixRequestContextRule;
20+
import com.netflix.hystrix.HystrixInvokableInfo;
21+
import com.netflix.hystrix.HystrixThreadPool;
22+
import com.netflix.hystrix.HystrixThreadPoolProperties;
1923
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
2024
import org.junit.Rule;
2125

26+
import java.lang.reflect.Field;
27+
2228
/**
2329
* Created by dmgcodevil
2430
*/
@@ -33,4 +39,21 @@ protected final HystrixRequestContext getHystrixContext() {
3339
protected void resetContext() {
3440
request.reset();
3541
}
42+
43+
protected final HystrixThreadPoolProperties getThreadPoolProperties(HystrixInvokableInfo<?> command) {
44+
try {
45+
Field field = command.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("threadPool");
46+
field.setAccessible(true);
47+
HystrixThreadPool threadPool = (HystrixThreadPool) field.get(command);
48+
49+
Field field2 = HystrixThreadPool.HystrixThreadPoolDefault.class.getDeclaredField("properties");
50+
field2.setAccessible(true);
51+
return (HystrixThreadPoolProperties) field2.get(threadPool);
52+
53+
} catch (NoSuchFieldException e) {
54+
throw Throwables.propagate(e);
55+
} catch (IllegalAccessException e) {
56+
throw Throwables.propagate(e);
57+
}
58+
}
3659
}

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/configuration/command/BasicCommandDefaultPropertiesTest.java

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.netflix.hystrix.HystrixInvokableInfo;
44
import com.netflix.hystrix.HystrixRequestLog;
5+
import com.netflix.hystrix.HystrixThreadPoolProperties;
56
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
67
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
8+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
79
import com.netflix.hystrix.contrib.javanica.test.common.BasicHystrixTest;
810
import org.junit.Before;
911
import org.junit.Test;
1012

13+
import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS;
1114
import static org.junit.Assert.assertEquals;
1215

1316
/**
@@ -26,7 +29,7 @@ public void setUp() throws Exception {
2629

2730
@Test
2831
public void testCommandInheritsDefaultGroupKey() {
29-
service.commandInheritsGroupKey();
32+
service.commandInheritsDefaultProperties();
3033
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
3134
.getAllExecutedCommands().iterator().next();
3235
assertEquals("DefaultGroupKey", command.getCommandGroup().name());
@@ -42,7 +45,7 @@ public void testCommandOverridesDefaultGroupKey() {
4245

4346
@Test
4447
public void testCommandInheritsDefaultThreadPoolKey() {
45-
service.commandInheritsThreadPoolKey();
48+
service.commandInheritsDefaultProperties();
4649
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
4750
.getAllExecutedCommands().iterator().next();
4851
assertEquals("DefaultThreadPoolKey", command.getThreadPoolKey().name());
@@ -56,11 +59,56 @@ public void testCommandOverridesDefaultThreadPoolKey() {
5659
assertEquals("SpecificThreadPoolKey", command.getThreadPoolKey().name());
5760
}
5861

59-
@DefaultProperties(groupKey = "DefaultGroupKey", threadPoolKey = "DefaultThreadPoolKey")
62+
@Test
63+
public void testCommandInheritsDefaultCommandProperties() {
64+
service.commandInheritsDefaultProperties();
65+
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
66+
.getAllExecutedCommands().iterator().next();
67+
assertEquals(456, command.getProperties().executionTimeoutInMilliseconds().get().intValue());
68+
}
69+
70+
@Test
71+
public void testCommandOverridesDefaultCommandProperties() {
72+
service.commandOverridesDefaultCommandProperties();
73+
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
74+
.getAllExecutedCommands().iterator().next();
75+
assertEquals(654, command.getProperties().executionTimeoutInMilliseconds().get().intValue());
76+
}
77+
78+
@Test
79+
public void testCommandInheritsThreadPollProperties() {
80+
service.commandInheritsDefaultProperties();
81+
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
82+
.getAllExecutedCommands().iterator().next();
83+
84+
HystrixThreadPoolProperties properties = getThreadPoolProperties(command);
85+
86+
assertEquals(123, properties.maxQueueSize().get().intValue());
87+
}
88+
89+
@Test
90+
public void testCommandOverridesDefaultThreadPollProperties() {
91+
service.commandOverridesDefaultThreadPoolProperties();
92+
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
93+
.getAllExecutedCommands().iterator().next();
94+
95+
HystrixThreadPoolProperties properties = getThreadPoolProperties(command);
96+
97+
assertEquals(321, properties.maxQueueSize().get().intValue());
98+
}
99+
100+
@DefaultProperties(groupKey = "DefaultGroupKey", threadPoolKey = "DefaultThreadPoolKey",
101+
commandProperties = {
102+
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "456")
103+
},
104+
threadPoolProperties = {
105+
@HystrixProperty(name = "maxQueueSize", value = "123")
106+
}
107+
)
60108
public static class Service {
61109

62110
@HystrixCommand
63-
public Object commandInheritsGroupKey() {
111+
public Object commandInheritsDefaultProperties() {
64112
return null;
65113
}
66114

@@ -69,13 +117,22 @@ public Object commandOverridesGroupKey() {
69117
return null;
70118
}
71119

72-
@HystrixCommand
73-
public Object commandInheritsThreadPoolKey() {
120+
@HystrixCommand(threadPoolKey = "SpecificThreadPoolKey")
121+
public Object commandOverridesThreadPoolKey() {
74122
return null;
75123
}
76124

77-
@HystrixCommand(threadPoolKey = "SpecificThreadPoolKey")
78-
public Object commandOverridesThreadPoolKey() {
125+
@HystrixCommand(commandProperties = {
126+
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "654")
127+
})
128+
public Object commandOverridesDefaultCommandProperties() {
129+
return null;
130+
}
131+
132+
@HystrixCommand(threadPoolProperties = {
133+
@HystrixProperty(name = "maxQueueSize", value = "321")
134+
})
135+
public Object commandOverridesDefaultThreadPoolProperties() {
79136
return null;
80137
}
81138
}

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/configuration/command/BasicCommandPropertiesTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.netflix.hystrix.HystrixEventType;
2020
import com.netflix.hystrix.HystrixInvokableInfo;
2121
import com.netflix.hystrix.HystrixRequestLog;
22-
import com.netflix.hystrix.HystrixThreadPool;
2322
import com.netflix.hystrix.HystrixThreadPoolProperties;
2423
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
2524
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
@@ -28,8 +27,6 @@
2827
import org.junit.Before;
2928
import org.junit.Test;
3029

31-
import java.lang.reflect.Field;
32-
3330
import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED;
3431
import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE;
3532
import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.CIRCUIT_BREAKER_FORCE_CLOSED;
@@ -84,13 +81,7 @@ public void testGetUser() throws NoSuchFieldException, IllegalAccessException {
8481
assertEquals(110, command.getProperties().executionTimeoutInMilliseconds().get().intValue());
8582
assertEquals(false, command.getProperties().executionIsolationThreadInterruptOnTimeout().get());
8683

87-
Field field = command.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("threadPool");
88-
field.setAccessible(true);
89-
HystrixThreadPool threadPool = (HystrixThreadPool) field.get(command);
90-
91-
Field field2 = HystrixThreadPool.HystrixThreadPoolDefault.class.getDeclaredField("properties");
92-
field2.setAccessible(true);
93-
HystrixThreadPoolProperties properties = (HystrixThreadPoolProperties) field2.get(threadPool);
84+
HystrixThreadPoolProperties properties = getThreadPoolProperties(command);
9485

9586
assertEquals(30, (int) properties.coreSize().get());
9687
assertEquals(101, (int) properties.maxQueueSize().get());

0 commit comments

Comments
 (0)