From 8749854a8ad476ab3c96f9a93016c7fdf34cd255 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 21 Mar 2016 14:58:22 +0100 Subject: [PATCH] Added tests for HystrixConcurrencyStrategy --- .../SleuthHystrixConcurrencyStrategy.java | 7 +- .../SleuthHystrixConcurrencyStrategyTest.java | 107 ++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategyTest.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategy.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategy.java index b8ffe7d09..de415d533 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategy.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategy.java @@ -79,13 +79,13 @@ public class SleuthHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy private void logCurrentStateOfHysrixPlugins(HystrixEventNotifier eventNotifier, HystrixMetricsPublisher metricsPublisher, HystrixPropertiesStrategy propertiesStrategy) { - log.info("Current Hystrix plugins configuration is [" + log.debug("Current Hystrix plugins configuration is [" + "concurrencyStrategy [" + this.delegate + "]," + "eventNotifier [" + eventNotifier + "]," + "metricPublisher [" + metricsPublisher + "]," + "propertiesStrategy [" + propertiesStrategy + "]," + "]"); - log.info("Resetting Hystrix custom strategies and registering Sleuth Hystrix Concurrency Strategy."); + log.debug("Registering Sleuth Hystrix Concurrency Strategy."); } @Override @@ -95,7 +95,8 @@ public class SleuthHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy return new HystrixTraceCallable<>(this.tracer, this.traceKeys, wrappedCallable); } - private static class HystrixTraceCallable implements Callable { + // Visible for testing + static class HystrixTraceCallable implements Callable { private Tracer tracer; private TraceKeys traceKeys; diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategyTest.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategyTest.java new file mode 100644 index 000000000..087187403 --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/SleuthHystrixConcurrencyStrategyTest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2013-2016 the original author or authors. + * + * 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 org.springframework.cloud.sleuth.instrument.hystrix; + +import java.util.concurrent.Callable; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.cloud.sleuth.TraceKeys; +import org.springframework.cloud.sleuth.Tracer; + +import com.netflix.hystrix.strategy.HystrixPlugins; +import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy; +import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier; +import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook; +import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher; +import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Marcin Grzejszczak + */ +@RunWith(MockitoJUnitRunner.class) +public class SleuthHystrixConcurrencyStrategyTest { + + @Mock Tracer tracer; + TraceKeys traceKeys = new TraceKeys(); + + @Before + @After + public void setup() { + HystrixPlugins.reset(); + } + + @Test + public void should_not_override_existing_custom_strategies() { + HystrixPlugins.getInstance().registerCommandExecutionHook(new MyHystrixCommandExecutionHook()); + HystrixPlugins.getInstance().registerEventNotifier(new MyHystrixEventNotifier()); + HystrixPlugins.getInstance().registerMetricsPublisher(new MyHystrixMetricsPublisher()); + HystrixPlugins.getInstance().registerPropertiesStrategy(new MyHystrixPropertiesStrategy()); + + new SleuthHystrixConcurrencyStrategy(this.tracer, this.traceKeys); + + then(HystrixPlugins + .getInstance().getCommandExecutionHook()).isExactlyInstanceOf(MyHystrixCommandExecutionHook.class); + then(HystrixPlugins.getInstance() + .getEventNotifier()).isExactlyInstanceOf(MyHystrixEventNotifier.class); + then(HystrixPlugins.getInstance() + .getMetricsPublisher()).isExactlyInstanceOf(MyHystrixMetricsPublisher.class); + then(HystrixPlugins.getInstance() + .getPropertiesStrategy()).isExactlyInstanceOf(MyHystrixPropertiesStrategy.class); + } + + @Test + public void should_wrap_delegates_callable_in_trace_callable_when_delegate_is_present() + throws Exception { + HystrixPlugins.getInstance().registerConcurrencyStrategy(new MyHystrixConcurrencyStrategy()); + SleuthHystrixConcurrencyStrategy strategy = new SleuthHystrixConcurrencyStrategy( + this.tracer, this.traceKeys); + + Callable callable = strategy.wrapCallable(() -> "hello"); + + then(callable).isInstanceOf(SleuthHystrixConcurrencyStrategy.HystrixTraceCallable.class); + then(callable.call()).isEqualTo("executed_custom_callable"); + } + + @Test + public void should_wrap_callable_in_trace_callable_when_delegate_is_present() + throws Exception { + SleuthHystrixConcurrencyStrategy strategy = new SleuthHystrixConcurrencyStrategy( + this.tracer, this.traceKeys); + + Callable callable = strategy.wrapCallable(() -> "hello"); + + then(callable).isInstanceOf(SleuthHystrixConcurrencyStrategy.HystrixTraceCallable.class); + } + + static class MyHystrixCommandExecutionHook extends HystrixCommandExecutionHook {} + @SuppressWarnings("unchecked") + static class MyHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy { + @Override public Callable wrapCallable(Callable callable) { + return () -> (T) "executed_custom_callable"; + } + } + static class MyHystrixEventNotifier extends HystrixEventNotifier {} + static class MyHystrixMetricsPublisher extends HystrixMetricsPublisher {} + static class MyHystrixPropertiesStrategy extends HystrixPropertiesStrategy {} +} \ No newline at end of file