parent
c347054f40
commit
e30851b4d8
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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 com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* {@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
|
||||
* @date 2022/7/8 9:31 下午
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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 com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* {@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
|
||||
* @date 2022/7/8 9:16 下午
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Metadata Wrapper interface.
|
||||
* <p>
|
||||
* Used to mark wrapper types, for example:
|
||||
* <ul>
|
||||
* <li/ {@link MetadataCallable}>
|
||||
* <li/ {@link MetadataRunnable}>
|
||||
* </ul>
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 9:17 上午
|
||||
*/
|
||||
|
||||
public interface MetadataWrap<T> {
|
||||
|
||||
/**
|
||||
* unwrap to the original/underneath one.
|
||||
*
|
||||
* @return a unWrap instance
|
||||
*/
|
||||
T unWrap();
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
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;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* {@link MetadataContext} Wrapper of {@link Executor},
|
||||
* transfer the {@link MetadataContext} from the task submit time of {@link Runnable}
|
||||
* to the execution time of {@link Runnable}.
|
||||
*
|
||||
* @author wlx
|
||||
* * @date 2022/7/8 9:35 下午
|
||||
*/
|
||||
class MetadataExecutor implements Executor, MetadataWrap<Executor> {
|
||||
|
||||
private final Executor delegate;
|
||||
|
||||
public MetadataExecutor(Executor delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(@NonNull Runnable command) {
|
||||
delegate.execute(MetadataRunnable.get(command));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MetadataExecutor that = (MetadataExecutor) o;
|
||||
return delegate.equals(that.delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Executor unWrap() {
|
||||
return this.delegate;
|
||||
}
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
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 java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
/**
|
||||
* {@link MetadataContext} Wrapper of {@link ExecutorService},
|
||||
* transfer the {@link MetadataContext} from the task submit time of {@link Runnable} or {@link Callable}
|
||||
* to the execution time of {@link Runnable} or {@link Callable}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/8 9:36 下午
|
||||
*/
|
||||
class MetadataExecutorService extends MetadataExecutor implements ExecutorService {
|
||||
|
||||
private final ExecutorService delegate;
|
||||
|
||||
public MetadataExecutorService(ExecutorService delegate) {
|
||||
super(delegate);
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
this.delegate.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public List<Runnable> shutdownNow() {
|
||||
return this.delegate.shutdownNow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShutdown() {
|
||||
return this.delegate.isShutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTerminated() {
|
||||
return this.delegate.isTerminated();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean awaitTermination(long timeout, @NonNull TimeUnit unit) throws InterruptedException {
|
||||
return this.delegate.awaitTermination(timeout, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public <T> Future<T> submit(@NonNull Callable<T> task) {
|
||||
return this.delegate.submit(MetadataCallable.get(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public <T> Future<T> submit(@NonNull Runnable task, T result) {
|
||||
return this.delegate.submit(MetadataRunnable.get(task), result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Future<?> submit(@NonNull Runnable task) {
|
||||
return this.delegate.submit(MetadataRunnable.get(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public <T> List<Future<T>> invokeAll(@NonNull Collection<? extends Callable<T>> tasks) throws InterruptedException {
|
||||
return this.delegate.invokeAll(MetadataCallable.gets(tasks));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public <T> T invokeAny(@NonNull Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
|
||||
return this.delegate.invokeAny(MetadataCallable.gets(tasks));
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutorService unWrap() {
|
||||
return this.delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MetadataExecutorService that = (MetadataExecutorService) o;
|
||||
return delegate.equals(that.delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(delegate);
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
import com.tencent.cloud.metadata.concurrent.MetadataWrap;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
/**
|
||||
* Util methods for Metadata wrapper of jdk executors.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/8 11:58 下午
|
||||
*/
|
||||
public class MetadataExecutors {
|
||||
|
||||
/**
|
||||
* wrap Executor instance to MetadataExecutorService instance.
|
||||
*
|
||||
* @param executor executor
|
||||
* @return MetadataExecutorService instance
|
||||
*/
|
||||
public static Executor getMetadataExecutor(Executor executor) {
|
||||
if (null == executor || isMetadataWrap(executor)) {
|
||||
return executor;
|
||||
}
|
||||
return new MetadataExecutor(executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrap ExecutorService instance to MetadataExecutorService instance.
|
||||
*
|
||||
* @param executorService executorService
|
||||
* @return MetadataExecutorService instance
|
||||
*/
|
||||
public static ExecutorService getMetadataExecutorService(ExecutorService executorService) {
|
||||
if (null == executorService || isMetadataWrap(executorService)) {
|
||||
return executorService;
|
||||
}
|
||||
return new MetadataExecutorService(executorService);
|
||||
}
|
||||
|
||||
/**
|
||||
* wrap ScheduledExecutorService instance to MetadataScheduledExecutorService instance.
|
||||
*
|
||||
* @param scheduledExecutorService scheduledExecutorService
|
||||
* @return MetadataScheduledExecutorService instance
|
||||
*/
|
||||
public static ScheduledExecutorService getMetadataScheduledExecutorService(ScheduledExecutorService
|
||||
scheduledExecutorService) {
|
||||
if (null == scheduledExecutorService || isMetadataWrap(scheduledExecutorService)) {
|
||||
return scheduledExecutorService;
|
||||
}
|
||||
return new MetadataScheduledExecutorService(scheduledExecutorService);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* unwrap to the original/underneath one.
|
||||
*
|
||||
* @param executor input executor
|
||||
* @param <T> Executor type
|
||||
* @return original/underneath one instance.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Executor> T unwrap(@Nullable T executor) {
|
||||
if (!isMetadataWrap(executor)) {
|
||||
return executor;
|
||||
}
|
||||
return (T) ((MetadataExecutor) executor).unWrap();
|
||||
}
|
||||
|
||||
/**
|
||||
* check the executor is a MetadataExecutor wrapper or not.
|
||||
* <p>
|
||||
* if the parameter executor is MetadataExecutor wrapper, return {@code true}, otherwise {@code false}.
|
||||
* <p>
|
||||
* NOTE: if input executor is {@code null}, return {@code false}.
|
||||
*
|
||||
* @param executor input executor
|
||||
* @param <T> Executor type
|
||||
*/
|
||||
public static <T extends Executor> boolean isMetadataWrap(@Nullable T executor) {
|
||||
return executor instanceof MetadataWrap;
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
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 java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* {@link MetadataContext} Wrapper of {@link ScheduledExecutorService},
|
||||
* transfer the {@link MetadataContext} from the task submit time of {@link Runnable} or {@link Callable}
|
||||
* to the execution time of {@link Runnable} or {@link Callable}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/8 9:40 下午
|
||||
*/
|
||||
class MetadataScheduledExecutorService extends MetadataExecutorService
|
||||
implements ScheduledExecutorService {
|
||||
|
||||
private final ScheduledExecutorService delegate;
|
||||
|
||||
public MetadataScheduledExecutorService(ScheduledExecutorService delegate) {
|
||||
super(delegate);
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public ScheduledFuture<?> schedule(@NonNull Runnable command, long delay, @NonNull TimeUnit unit) {
|
||||
return this.delegate.schedule(MetadataRunnable.get(command), delay, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public <V> ScheduledFuture<V> schedule(@NonNull Callable<V> callable, long delay, @NonNull TimeUnit unit) {
|
||||
return this.delegate.schedule(MetadataCallable.get(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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService unWrap() {
|
||||
return this.delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MetadataScheduledExecutorService that = (MetadataScheduledExecutorService) o;
|
||||
return delegate.equals(that.delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(delegate);
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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 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 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 static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Test for {@link MetadataCallable}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 12:11 下午
|
||||
*/
|
||||
@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 {
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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 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 java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Test for {@link MetadataRunnable}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 3:28 下午
|
||||
*/
|
||||
@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 {
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author wlx
|
||||
* @date 2022/7/9 3:23 下午
|
||||
*/
|
||||
public class MetadataTestUtil {
|
||||
|
||||
public static void initMetadataContext() {
|
||||
Map<String, String> customMetadata = new HashMap<>();
|
||||
customMetadata.put("a", "1");
|
||||
customMetadata.put("b", "2");
|
||||
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
|
||||
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE, customMetadata);
|
||||
MetadataContextHolder.set(metadataContext);
|
||||
|
||||
customMetadata = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
|
||||
Assertions.assertThat(customMetadata.get("b")).isEqualTo("2");
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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 java.util.stream.Collectors;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Test for {@link MetadataExecutorService}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 4:04 下午
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
||||
classes = MetadataExecutorServiceTest.TestApplication.class,
|
||||
properties = {"spring.config.location = classpath:application-test.yml",
|
||||
"spring.main.web-application-type = servlet",
|
||||
"spring.cloud.gateway.enabled = false"})
|
||||
public class MetadataExecutorServiceTest {
|
||||
|
||||
private static final ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
|
||||
@Test
|
||||
public void submitTest() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
MetadataExecutorService metadataExecutorService = new MetadataExecutorService(executorService);
|
||||
Future<Map<String, String>> future = metadataExecutorService.submit(
|
||||
() -> {
|
||||
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
}
|
||||
);
|
||||
Map<String, String> res = future.get(200, TimeUnit.MILLISECONDS);
|
||||
Assertions.assertThat(res).isEqualTo(
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAnyTest() throws ExecutionException, InterruptedException {
|
||||
MetadataExecutorService metadataExecutorService = new MetadataExecutorService(executorService);
|
||||
Map<String, String> res = metadataExecutorService.invokeAny(getCallableList());
|
||||
Assertions.assertThat(res).isEqualTo(
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAllTest() throws InterruptedException {
|
||||
MetadataExecutorService metadataExecutorService = new MetadataExecutorService(executorService);
|
||||
List<Future<Map<String, String>>> futures = metadataExecutorService.invokeAll(getCallableList());
|
||||
List<Map<String, String>> resList = futures.stream().map(
|
||||
future -> {
|
||||
try {
|
||||
return future.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
).collect(Collectors.toList());
|
||||
|
||||
resList.forEach(
|
||||
res -> {
|
||||
Assertions.assertThat(res).isEqualTo(
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
}
|
||||
|
||||
private List<Callable<Map<String, String>>> getCallableList() {
|
||||
List<Callable<Map<String, String>>> callableList = new ArrayList<>();
|
||||
callableList.add(() -> {
|
||||
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
});
|
||||
callableList.add(() -> {
|
||||
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
});
|
||||
return callableList;
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.metadata.concurrent.MetadataTestUtil;
|
||||
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 java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Test for {@link MetadataExecutor}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 3:44 下午
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
||||
classes = MetadataExecutorTest.TestApplication.class,
|
||||
properties = {"spring.config.location = classpath:application-test.yml",
|
||||
"spring.main.web-application-type = servlet",
|
||||
"spring.cloud.gateway.enabled = false"})
|
||||
public class MetadataExecutorTest {
|
||||
|
||||
private static final Executor executorService = Executors.newFixedThreadPool(1);
|
||||
|
||||
@Test
|
||||
public void metadataExecutorTest() throws InterruptedException {
|
||||
MetadataExecutor metadataExecutor = new MetadataExecutor(executorService);
|
||||
Map<String, String> fragmentContextBeforeInit =
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
metadataExecutor.execute(() -> {
|
||||
Map<String, String> fragmentContext =
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext));
|
||||
});
|
||||
|
||||
// wait 200ms for metadataExecutor execute task
|
||||
TimeUnit.MILLISECONDS.sleep(200);
|
||||
|
||||
MetadataTestUtil.initMetadataContext();
|
||||
|
||||
Map<String, String> fragmentContextAfterInit =
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
metadataExecutor.execute(() -> {
|
||||
Map<String, String> fragmentContext =
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
Assertions.assertThat(fragmentContextAfterInit.equals(fragmentContext));
|
||||
});
|
||||
|
||||
// wait 200ms for metadataExecutor execute task
|
||||
TimeUnit.MILLISECONDS.sleep(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metadataExecutorUnWrap() {
|
||||
MetadataExecutor metadataExecutor = new MetadataExecutor(executorService);
|
||||
Executor executor = metadataExecutor.unWrap();
|
||||
Assertions.assertThat(executor).isEqualTo(executorService);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
if (executorService instanceof ExecutorService) {
|
||||
ExecutorService executorService = (ExecutorService) MetadataExecutorTest.executorService;
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
/**
|
||||
* Test for {@link MetadataExecutors}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 4:05 下午
|
||||
*/
|
||||
public class MetadataExecutorsTest {
|
||||
|
||||
@Test
|
||||
public void getMetadataExecutor() {
|
||||
Executor metadataExecutor = MetadataExecutors.getMetadataExecutor(Executors.newFixedThreadPool(1));
|
||||
Assertions.assertThat(metadataExecutor instanceof MetadataExecutor).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMetadataExecutorService() {
|
||||
ExecutorService metadataExecutorService =
|
||||
MetadataExecutors.getMetadataExecutorService(Executors.newFixedThreadPool(1));
|
||||
Assertions.assertThat(metadataExecutorService instanceof MetadataExecutorService).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMetadataScheduledExecutorService() {
|
||||
ScheduledExecutorService metadataScheduledExecutorService =
|
||||
MetadataExecutors.getMetadataScheduledExecutorService(Executors.newSingleThreadScheduledExecutor());
|
||||
Assertions.assertThat(metadataScheduledExecutorService instanceof MetadataScheduledExecutorService).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unWrap() {
|
||||
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
|
||||
ScheduledExecutorService unwrap = MetadataExecutors.unwrap(
|
||||
MetadataExecutors.getMetadataScheduledExecutorService(scheduledExecutorService));
|
||||
Assertions.assertThat(unwrap).isEqualTo(scheduledExecutorService);
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.executor;
|
||||
|
||||
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 java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Test for {@link MetadataScheduledExecutorService}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/9 4:04 下午
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
||||
classes = MetadataScheduledExecutorServiceTest.TestApplication.class,
|
||||
properties = {"spring.config.location = classpath:application-test.yml",
|
||||
"spring.main.web-application-type = servlet",
|
||||
"spring.cloud.gateway.enabled = false"})
|
||||
public class MetadataScheduledExecutorServiceTest {
|
||||
|
||||
private static final ScheduledExecutorService scheduledExecutorService =
|
||||
Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
@Test
|
||||
public void scheduleRunnableTest() throws ExecutionException, InterruptedException {
|
||||
Map<String, String> fragmentContext =
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
|
||||
ScheduledFuture<?> schedule = scheduledExecutorService.schedule(() -> {
|
||||
Assertions.assertThat(fragmentContext).
|
||||
isEqualTo(MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE));
|
||||
}, 100, TimeUnit.MILLISECONDS);
|
||||
schedule.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scheduleCallableTest() throws ExecutionException, InterruptedException {
|
||||
ScheduledFuture<Map<String, String>> schedule = scheduledExecutorService.schedule(() -> {
|
||||
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
}, 100, TimeUnit.MILLISECONDS);
|
||||
Map<String, String> res = schedule.get();
|
||||
Assertions.assertThat(res).isEqualTo(
|
||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
||||
);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
scheduledExecutorService.shutdownNow();
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
}
|
||||
}
|
Loading…
Reference in new issue