Skip to content

Commit

Permalink
iss993: added tests for default command properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgcodevil committed Jun 30, 2016
1 parent 2a1b71d commit 584f362
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
*/
package com.netflix.hystrix.contrib.javanica.test.common;

import com.google.common.base.Throwables;
import com.hystrix.junit.HystrixRequestContextRule;
import com.netflix.hystrix.HystrixInvokableInfo;
import com.netflix.hystrix.HystrixThreadPool;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.junit.Rule;

import java.lang.reflect.Field;

/**
* Created by dmgcodevil
*/
Expand All @@ -33,4 +39,21 @@ protected final HystrixRequestContext getHystrixContext() {
protected void resetContext() {
request.reset();
}

protected final HystrixThreadPoolProperties getThreadPoolProperties(HystrixInvokableInfo<?> command) {
try {
Field field = command.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("threadPool");
field.setAccessible(true);
HystrixThreadPool threadPool = (HystrixThreadPool) field.get(command);

Field field2 = HystrixThreadPool.HystrixThreadPoolDefault.class.getDeclaredField("properties");
field2.setAccessible(true);
return (HystrixThreadPoolProperties) field2.get(threadPool);

} catch (NoSuchFieldException e) {
throw Throwables.propagate(e);
} catch (IllegalAccessException e) {
throw Throwables.propagate(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.netflix.hystrix.HystrixInvokableInfo;
import com.netflix.hystrix.HystrixRequestLog;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.test.common.BasicHystrixTest;
import org.junit.Before;
import org.junit.Test;

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

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

@Test
public void testCommandInheritsDefaultGroupKey() {
service.commandInheritsGroupKey();
service.commandInheritsDefaultProperties();
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands().iterator().next();
assertEquals("DefaultGroupKey", command.getCommandGroup().name());
Expand All @@ -42,7 +45,7 @@ public void testCommandOverridesDefaultGroupKey() {

@Test
public void testCommandInheritsDefaultThreadPoolKey() {
service.commandInheritsThreadPoolKey();
service.commandInheritsDefaultProperties();
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands().iterator().next();
assertEquals("DefaultThreadPoolKey", command.getThreadPoolKey().name());
Expand All @@ -56,11 +59,56 @@ public void testCommandOverridesDefaultThreadPoolKey() {
assertEquals("SpecificThreadPoolKey", command.getThreadPoolKey().name());
}

@DefaultProperties(groupKey = "DefaultGroupKey", threadPoolKey = "DefaultThreadPoolKey")
@Test
public void testCommandInheritsDefaultCommandProperties() {
service.commandInheritsDefaultProperties();
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands().iterator().next();
assertEquals(456, command.getProperties().executionTimeoutInMilliseconds().get().intValue());
}

@Test
public void testCommandOverridesDefaultCommandProperties() {
service.commandOverridesDefaultCommandProperties();
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands().iterator().next();
assertEquals(654, command.getProperties().executionTimeoutInMilliseconds().get().intValue());
}

@Test
public void testCommandInheritsThreadPollProperties() {
service.commandInheritsDefaultProperties();
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands().iterator().next();

HystrixThreadPoolProperties properties = getThreadPoolProperties(command);

assertEquals(123, properties.maxQueueSize().get().intValue());
}

@Test
public void testCommandOverridesDefaultThreadPollProperties() {
service.commandOverridesDefaultThreadPoolProperties();
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands().iterator().next();

HystrixThreadPoolProperties properties = getThreadPoolProperties(command);

assertEquals(321, properties.maxQueueSize().get().intValue());
}

@DefaultProperties(groupKey = "DefaultGroupKey", threadPoolKey = "DefaultThreadPoolKey",
commandProperties = {
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "456")
},
threadPoolProperties = {
@HystrixProperty(name = "maxQueueSize", value = "123")
}
)
public static class Service {

@HystrixCommand
public Object commandInheritsGroupKey() {
public Object commandInheritsDefaultProperties() {
return null;
}

Expand All @@ -69,13 +117,22 @@ public Object commandOverridesGroupKey() {
return null;
}

@HystrixCommand
public Object commandInheritsThreadPoolKey() {
@HystrixCommand(threadPoolKey = "SpecificThreadPoolKey")
public Object commandOverridesThreadPoolKey() {
return null;
}

@HystrixCommand(threadPoolKey = "SpecificThreadPoolKey")
public Object commandOverridesThreadPoolKey() {
@HystrixCommand(commandProperties = {
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "654")
})
public Object commandOverridesDefaultCommandProperties() {
return null;
}

@HystrixCommand(threadPoolProperties = {
@HystrixProperty(name = "maxQueueSize", value = "321")
})
public Object commandOverridesDefaultThreadPoolProperties() {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.netflix.hystrix.HystrixEventType;
import com.netflix.hystrix.HystrixInvokableInfo;
import com.netflix.hystrix.HystrixRequestLog;
import com.netflix.hystrix.HystrixThreadPool;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
Expand All @@ -28,8 +27,6 @@
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;

import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED;
import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE;
import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.CIRCUIT_BREAKER_FORCE_CLOSED;
Expand Down Expand Up @@ -84,13 +81,7 @@ public void testGetUser() throws NoSuchFieldException, IllegalAccessException {
assertEquals(110, command.getProperties().executionTimeoutInMilliseconds().get().intValue());
assertEquals(false, command.getProperties().executionIsolationThreadInterruptOnTimeout().get());

Field field = command.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("threadPool");
field.setAccessible(true);
HystrixThreadPool threadPool = (HystrixThreadPool) field.get(command);

Field field2 = HystrixThreadPool.HystrixThreadPoolDefault.class.getDeclaredField("properties");
field2.setAccessible(true);
HystrixThreadPoolProperties properties = (HystrixThreadPoolProperties) field2.get(threadPool);
HystrixThreadPoolProperties properties = getThreadPoolProperties(command);

assertEquals(30, (int) properties.coreSize().get());
assertEquals(101, (int) properties.maxQueueSize().get());
Expand Down

0 comments on commit 584f362

Please sign in to comment.