/*
* Copyright 2016 Naver Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.plugin.hystrix;
import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.plugin.hystrix.commands.SayHelloCommand;
import com.navercorp.pinpoint.plugin.hystrix.commands.ThrowExceptionCommand;
import com.navercorp.pinpoint.plugin.hystrix.commands.ThrowExceptionCommandWithFallback;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.netflix.hystrix.HystrixCommand;
import org.junit.Test;
import org.junit.runner.RunWith;
import rx.Subscriber;
import java.lang.reflect.Method;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.annotation;
/**
* Integration test for hystrix-core 1.4.1 - 1.4.2 traces the following :
* <p>
* <tt>HystrixCommand.queue()</tt><br/>
* <tt>HystrixCommand$1.call(Subscriber<? super R>)</tt> - Anonymous inner class that invokes run()<br/>
* <tt>HystrixCommand$2.call(Subscriber<? super R>)</tt> - Anonymous inner class that invokes getFallback()<br/>
* <p>
* The cause of fallback is not traced as AbstractCommand.getExecutionException() method is missing.
* <p>
* Same as {@link HystrixCommand_1_4_3_to_1_5_2_IT} but separated as Java 6 was dropped for
* Hystrix 1.4.1 and 1.4.2 - https://github.com/Netflix/Hystrix/issues/702
* <p>
* Created by jack on 4/22/16.
*/
@RunWith(PinpointPluginTestSuite.class)
@JvmVersion(7)
@Dependency({"com.netflix.hystrix:hystrix-core:[1.4.1,1.4.2]", "com.netflix.hystrix:hystrix-metrics-event-stream:1.1.2"})
public class HystrixCommand_1_4_1_to_1_4_2_IT extends HystrixCommandITBase {
private static final String EXECUTION_OBSERVABLE_INNER_CLASS = "com.netflix.hystrix.HystrixCommand$1";
private static final String FALLBACK_OBSERVABLE_INNER_CLASS = "com.netflix.hystrix.HystrixCommand$2";
@Test
public void testSyncCall() throws Exception {
String name = "Pinpoint";
executeSayHelloCommand(name);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
Method queue = HystrixCommand.class.getMethod("queue");
Class<?> executionObservableClazz = Class.forName(EXECUTION_OBSERVABLE_INNER_CLASS);
Method executioObservableCallCmd = executionObservableClazz.getDeclaredMethod("call", Subscriber.class);
verifier.verifyTrace(Expectations.async(
Expectations.event("HYSTRIX_COMMAND", queue, annotation("hystrix.command", SayHelloCommand.class.getSimpleName())),
Expectations.event("ASYNC", "Asynchronous Invocation"),
Expectations.event("HYSTRIX_COMMAND_INTERNAL", executioObservableCallCmd,
annotation("hystrix.command.execution", "run"))
));
// no more traces
verifier.verifyTraceCount(0);
}
@Test
public void testAsyncCall() throws Exception {
String name = "Pinpoint";
queueAndGetSayHelloCommand(name);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
Method queue = HystrixCommand.class.getMethod("queue");
Class<?> executionObservableClazz = Class.forName(EXECUTION_OBSERVABLE_INNER_CLASS);
Method executionObservableCallCmd = executionObservableClazz.getDeclaredMethod("call", Subscriber.class);
verifier.verifyTrace(Expectations.async(
Expectations.event("HYSTRIX_COMMAND", queue, annotation("hystrix.command", SayHelloCommand.class.getSimpleName())),
Expectations.event("ASYNC", "Asynchronous Invocation"),
Expectations.event("HYSTRIX_COMMAND_INTERNAL", executionObservableCallCmd,
annotation("hystrix.command.execution", "run"))
));
// no more traces
verifier.verifyTraceCount(0);
}
@Test
public void testExecutionException() throws Exception {
Exception expectedException = new RuntimeException("expected");
executeThrowExceptionCommand(expectedException);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
Method queue = HystrixCommand.class.getMethod("queue");
Class<?> executionObservableClazz = Class.forName(EXECUTION_OBSERVABLE_INNER_CLASS);
Method executionObservableCallCmd = executionObservableClazz.getDeclaredMethod("call", Subscriber.class);
Class<?> fallbackObservableClazz = Class.forName(FALLBACK_OBSERVABLE_INNER_CLASS);
Method fallbackObservableCallCmd = fallbackObservableClazz.getDeclaredMethod("call", Subscriber.class);
verifier.verifyTrace(Expectations.async(
Expectations.event("HYSTRIX_COMMAND", queue, annotation("hystrix.command", ThrowExceptionCommand.class.getSimpleName())),
Expectations.event("ASYNC", "Asynchronous Invocation"),
Expectations.event("HYSTRIX_COMMAND_INTERNAL", executionObservableCallCmd,
annotation("hystrix.command.execution", "run")),
Expectations.event("HYSTRIX_COMMAND_INTERNAL", fallbackObservableCallCmd,
annotation("hystrix.command.execution", "fallback"))
));
// no more traces
verifier.verifyTraceCount(0);
}
@Test
public void testExecutionExceptionWithFallback() throws Exception {
Exception expectedException = new RuntimeException("expected");
String fallbackMessage = "Fallback";
executeThrowExceptionWithFallbackCommand(expectedException, fallbackMessage);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
Method queue = HystrixCommand.class.getMethod("queue");
Class<?> executionObservableClazz = Class.forName(EXECUTION_OBSERVABLE_INNER_CLASS);
Method executionObservableCallCmd = executionObservableClazz.getDeclaredMethod("call", Subscriber.class);
Class<?> fallbackObservableClazz = Class.forName(FALLBACK_OBSERVABLE_INNER_CLASS);
Method fallbackObservableCallCmd = fallbackObservableClazz.getDeclaredMethod("call", Subscriber.class);
verifier.verifyTrace(Expectations.async(
Expectations.event("HYSTRIX_COMMAND", queue, annotation("hystrix.command", ThrowExceptionCommandWithFallback.class.getSimpleName())),
Expectations.event("ASYNC", "Asynchronous Invocation"),
Expectations.event("HYSTRIX_COMMAND_INTERNAL", executionObservableCallCmd,
annotation("hystrix.command.execution", "run")),
Expectations.event("HYSTRIX_COMMAND_INTERNAL", fallbackObservableCallCmd,
annotation("hystrix.command.execution", "fallback"))
));
// no more traces
verifier.verifyTraceCount(0);
}
@Test
public void testTraceContinuation() throws Exception {
String name = "Pinpoint";
executeInvokeSayHelloCommand(name);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
// If the trace is propagated properly to a HystrixCommand's run() method, there should be 6 total traces.
// 3 for InvokeSayHelloCommand, 3 for SayHelloCommand.
verifier.verifyTraceCount(6);
}
}