From 4eb9a641137e2d4c5fb7d3959d518335f59fbf3c Mon Sep 17 00:00:00 2001 From: single-wolf Date: Wed, 20 Jan 2021 22:25:26 +0800 Subject: [PATCH 1/3] Fix to prevent override a implicit AsyncTaskExecutor (#313) --- .../spring/cloud/async/DefaultAsyncAutoConfiguration.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/async/DefaultAsyncAutoConfiguration.java b/instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/async/DefaultAsyncAutoConfiguration.java index 3f341bc2..5a049a80 100644 --- a/instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/async/DefaultAsyncAutoConfiguration.java +++ b/instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/async/DefaultAsyncAutoConfiguration.java @@ -27,6 +27,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import org.springframework.scheduling.annotation.EnableAsync; @@ -47,7 +48,7 @@ public class DefaultAsyncAutoConfiguration { private Tracer tracer; @Configuration - @ConditionalOnMissingBean(AsyncConfigurer.class) + @ConditionalOnMissingBean({AsyncConfigurer.class, TaskExecutor.class, Executor.class}) static class DefaultTracedAsyncConfigurerSupport extends AsyncConfigurerSupport { @Autowired From f8862fc93736d6b98af51d9581a44fd09647b10e Mon Sep 17 00:00:00 2001 From: single-wolf Date: Wed, 3 Feb 2021 18:35:07 +0800 Subject: [PATCH 2/3] Add a test case (#313) --- .../cloud/async/AsyncImplicitOneTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java diff --git a/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java b/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java new file mode 100644 index 00000000..6ad7cff6 --- /dev/null +++ b/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java @@ -0,0 +1,82 @@ +/** + * Copyright 2017-2021 The OpenTracing 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 io.opentracing.contrib.spring.cloud.async; + +import io.opentracing.contrib.spring.cloud.MockTracingConfiguration; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.AsyncResult; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * Prevent overriding a implicit AsyncTaskExecutor {@linkplain DefaultAsyncAutoConfiguration} + * + * @author Jerry Zhong + */ +@SpringBootTest(classes = {AsyncImplicitOneTest.Configuration.class, MockTracingConfiguration.class, DefaultAsyncAutoConfiguration.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class AsyncImplicitOneTest { + + public static final String IMPLICIT_THREAD_GROUP = "implicit-thread-group"; + + @Autowired(required = false) + private AsyncConfigurer asyncConfigurer; + @Autowired + private AsyncService asyncService; + + @org.springframework.context.annotation.Configuration + static class Configuration { + + @Bean + public Executor threadPoolTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setThreadGroupName(IMPLICIT_THREAD_GROUP); + executor.initialize(); + return executor; + } + + @Bean + public AsyncService asyncService() { + return new AsyncService(); + } + } + + static class AsyncService { + + @Async + public Future asyncThreadGroupName() { + return new AsyncResult<>(Thread.currentThread().getThreadGroup().getName()); + } + } + + @Test + public void testNoOverrideImplicitOne() throws ExecutionException, InterruptedException { + assertNull(asyncConfigurer); + Future asyncFuture = asyncService.asyncThreadGroupName(); + assertEquals(IMPLICIT_THREAD_GROUP, asyncFuture.get()); + } +} From 71ad8ddabd9d90f93fbc8844df8e74eac8fe1be4 Mon Sep 17 00:00:00 2001 From: single-wolf Date: Wed, 3 Feb 2021 19:45:27 +0800 Subject: [PATCH 3/3] format test case (#313) --- .../cloud/async/AsyncImplicitOneTest.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java b/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java index 6ad7cff6..46794a00 100644 --- a/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java +++ b/instrument-starters/opentracing-spring-cloud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java @@ -1,11 +1,11 @@ /** * Copyright 2017-2021 The OpenTracing 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 @@ -13,7 +13,15 @@ */ package io.opentracing.contrib.spring.cloud.async; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + import io.opentracing.contrib.spring.cloud.MockTracingConfiguration; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -25,13 +33,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executor; -import java.util.concurrent.Future; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - /** * Prevent overriding a implicit AsyncTaskExecutor {@linkplain DefaultAsyncAutoConfiguration} *