Skip to content

Commit

Permalink
Merge pull request Netflix#1007 from mattrjacobs/unit-tests-assert-ev…
Browse files Browse the repository at this point in the history
…ents-instead-of-metrics

Modify unit tests to assert on event counts and ordering instead of metrics gathered
  • Loading branch information
mattrjacobs committed Dec 3, 2015
2 parents c742c2b + 110b6fe commit ba30ec6
Show file tree
Hide file tree
Showing 8 changed files with 830 additions and 3,335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import rx.functions.Action1;
import rx.functions.Func0;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
Expand Down Expand Up @@ -171,6 +173,40 @@ protected void assertSaneHystrixRequestLog(final int numCommands) {
}
}

protected void assertCommandExecutionEvents(HystrixInvokableInfo<?> command, HystrixEventType... expectedEventTypes) {
boolean emitExpected = false;
int expectedEmitCount = 0;

boolean fallbackEmitExpected = false;
int expectedFallbackEmitCount = 0;

List<HystrixEventType> condensedEmitExpectedEventTypes = new ArrayList<HystrixEventType>();

for (HystrixEventType expectedEventType: expectedEventTypes) {
if (expectedEventType.equals(HystrixEventType.EMIT)) {
if (!emitExpected) {
//first EMIT encountered, add it to condensedEmitExpectedEventTypes
condensedEmitExpectedEventTypes.add(HystrixEventType.EMIT);
}
emitExpected = true;
expectedEmitCount++;
} else if (expectedEventType.equals(HystrixEventType.FALLBACK_EMIT)) {
if (!fallbackEmitExpected) {
//first FALLBACK_EMIT encountered, add it to condensedEmitExpectedEventTypes
condensedEmitExpectedEventTypes.add(HystrixEventType.FALLBACK_EMIT);
}
fallbackEmitExpected = true;
expectedFallbackEmitCount++;
} else {
condensedEmitExpectedEventTypes.add(expectedEventType);
}
}
List<HystrixEventType> actualEventTypes = command.getExecutionEvents();
assertEquals(expectedEmitCount, command.getNumberEmissions());
assertEquals(expectedFallbackEmitCount, command.getNumberFallbackEmissions());
assertEquals(condensedEmitExpectedEventTypes, actualEventTypes);
}

/**
* Threadpool with 1 thread, queue of size 1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public TestCircuitBreaker() {
forceShortCircuit = false;
}

public TestCircuitBreaker(HystrixCommandKey commandKey) {
this.metrics = getMetrics(commandKey, HystrixCommandPropertiesTest.getUnitTestPropertiesSetter());
forceShortCircuit = false;
}


public TestCircuitBreaker setForceShortCircuit(boolean value) {
this.forceShortCircuit = value;
return this;
Expand All @@ -57,7 +63,7 @@ public boolean isOpen() {
if (forceShortCircuit) {
return true;
} else {
return metrics.getCumulativeCount(HystrixRollingNumberEvent.FAILURE) >= 3;
return metrics.getHealthCounts().getErrorCount() >= 3;
}
}

Expand Down Expand Up @@ -497,6 +503,14 @@ private static HystrixCommandMetrics getMetrics(HystrixCommandProperties.Setter
return new HystrixCommandMetrics(CommandKeyForUnitTest.KEY_ONE, CommandOwnerForUnitTest.OWNER_ONE, ThreadPoolKeyForUnitTest.THREAD_POOL_ONE, HystrixCommandPropertiesTest.asMock(properties), HystrixEventNotifierDefault.getInstance());
}


/**
* Utility method for creating {@link HystrixCommandMetrics} for unit tests.
*/
private static HystrixCommandMetrics getMetrics(HystrixCommandKey commandKey, HystrixCommandProperties.Setter properties) {
return new HystrixCommandMetrics(commandKey, CommandOwnerForUnitTest.OWNER_ONE, ThreadPoolKeyForUnitTest.THREAD_POOL_ONE, HystrixCommandPropertiesTest.asMock(properties), HystrixEventNotifierDefault.getInstance());
}

/**
* Utility method for creating {@link HystrixCircuitBreaker} for unit tests.
*/
Expand Down
1,975 changes: 323 additions & 1,652 deletions hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java

Large diffs are not rendered by default.

2,115 changes: 439 additions & 1,676 deletions hystrix-core/src/test/java/com/netflix/hystrix/HystrixObservableCommandTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ public enum ThreadPoolKeyForUnitTest implements HystrixThreadPoolKey {
}

public static class TestCommandBuilder {
HystrixCircuitBreakerTest.TestCircuitBreaker _cb = new HystrixCircuitBreakerTest.TestCircuitBreaker();
HystrixCommandGroupKey owner = CommandGroupForUnitTest.OWNER_ONE;
HystrixCommandKey dependencyKey = null;
HystrixThreadPoolKey threadPoolKey = null;
HystrixCircuitBreaker circuitBreaker = _cb;
HystrixCircuitBreaker circuitBreaker;
HystrixThreadPool threadPool = null;
HystrixCommandProperties.Setter commandPropertiesDefaults = HystrixCommandPropertiesTest.getUnitTestPropertiesSetter();
HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults = HystrixThreadPoolProperties.Setter.getUnitTestPropertiesBuilder();
HystrixCommandMetrics metrics = _cb.metrics;
HystrixCommandMetrics metrics;
AbstractCommand.TryableSemaphore fallbackSemaphore = null;
AbstractCommand.TryableSemaphore executionSemaphore = null;
TestableExecutionHook executionHook = new TestableExecutionHook();
Expand All @@ -63,8 +62,11 @@ TestCommandBuilder setThreadPoolKey(HystrixThreadPoolKey threadPoolKey) {
return this;
}

TestCommandBuilder setCircuitBreaker(HystrixCircuitBreaker circuitBreaker) {
TestCommandBuilder setCircuitBreaker(HystrixCircuitBreakerTest.TestCircuitBreaker circuitBreaker) {
this.circuitBreaker = circuitBreaker;
if (circuitBreaker != null) {
this.metrics = circuitBreaker.metrics;
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ static TestCommandBuilder testPropsBuilder() {
return new TestCommandBuilder(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
}

static TestCommandBuilder testPropsBuilder(HystrixCircuitBreakerTest.TestCircuitBreaker circuitBreaker) {
return new TestCommandBuilder(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD).setCircuitBreaker(circuitBreaker);
}

static TestCommandBuilder testPropsBuilder(HystrixCommandProperties.ExecutionIsolationStrategy isolationStrategy) {
return new TestCommandBuilder(isolationStrategy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ static TestCommandBuilder testPropsBuilder() {
return new TestCommandBuilder(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE);
}

static TestCommandBuilder testPropsBuilder(HystrixCommandProperties.ExecutionIsolationStrategy isolationStrategy) {
return new TestCommandBuilder(isolationStrategy);
static TestCommandBuilder testPropsBuilder(HystrixCircuitBreakerTest.TestCircuitBreaker circuitBreaker) {
return new TestCommandBuilder(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE).setCircuitBreaker(circuitBreaker);
}

static TestCommandBuilder testPropsBuilder(HystrixCommandProperties.ExecutionIsolationStrategy isolationStrategy, HystrixCircuitBreakerTest.TestCircuitBreaker circuitBreaker) {
return new TestCommandBuilder(isolationStrategy).setCircuitBreaker(circuitBreaker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public Throwable getFallbackException() {
private Throwable getException(List<Notification<?>> l) {
for (Notification<?> n: l) {
if (n.isOnError()) {
n.getThrowable().printStackTrace();
return n.getThrowable();
}
}
Expand Down

0 comments on commit ba30ec6

Please sign in to comment.