From 0fc47467e878bab542941205153e9e23815c9321 Mon Sep 17 00:00:00 2001 From: wulingxiao <1251605638@qqcom> Date: Sun, 10 Jul 2022 12:58:19 +0800 Subject: [PATCH] add transmittable-thread-local dependency --- .../pom.xml | 7 + .../metadata/concurrent/MetadataCallable.java | 101 ------------- .../metadata/concurrent/MetadataRunnable.java | 98 ------------- .../metadata/concurrent/MetadataWrap.java | 5 +- .../concurrent/executor/MetadataExecutor.java | 7 +- .../executor/MetadataExecutorService.java | 20 +-- .../executor/MetadataExecutors.java | 1 - .../MetadataScheduledExecutorService.java | 14 +- .../concurrent/MetadataCallableTest.java | 131 ----------------- .../concurrent/MetadataRunnableTest.java | 133 ------------------ .../executor/MetadataExecutorServiceTest.java | 1 + .../executor/MetadataExecutorTest.java | 31 ++++ .../MetadataScheduledExecutorServiceTest.java | 1 + spring-cloud-tencent-dependencies/pom.xml | 8 ++ 14 files changed, 70 insertions(+), 488 deletions(-) delete mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataCallable.java delete mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataRunnable.java delete mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataCallableTest.java delete mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataRunnableTest.java diff --git a/spring-cloud-starter-tencent-metadata-transfer/pom.xml b/spring-cloud-starter-tencent-metadata-transfer/pom.xml index ad4714f7a..7cfc26733 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/pom.xml +++ b/spring-cloud-starter-tencent-metadata-transfer/pom.xml @@ -56,6 +56,13 @@ spring-cloud-starter-netflix-ribbon test + + + + com.alibaba + transmittable-thread-local + + diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataCallable.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataCallable.java deleted file mode 100644 index f7b4c4b35..000000000 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataCallable.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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.tencent.cloud.metadata.concurrent; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; - -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.metadata.MetadataContextHolder; - -/** - * {@link MetadataCallable} decorate {@link Callable} to get {@link MetadataContext} value - * and transfer it to the time of {@link Callable} execution, needed when use {@link Callable} to thread pool. - *

- * Use factory methods {@link #get} / {@link #gets} to create instance. - * - * @author wlx - */ -public final class MetadataCallable implements Callable, - MetadataWrap> { - - private final Callable delegate; - - private final AtomicReference metadataContextReference; - - private MetadataCallable(Callable delegate) { - this.delegate = delegate; - this.metadataContextReference = new AtomicReference<>(MetadataContextHolder.get()); - } - - @Override - public V call() throws Exception { - MetadataContext metadataContext = metadataContextReference.get(); - MetadataContext metadataContextBackup = MetadataContextHolder.get(); - MetadataContextHolder.set(metadataContext); - try { - return delegate.call(); - } - finally { - MetadataContextHolder.set(metadataContextBackup); - } - } - - /** - * Factory method to create {@link MetadataCallable} instance. - * - * @param delegate delegate - * @param MetadataCallable return type - * @return {@link MetadataCallable} instance - */ - public static Callable get(Callable delegate) { - if (null == delegate || delegate instanceof MetadataCallable) { - return delegate; - } - else { - return new MetadataCallable<>(delegate); - } - } - - - /** - * Factory method to create some {@link MetadataCallable} instance. - * - * @param delegates delegates - * @param MetadataCallable return type - * @return some {@link MetadataCallable} instance - */ - public static List> gets(Collection> delegates) { - if (delegates == null) { - return Collections.emptyList(); - } - return delegates.stream().map( - MetadataCallable::get - ).collect(Collectors.toList()); - } - - @Override - public Callable unWrap() { - return this.delegate; - } -} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataRunnable.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataRunnable.java deleted file mode 100644 index a48d13482..000000000 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataRunnable.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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.tencent.cloud.metadata.concurrent; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; - -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.metadata.MetadataContextHolder; - -/** - * {@link MetadataRunnable} decorate {@link Runnable} to get {@link MetadataContext} value - * and transfer it to the time of {@link Runnable} execution, needed when use {@link Runnable} to thread pool. - *

- * Use factory methods {@link #get} / {@link #gets} to create instance. - *

- * - * @author wlx - */ -public final class MetadataRunnable implements Runnable, - MetadataWrap { - - private final Runnable delegate; - - private final AtomicReference metadataContextReference; - - private MetadataRunnable(Runnable delegate) { - this.delegate = delegate; - this.metadataContextReference = new AtomicReference<>(MetadataContextHolder.get()); - } - - @Override - public void run() { - MetadataContext metadataContext = metadataContextReference.get(); - MetadataContext metadataContextBackup = MetadataContextHolder.get(); - MetadataContextHolder.set(metadataContext); - try { - delegate.run(); - } - finally { - MetadataContextHolder.set(metadataContextBackup); - } - } - - /** - * Factory method to create {@link MetadataRunnable} instance. - * - * @param delegate delegate - * @return MetadataRunnable instance - */ - public static Runnable get(Runnable delegate) { - if (null == delegate || delegate instanceof MetadataRunnable) { - return delegate; - } - else { - return new MetadataRunnable(delegate); - } - } - - /** - * Factory method to create {@link MetadataRunnable} instance. - * - * @param delegates delegates - * @return MetadataRunnable instances - */ - public static List gets(Collection delegates) { - if (delegates == null) { - return Collections.emptyList(); - } - return delegates.stream().map( - MetadataRunnable::get - ).collect(Collectors.toList()); - } - - @Override - public Runnable unWrap() { - return this.delegate; - } -} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataWrap.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataWrap.java index 59583a8b8..46c8203c3 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataWrap.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/MetadataWrap.java @@ -22,10 +22,6 @@ package com.tencent.cloud.metadata.concurrent; * Metadata Wrapper interface. *

* Used to mark wrapper types, for example: - *

* * @author wlx */ @@ -37,4 +33,5 @@ public interface MetadataWrap { * @return a unWrap instance */ T unWrap(); + } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutor.java index 734f618fe..9c9447036 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutor.java @@ -21,8 +21,8 @@ package com.tencent.cloud.metadata.concurrent.executor; import java.util.Objects; import java.util.concurrent.Executor; +import com.alibaba.ttl.threadpool.TtlExecutors; import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.metadata.concurrent.MetadataRunnable; import com.tencent.cloud.metadata.concurrent.MetadataWrap; import org.springframework.lang.NonNull; @@ -39,12 +39,12 @@ class MetadataExecutor implements Executor, MetadataWrap { private final Executor delegate; MetadataExecutor(Executor delegate) { - this.delegate = delegate; + this.delegate = TtlExecutors.getTtlExecutor(delegate); } @Override public void execute(@NonNull Runnable command) { - delegate.execute(MetadataRunnable.get(command)); + delegate.execute(command); } @Override @@ -68,4 +68,5 @@ class MetadataExecutor implements Executor, MetadataWrap { public Executor unWrap() { return this.delegate; } + } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorService.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorService.java index a50a1aafa..cd0cec234 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorService.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorService.java @@ -28,9 +28,8 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import com.alibaba.ttl.threadpool.TtlExecutors; import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.metadata.concurrent.MetadataCallable; -import com.tencent.cloud.metadata.concurrent.MetadataRunnable; import org.springframework.lang.NonNull; @@ -47,7 +46,7 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic MetadataExecutorService(ExecutorService delegate) { super(delegate); - this.delegate = delegate; + this.delegate = TtlExecutors.getTtlExecutorService(delegate); } @Override @@ -79,42 +78,42 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic @Override @NonNull public Future submit(@NonNull Callable task) { - return this.delegate.submit(MetadataCallable.get(task)); + return this.delegate.submit(task); } @Override @NonNull public Future submit(@NonNull Runnable task, T result) { - return this.delegate.submit(MetadataRunnable.get(task), result); + return this.delegate.submit(task, result); } @Override @NonNull public Future submit(@NonNull Runnable task) { - return this.delegate.submit(MetadataRunnable.get(task)); + return this.delegate.submit(task); } @Override @NonNull public List> invokeAll(@NonNull Collection> tasks) throws InterruptedException { - return this.delegate.invokeAll(MetadataCallable.gets(tasks)); + return this.delegate.invokeAll(tasks); } @Override @NonNull public List> invokeAll(@NonNull Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException { - return this.delegate.invokeAll(MetadataCallable.gets(tasks), timeout, unit); + return this.delegate.invokeAll(tasks, timeout, unit); } @Override @NonNull public T invokeAny(@NonNull Collection> tasks) throws InterruptedException, ExecutionException { - return this.delegate.invokeAny(MetadataCallable.gets(tasks)); + return this.delegate.invokeAny(tasks); } @Override public T invokeAny(@NonNull Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return this.delegate.invokeAny(MetadataCallable.gets(tasks), timeout, unit); + return this.delegate.invokeAny(tasks, timeout, unit); } @Override @@ -138,4 +137,5 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic public int hashCode() { return Objects.hash(delegate); } + } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutors.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutors.java index 2d2d34c43..030c2cf9e 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutors.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutors.java @@ -104,7 +104,6 @@ public final class MetadataExecutors { } private MetadataExecutors() { - } } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorService.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorService.java index 80c572ff8..8f4a955bc 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorService.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorService.java @@ -24,9 +24,8 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import com.alibaba.ttl.threadpool.TtlExecutors; import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.metadata.concurrent.MetadataCallable; -import com.tencent.cloud.metadata.concurrent.MetadataRunnable; import org.springframework.lang.NonNull; @@ -44,31 +43,31 @@ class MetadataScheduledExecutorService extends MetadataExecutorService MetadataScheduledExecutorService(ScheduledExecutorService delegate) { super(delegate); - this.delegate = delegate; + this.delegate = TtlExecutors.getTtlScheduledExecutorService(delegate); } @Override @NonNull public ScheduledFuture schedule(@NonNull Runnable command, long delay, @NonNull TimeUnit unit) { - return this.delegate.schedule(MetadataRunnable.get(command), delay, unit); + return this.delegate.schedule(command, delay, unit); } @Override @NonNull public ScheduledFuture schedule(@NonNull Callable callable, long delay, @NonNull TimeUnit unit) { - return this.delegate.schedule(MetadataCallable.get(callable), delay, unit); + return this.delegate.schedule(callable, delay, unit); } @Override @NonNull public ScheduledFuture scheduleAtFixedRate(@NonNull Runnable command, long initialDelay, long period, @NonNull TimeUnit unit) { - return this.delegate.scheduleAtFixedRate(MetadataRunnable.get(command), initialDelay, period, unit); + return this.delegate.scheduleAtFixedRate(command, initialDelay, period, unit); } @Override @NonNull public ScheduledFuture scheduleWithFixedDelay(@NonNull Runnable command, long initialDelay, long delay, @NonNull TimeUnit unit) { - return this.delegate.scheduleAtFixedRate(MetadataRunnable.get(command), initialDelay, delay, unit); + return this.delegate.scheduleAtFixedRate(command, initialDelay, delay, unit); } @Override @@ -92,4 +91,5 @@ class MetadataScheduledExecutorService extends MetadataExecutorService public int hashCode() { return Objects.hash(delegate); } + } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataCallableTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataCallableTest.java deleted file mode 100644 index fb78347d3..000000000 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataCallableTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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.tencent.cloud.metadata.concurrent; - -import java.util.Map; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.metadata.MetadataContextHolder; -import org.assertj.core.api.Assertions; -import org.junit.AfterClass; -import org.junit.Test; -import org.junit.runner.RunWith; - -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; - -/** - * Test for {@link MetadataCallable}. - * - * @author wlx - */ -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, - classes = MetadataCallableTest.TestApplication.class, - properties = {"spring.config.location = classpath:application-test.yml", - "spring.main.web-application-type = servlet", - "spring.cloud.gateway.enabled = false"}) -public class MetadataCallableTest { - - private static final ExecutorService executor = Executors.newFixedThreadPool(1); - - @Test - public void threadMultiplexingTest() throws InterruptedException, ExecutionException, TimeoutException { - - Future> future = executor.submit(() -> { - return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - - }); - Map map = future.get(200, TimeUnit.MILLISECONDS); - - // init after new Task, won't see parent value in in task! - MetadataTestUtil.initMetadataContext(); - - Future> future1 = executor.submit(() -> { - return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - }); - - Map map1 = future1.get(200, TimeUnit.MILLISECONDS); - - Map fragmentContext = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Assertions.assertThat(fragmentContext.get("a")).isEqualTo("1"); - Assertions.assertThat(fragmentContext.get("b")).isEqualTo("2"); - - // init after new Task, won't see parent value in in task!,so before init and after init task res will be same! - Assertions.assertThat(map.equals(map1)).isTrue(); - } - - @Test - public void metadataCallableTest() throws InterruptedException, ExecutionException, TimeoutException { - Future> future = executor.submit( - MetadataCallable.get( - () -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE) - ) - ); - Map map = future.get(200, TimeUnit.MILLISECONDS); - - Assertions.assertThat(map.equals(MetadataContextHolder.get() - .getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE))); - - MetadataTestUtil.initMetadataContext(); - - Future> future1 = executor.submit( - MetadataCallable.get( - () -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE) - ) - ); - - Map map1 = future1.get(200, TimeUnit.MILLISECONDS); - - Map fragmentContext = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Assertions.assertThat(fragmentContext.get("a")).isEqualTo("1"); - Assertions.assertThat(fragmentContext.get("b")).isEqualTo("2"); - - Assertions.assertThat(fragmentContext.equals(map1)).isTrue(); - } - - @Test - public void metadataCallableWrap() { - Callable> callable = MetadataCallable.get( - () -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE) - ); - Assertions.assertThat(callable instanceof MetadataCallable).isTrue(); - } - - @AfterClass - public static void cleanUp() { - executor.shutdownNow(); - } - - @SpringBootApplication - protected static class TestApplication { - } -} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataRunnableTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataRunnableTest.java deleted file mode 100644 index ba55a69ed..000000000 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/MetadataRunnableTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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.tencent.cloud.metadata.concurrent; - -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.metadata.MetadataContextHolder; -import org.assertj.core.api.Assertions; -import org.junit.AfterClass; -import org.junit.Test; -import org.junit.runner.RunWith; - -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; - -/** - * Test for {@link MetadataRunnable}. - * - * @author wlx - */ -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, - classes = MetadataRunnableTest.TestApplication.class, - properties = {"spring.config.location = classpath:application-test.yml", - "spring.main.web-application-type = servlet", - "spring.cloud.gateway.enabled = false"}) -public class MetadataRunnableTest { - - private static final ExecutorService executor = Executors.newFixedThreadPool(1); - - @Test - public void threadMultiplexingTest() { - Map fragmentContextBeforeInit = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - - executor.submit(() -> { - Map fragmentContext = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isTrue(); - }); - - // init after new Task, won't see parent value in in task! - MetadataTestUtil.initMetadataContext(); - - Map fragmentContextAfterInit = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - - - executor.submit(() -> { - Map fragmentContext = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - // init after new Task, won't see parent value in in task! - // so before init and after init task res will be same! - Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isTrue(); - }); - - Assertions.assertThat(fragmentContextAfterInit.get("a")).isEqualTo("1"); - Assertions.assertThat(fragmentContextAfterInit.get("b")).isEqualTo("2"); - - } - - @Test - public void metadataRunnableTest() { - Map fragmentContextBeforeInit = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - - executor.submit(MetadataRunnable.get( - () -> { - Map fragmentContext = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)); - } - )); - - MetadataTestUtil.initMetadataContext(); - - Map fragmentContextAfterInit = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - - executor.submit(MetadataRunnable.get( - () -> { - Map fragmentContext = - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isFalse(); - Assertions.assertThat(fragmentContextAfterInit.equals(fragmentContext)).isTrue(); - } - )); - - Assertions.assertThat(fragmentContextAfterInit.get("a")).isEqualTo("1"); - Assertions.assertThat(fragmentContextAfterInit.get("b")).isEqualTo("2"); - } - - @Test - public void metadataRunnableWrapTest() { - Runnable runnable = MetadataRunnable.get( - () -> { - MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - } - ); - Assertions.assertThat(runnable instanceof MetadataRunnable).isTrue(); - } - - @AfterClass - public static void cleanUp() { - executor.shutdownNow(); - } - - @SpringBootApplication - protected static class TestApplication { - } -} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorServiceTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorServiceTest.java index 88a9723ed..1c21c36de 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorServiceTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorServiceTest.java @@ -124,4 +124,5 @@ public class MetadataExecutorServiceTest { @SpringBootApplication protected static class TestApplication { } + } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorTest.java index 762c157ac..068d04875 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataExecutorTest.java @@ -88,6 +88,36 @@ public class MetadataExecutorTest { Assertions.assertThat(executor).isEqualTo(executorService); } + @Test + public void threadMultiplexingTest() { + Map fragmentContextBeforeInit = + MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + + executorService.execute(() -> { + Map fragmentContext = + MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isTrue(); + }); + + // init after new Task, won't see parent value in in task! + MetadataTestUtil.initMetadataContext(); + + Map fragmentContextAfterInit = + MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + + + executorService.execute(() -> { + Map fragmentContext = + MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + // init after new Task, won't see parent value in in task! + // so before init and after init task res will be same! + Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isTrue(); + }); + + Assertions.assertThat(fragmentContextAfterInit.get("a")).isEqualTo("1"); + Assertions.assertThat(fragmentContextAfterInit.get("b")).isEqualTo("2"); + } + @AfterClass public static void cleanUp() { if (executorService instanceof ExecutorService) { @@ -99,4 +129,5 @@ public class MetadataExecutorTest { @SpringBootApplication protected static class TestApplication { } + } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorServiceTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorServiceTest.java index 631d0ab6f..a281d225c 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorServiceTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/concurrent/executor/MetadataScheduledExecutorServiceTest.java @@ -85,4 +85,5 @@ public class MetadataScheduledExecutorServiceTest { @SpringBootApplication protected static class TestApplication { } + } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 2027ffae6..a0abe8b36 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -79,6 +79,7 @@ 3.16.1 1.69 31.0.1-jre + 2.12.0 3.2.0 @@ -234,6 +235,13 @@ ${byte-buddy.version} test + + + com.alibaba + transmittable-thread-local + ${transmittable-thread-local.version} + +