From 8097506f6d44110ad5f706b69d6f04a539837ca1 Mon Sep 17 00:00:00 2001 From: Daniel Sawano Date: Thu, 27 Nov 2014 20:31:01 +0100 Subject: [PATCH 1/2] Reducing repetetive test code --- .../test/spring/command/CommandTest.java | 71 ++++++++++--------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java index aeacceb6f..c1350f94d 100644 --- a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java +++ b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java @@ -8,6 +8,8 @@ import com.netflix.hystrix.contrib.javanica.test.spring.conf.AopCglibConfig; import com.netflix.hystrix.contrib.javanica.test.spring.domain.User; import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -34,46 +36,49 @@ public class CommandTest { @Autowired private UserService userService; + private HystrixRequestContext context; + + @Before + public void setUp() throws Exception { + context = HystrixRequestContext.initializeContext(); + } + + @After + public void tearDown() throws Exception { + context.shutdown(); + } @Test public void testGetUserAsync() throws ExecutionException, InterruptedException { - HystrixRequestContext context = HystrixRequestContext.initializeContext(); - try { - Future f1 = userService.getUserAsync("1", "name: "); - - assertEquals("name: 1", f1.get().getName()); - assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); - com.netflix.hystrix.HystrixExecutableInfo command = HystrixRequestLog.getCurrentRequest() - .getAllExecutedCommands().iterator().next(); - // assert the command key name is the we're expecting - assertEquals("GetUserCommand", command.getCommandKey().name()); - // assert the command group key name is the we're expecting - assertEquals("UserService", command.getCommandGroup().name()); - // assert the command thread pool key name is the we're expecting - assertEquals("CommandTestAsync", command.getThreadPoolKey().name()); - // it was successful - assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); - } finally { - context.shutdown(); - } + Future f1 = userService.getUserAsync("1", "name: "); + + assertEquals("name: 1", f1.get().getName()); + assertEquals(1, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size()); + com.netflix.hystrix.HystrixCommand command = getCommand(); + // assert the command key name is the we're expecting + assertEquals("GetUserCommand", command.getCommandKey().name()); + // assert the command group key name is the we're expecting + assertEquals("UserService", command.getCommandGroup().name()); + // assert the command thread pool key name is the we're expecting + assertEquals("CommandTestAsync", command.getThreadPoolKey().name()); + // it was successful + assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); } @Test public void testGetUserSync() { - HystrixRequestContext context = HystrixRequestContext.initializeContext(); - try { - User u1 = userService.getUserSync("1", "name: "); - assertEquals("name: 1", u1.getName()); - assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size()); - com.netflix.hystrix.HystrixExecutableInfo command = HystrixRequestLog.getCurrentRequest() - .getAllExecutedCommands().iterator().next(); - assertEquals("getUserSync", command.getCommandKey().name()); - assertEquals("UserGroup", command.getCommandGroup().name()); - assertEquals("UserGroup", command.getThreadPoolKey().name()); - assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); - } finally { - context.shutdown(); - } + User u1 = userService.getUserSync("1", "name: "); + assertEquals("name: 1", u1.getName()); + assertEquals(1, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size()); + com.netflix.hystrix.HystrixCommand command = getCommand(); + assertEquals("getUserSync", command.getCommandKey().name()); + assertEquals("UserGroup", command.getCommandGroup().name()); + assertEquals("UserGroup", command.getThreadPoolKey().name()); + assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS)); + } + + private com.netflix.hystrix.HystrixCommand getCommand() { + return HystrixRequestLog.getCurrentRequest().getExecutedCommands().iterator().next(); } public static class UserService { From 2137cb15bf6a7ff035f6434c218162c751eef740 Mon Sep 17 00:00:00 2001 From: Daniel Sawano Date: Thu, 27 Nov 2014 20:32:45 +0100 Subject: [PATCH 2/2] Execution type is now correctly determined from method return type --- .../javanica/command/ExecutionType.java | 5 +- .../javanica/command/ExecutionTypeTest.java | 60 +++++++++++++++++++ .../test/spring/command/CommandTest.java | 13 ++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/command/ExecutionTypeTest.java diff --git a/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/ExecutionType.java b/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/ExecutionType.java index 57638c2c8..ea3e285d1 100644 --- a/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/ExecutionType.java +++ b/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/ExecutionType.java @@ -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; diff --git a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/command/ExecutionTypeTest.java b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/command/ExecutionTypeTest.java new file mode 100644 index 000000000..fb9e5e0bf --- /dev/null +++ b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/command/ExecutionTypeTest.java @@ -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 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; + } +} diff --git a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java index c1350f94d..2e320f46b 100644 --- a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java +++ b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/command/CommandTest.java @@ -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(); } @@ -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 echo(T value) { + return value; + } + } @Configurable