add transmittable-thread-local dependency

pull/422/head
wulingxiao 3 years ago
parent 01f81b98cf
commit 0fc47467e8

@ -56,6 +56,13 @@
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/transmittable-thread-local -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -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.
* <p>
* Use factory methods {@link #get} / {@link #gets} to create instance.
*
* @author wlx
*/
public final class MetadataCallable<V> implements Callable<V>,
MetadataWrap<Callable<V>> {
private final Callable<V> delegate;
private final AtomicReference<MetadataContext> metadataContextReference;
private MetadataCallable(Callable<V> 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 <V> MetadataCallable return type
* @return {@link MetadataCallable} instance
*/
public static <V> Callable<V> get(Callable<V> 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 <V> MetadataCallable return type
* @return some {@link MetadataCallable} instance
*/
public static <V> List<Callable<V>> gets(Collection<? extends Callable<V>> delegates) {
if (delegates == null) {
return Collections.emptyList();
}
return delegates.stream().map(
MetadataCallable::get
).collect(Collectors.toList());
}
@Override
public Callable<V> unWrap() {
return this.delegate;
}
}

@ -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.
* <p>
* Use factory methods {@link #get} / {@link #gets} to create instance.
* <p>
*
* @author wlx
*/
public final class MetadataRunnable implements Runnable,
MetadataWrap<Runnable> {
private final Runnable delegate;
private final AtomicReference<MetadataContext> 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<Runnable> gets(Collection<Runnable> delegates) {
if (delegates == null) {
return Collections.emptyList();
}
return delegates.stream().map(
MetadataRunnable::get
).collect(Collectors.toList());
}
@Override
public Runnable unWrap() {
return this.delegate;
}
}

@ -22,10 +22,6 @@ package com.tencent.cloud.metadata.concurrent;
* Metadata Wrapper interface. * Metadata Wrapper interface.
* <p> * <p>
* Used to mark wrapper types, for example: * Used to mark wrapper types, for example:
* <ul>
* <li>{@link MetadataCallable}</li>
* <li>{@link MetadataRunnable}</li>
* </ul>
* *
* @author wlx * @author wlx
*/ */
@ -37,4 +33,5 @@ public interface MetadataWrap<T> {
* @return a unWrap instance * @return a unWrap instance
*/ */
T unWrap(); T unWrap();
} }

@ -21,8 +21,8 @@ package com.tencent.cloud.metadata.concurrent.executor;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.metadata.concurrent.MetadataRunnable;
import com.tencent.cloud.metadata.concurrent.MetadataWrap; import com.tencent.cloud.metadata.concurrent.MetadataWrap;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -39,12 +39,12 @@ class MetadataExecutor implements Executor, MetadataWrap<Executor> {
private final Executor delegate; private final Executor delegate;
MetadataExecutor(Executor delegate) { MetadataExecutor(Executor delegate) {
this.delegate = delegate; this.delegate = TtlExecutors.getTtlExecutor(delegate);
} }
@Override @Override
public void execute(@NonNull Runnable command) { public void execute(@NonNull Runnable command) {
delegate.execute(MetadataRunnable.get(command)); delegate.execute(command);
} }
@Override @Override
@ -68,4 +68,5 @@ class MetadataExecutor implements Executor, MetadataWrap<Executor> {
public Executor unWrap() { public Executor unWrap() {
return this.delegate; return this.delegate;
} }
} }

@ -28,9 +28,8 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.tencent.cloud.common.metadata.MetadataContext; 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; import org.springframework.lang.NonNull;
@ -47,7 +46,7 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic
MetadataExecutorService(ExecutorService delegate) { MetadataExecutorService(ExecutorService delegate) {
super(delegate); super(delegate);
this.delegate = delegate; this.delegate = TtlExecutors.getTtlExecutorService(delegate);
} }
@Override @Override
@ -79,42 +78,42 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic
@Override @Override
@NonNull @NonNull
public <T> Future<T> submit(@NonNull Callable<T> task) { public <T> Future<T> submit(@NonNull Callable<T> task) {
return this.delegate.submit(MetadataCallable.get(task)); return this.delegate.submit(task);
} }
@Override @Override
@NonNull @NonNull
public <T> Future<T> submit(@NonNull Runnable task, T result) { public <T> Future<T> submit(@NonNull Runnable task, T result) {
return this.delegate.submit(MetadataRunnable.get(task), result); return this.delegate.submit(task, result);
} }
@Override @Override
@NonNull @NonNull
public Future<?> submit(@NonNull Runnable task) { public Future<?> submit(@NonNull Runnable task) {
return this.delegate.submit(MetadataRunnable.get(task)); return this.delegate.submit(task);
} }
@Override @Override
@NonNull @NonNull
public <T> List<Future<T>> invokeAll(@NonNull Collection<? extends Callable<T>> tasks) throws InterruptedException { public <T> List<Future<T>> invokeAll(@NonNull Collection<? extends Callable<T>> tasks) throws InterruptedException {
return this.delegate.invokeAll(MetadataCallable.gets(tasks)); return this.delegate.invokeAll(tasks);
} }
@Override @Override
@NonNull @NonNull
public <T> List<Future<T>> invokeAll(@NonNull Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException { public <T> List<Future<T>> invokeAll(@NonNull Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return this.delegate.invokeAll(MetadataCallable.gets(tasks), timeout, unit); return this.delegate.invokeAll(tasks, timeout, unit);
} }
@Override @Override
@NonNull @NonNull
public <T> T invokeAny(@NonNull Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { public <T> T invokeAny(@NonNull Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return this.delegate.invokeAny(MetadataCallable.gets(tasks)); return this.delegate.invokeAny(tasks);
} }
@Override @Override
public <T> T invokeAny(@NonNull Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { public <T> T invokeAny(@NonNull Collection<? extends Callable<T>> 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 @Override
@ -138,4 +137,5 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic
public int hashCode() { public int hashCode() {
return Objects.hash(delegate); return Objects.hash(delegate);
} }
} }

@ -104,7 +104,6 @@ public final class MetadataExecutors {
} }
private MetadataExecutors() { private MetadataExecutors() {
} }
} }

@ -24,9 +24,8 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.tencent.cloud.common.metadata.MetadataContext; 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; import org.springframework.lang.NonNull;
@ -44,31 +43,31 @@ class MetadataScheduledExecutorService extends MetadataExecutorService
MetadataScheduledExecutorService(ScheduledExecutorService delegate) { MetadataScheduledExecutorService(ScheduledExecutorService delegate) {
super(delegate); super(delegate);
this.delegate = delegate; this.delegate = TtlExecutors.getTtlScheduledExecutorService(delegate);
} }
@Override @Override
@NonNull @NonNull
public ScheduledFuture<?> schedule(@NonNull Runnable command, long delay, @NonNull TimeUnit unit) { 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 @Override
@NonNull @NonNull
public <V> ScheduledFuture<V> schedule(@NonNull Callable<V> callable, long delay, @NonNull TimeUnit unit) { public <V> ScheduledFuture<V> schedule(@NonNull Callable<V> callable, long delay, @NonNull TimeUnit unit) {
return this.delegate.schedule(MetadataCallable.get(callable), delay, unit); return this.delegate.schedule(callable, delay, unit);
} }
@Override @Override
@NonNull @NonNull
public ScheduledFuture<?> scheduleAtFixedRate(@NonNull Runnable command, long initialDelay, long period, @NonNull TimeUnit unit) { 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 @Override
@NonNull @NonNull
public ScheduledFuture<?> scheduleWithFixedDelay(@NonNull Runnable command, long initialDelay, long delay, @NonNull TimeUnit unit) { 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 @Override
@ -92,4 +91,5 @@ class MetadataScheduledExecutorService extends MetadataExecutorService
public int hashCode() { public int hashCode() {
return Objects.hash(delegate); return Objects.hash(delegate);
} }
} }

@ -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<Map<String, String>> future = executor.submit(() -> {
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
});
Map<String, String> map = future.get(200, TimeUnit.MILLISECONDS);
// init after new Task, won't see parent value in in task!
MetadataTestUtil.initMetadataContext();
Future<Map<String, String>> future1 = executor.submit(() -> {
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
});
Map<String, String> map1 = future1.get(200, TimeUnit.MILLISECONDS);
Map<String, String> 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<Map<String, String>> future = executor.submit(
MetadataCallable.get(
() -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
)
);
Map<String, String> map = future.get(200, TimeUnit.MILLISECONDS);
Assertions.assertThat(map.equals(MetadataContextHolder.get()
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)));
MetadataTestUtil.initMetadataContext();
Future<Map<String, String>> future1 = executor.submit(
MetadataCallable.get(
() -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
)
);
Map<String, String> map1 = future1.get(200, TimeUnit.MILLISECONDS);
Map<String, String> 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<Map<String, String>> 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 {
}
}

@ -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<String, String> fragmentContextBeforeInit =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
executor.submit(() -> {
Map<String, String> 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<String, String> fragmentContextAfterInit =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
executor.submit(() -> {
Map<String, String> 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<String, String> fragmentContextBeforeInit =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
executor.submit(MetadataRunnable.get(
() -> {
Map<String, String> fragmentContext =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext));
}
));
MetadataTestUtil.initMetadataContext();
Map<String, String> fragmentContextAfterInit =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
executor.submit(MetadataRunnable.get(
() -> {
Map<String, String> 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 {
}
}

@ -124,4 +124,5 @@ public class MetadataExecutorServiceTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
} }
} }

@ -88,6 +88,36 @@ public class MetadataExecutorTest {
Assertions.assertThat(executor).isEqualTo(executorService); Assertions.assertThat(executor).isEqualTo(executorService);
} }
@Test
public void threadMultiplexingTest() {
Map<String, String> fragmentContextBeforeInit =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
executorService.execute(() -> {
Map<String, String> 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<String, String> fragmentContextAfterInit =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
executorService.execute(() -> {
Map<String, String> 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 @AfterClass
public static void cleanUp() { public static void cleanUp() {
if (executorService instanceof ExecutorService) { if (executorService instanceof ExecutorService) {
@ -99,4 +129,5 @@ public class MetadataExecutorTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
} }
} }

@ -85,4 +85,5 @@ public class MetadataScheduledExecutorServiceTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
} }
} }

@ -79,6 +79,7 @@
<protobuf-java.version>3.16.1</protobuf-java.version> <protobuf-java.version>3.16.1</protobuf-java.version>
<bcprov-jdk15on.version>1.69</bcprov-jdk15on.version> <bcprov-jdk15on.version>1.69</bcprov-jdk15on.version>
<guava.version>31.0.1-jre</guava.version> <guava.version>31.0.1-jre</guava.version>
<transmittable-thread-local.version>2.12.0</transmittable-thread-local.version>
<!-- Maven Plugin Versions --> <!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version> <maven-source-plugin.version>3.2.0</maven-source-plugin.version>
@ -234,6 +235,13 @@
<version>${byte-buddy.version}</version> <version>${byte-buddy.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>${transmittable-thread-local.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save