Skip to content

Commit 2137cb1

Browse files
committed
Execution type is now correctly determined from method return type
1 parent 8097506 commit 2137cb1

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/ExecutionType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ public enum ExecutionType {
3939
*/
4040
OBSERVABLE;
4141

42-
4342
/**
4443
* Gets execution type for specified class type.
4544
* @param type the type
4645
* @return the execution type {@link ExecutionType}
4746
*/
4847
public static ExecutionType getExecutionType(Class<?> type) {
49-
if (type.isAssignableFrom(Future.class)) {
48+
if (Future.class.isAssignableFrom(type)) {
5049
return ExecutionType.ASYNCHRONOUS;
51-
} else if (type.isAssignableFrom(Observable.class)) {
50+
} else if (Observable.class.isAssignableFrom(type)) {
5251
return ExecutionType.OBSERVABLE;
5352
} else {
5453
return ExecutionType.SYNCHRONOUS;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.netflix.hystrix.contrib.javanica.command;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
import rx.Observable;
7+
import rx.internal.operators.OperatorMulticast;
8+
9+
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.Future;
12+
import java.util.concurrent.RunnableFuture;
13+
14+
import static com.netflix.hystrix.contrib.javanica.command.ExecutionType.ASYNCHRONOUS;
15+
import static com.netflix.hystrix.contrib.javanica.command.ExecutionType.OBSERVABLE;
16+
import static com.netflix.hystrix.contrib.javanica.command.ExecutionType.SYNCHRONOUS;
17+
import static java.util.Arrays.asList;
18+
import static org.junit.Assert.assertEquals;
19+
20+
@RunWith(Parameterized.class)
21+
public class ExecutionTypeTest {
22+
23+
@Parameterized.Parameters
24+
public static List<Object[]> data() {
25+
return asList(new Object[][]{
26+
{returnType(Integer.class), shouldHaveExecutionType(SYNCHRONOUS)},
27+
{returnType(List.class), shouldHaveExecutionType(SYNCHRONOUS)},
28+
{returnType(Object.class), shouldHaveExecutionType(SYNCHRONOUS)},
29+
{returnType(Class.class), shouldHaveExecutionType(SYNCHRONOUS)},
30+
{returnType(Future.class), shouldHaveExecutionType(ASYNCHRONOUS)},
31+
{returnType(AsyncResult.class), shouldHaveExecutionType(ASYNCHRONOUS)},
32+
{returnType(RunnableFuture.class), shouldHaveExecutionType(ASYNCHRONOUS)},
33+
{returnType(CompletableFuture.class), shouldHaveExecutionType(ASYNCHRONOUS)},
34+
{returnType(Observable.class), shouldHaveExecutionType(OBSERVABLE)},
35+
{returnType(OperatorMulticast.class), shouldHaveExecutionType(OBSERVABLE)},
36+
});
37+
}
38+
39+
@Test
40+
public void should_return_correct_execution_type() throws Exception {
41+
assertEquals("Unexpected execution type for method return type: " + methodReturnType, expectedType, ExecutionType.getExecutionType(methodReturnType));
42+
43+
}
44+
45+
private static ExecutionType shouldHaveExecutionType(final ExecutionType type) {
46+
return type;
47+
}
48+
49+
private static Class<?> returnType(final Class<?> aClass) {
50+
return aClass;
51+
}
52+
53+
private final Class<?> methodReturnType;
54+
private final ExecutionType expectedType;
55+
56+
public ExecutionTypeTest(final Class<?> methodReturnType, final ExecutionType expectedType) {
57+
this.methodReturnType = methodReturnType;
58+
this.expectedType = expectedType;
59+
}
60+
}

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ public void testGetUserSync() {
7777
assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
7878
}
7979

80+
@Test
81+
public void should_work_with_parameterized_method() throws Exception {
82+
assertEquals(Integer.valueOf(1), userService.echo(1));
83+
84+
assertEquals(1, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size());
85+
assertTrue(getCommand().getExecutionEvents().contains(HystrixEventType.SUCCESS));
86+
}
87+
8088
private com.netflix.hystrix.HystrixCommand<?> getCommand() {
8189
return HystrixRequestLog.getCurrentRequest().getExecutedCommands().iterator().next();
8290
}
@@ -98,6 +106,11 @@ public User getUserSync(String id, String name) {
98106
return new User(id, name + id); // it should be network call
99107
}
100108

109+
@HystrixCommand
110+
public <T> T echo(T value) {
111+
return value;
112+
}
113+
101114
}
102115

103116
@Configurable

0 commit comments

Comments
 (0)