Skip to content

Commit

Permalink
Execution type is now correctly determined from method return type
Browse files Browse the repository at this point in the history
  • Loading branch information
sawano committed Nov 27, 2014
1 parent 8097506 commit 2137cb1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ public enum ExecutionType {
*/
OBSERVABLE;


/**
* Gets execution type for specified class type.
* @param type the type
* @return the execution type {@link ExecutionType}
*/
public static ExecutionType getExecutionType(Class<?> type) {
if (type.isAssignableFrom(Future.class)) {
if (Future.class.isAssignableFrom(type)) {
return ExecutionType.ASYNCHRONOUS;
} else if (type.isAssignableFrom(Observable.class)) {
} else if (Observable.class.isAssignableFrom(type)) {
return ExecutionType.OBSERVABLE;
} else {
return ExecutionType.SYNCHRONOUS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.netflix.hystrix.contrib.javanica.command;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import rx.Observable;
import rx.internal.operators.OperatorMulticast;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;

import static com.netflix.hystrix.contrib.javanica.command.ExecutionType.ASYNCHRONOUS;
import static com.netflix.hystrix.contrib.javanica.command.ExecutionType.OBSERVABLE;
import static com.netflix.hystrix.contrib.javanica.command.ExecutionType.SYNCHRONOUS;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class ExecutionTypeTest {

@Parameterized.Parameters
public static List<Object[]> data() {
return asList(new Object[][]{
{returnType(Integer.class), shouldHaveExecutionType(SYNCHRONOUS)},
{returnType(List.class), shouldHaveExecutionType(SYNCHRONOUS)},
{returnType(Object.class), shouldHaveExecutionType(SYNCHRONOUS)},
{returnType(Class.class), shouldHaveExecutionType(SYNCHRONOUS)},
{returnType(Future.class), shouldHaveExecutionType(ASYNCHRONOUS)},
{returnType(AsyncResult.class), shouldHaveExecutionType(ASYNCHRONOUS)},
{returnType(RunnableFuture.class), shouldHaveExecutionType(ASYNCHRONOUS)},
{returnType(CompletableFuture.class), shouldHaveExecutionType(ASYNCHRONOUS)},
{returnType(Observable.class), shouldHaveExecutionType(OBSERVABLE)},
{returnType(OperatorMulticast.class), shouldHaveExecutionType(OBSERVABLE)},
});
}

@Test
public void should_return_correct_execution_type() throws Exception {
assertEquals("Unexpected execution type for method return type: " + methodReturnType, expectedType, ExecutionType.getExecutionType(methodReturnType));

}

private static ExecutionType shouldHaveExecutionType(final ExecutionType type) {
return type;
}

private static Class<?> returnType(final Class<?> aClass) {
return aClass;
}

private final Class<?> methodReturnType;
private final ExecutionType expectedType;

public ExecutionTypeTest(final Class<?> methodReturnType, final ExecutionType expectedType) {
this.methodReturnType = methodReturnType;
this.expectedType = expectedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public void testGetUserSync() {
assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
}

@Test
public void should_work_with_parameterized_method() throws Exception {
assertEquals(Integer.valueOf(1), userService.echo(1));

assertEquals(1, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size());
assertTrue(getCommand().getExecutionEvents().contains(HystrixEventType.SUCCESS));
}

private com.netflix.hystrix.HystrixCommand<?> getCommand() {
return HystrixRequestLog.getCurrentRequest().getExecutedCommands().iterator().next();
}
Expand All @@ -98,6 +106,11 @@ public User getUserSync(String id, String name) {
return new User(id, name + id); // it should be network call
}

@HystrixCommand
public <T> T echo(T value) {
return value;
}

}

@Configurable
Expand Down

0 comments on commit 2137cb1

Please sign in to comment.