From 3a520d22d949012518833f84d02aae3928c4e8c5 Mon Sep 17 00:00:00 2001 From: rnmb <19276209+rnmb@users.noreply.github.com> Date: Fri, 26 May 2023 11:50:15 +0800 Subject: [PATCH] feat(infra): add SyncPutQueuePolicyTest test class #1336 1. add SyncPutQueuePolicyTest test class Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> --- .../support/SyncPutQueuePolicyTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java new file mode 100644 index 00000000..fba4941d --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java @@ -0,0 +1,34 @@ +package cn.hippo4j.common.executor.support; + +import cn.hippo4j.common.toolkit.ThreadUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.*; +import java.util.stream.IntStream; + +/** + * Synchronous placement queue policy implementation test + */ +public class SyncPutQueuePolicyTest { + + /** + * test thread pool rejected execution + */ + @Test + public void testRejectedExecution() throws InterruptedException { + SyncPutQueuePolicy syncPutQueuePolicy = new SyncPutQueuePolicy(); + ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, + 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), syncPutQueuePolicy); + threadPoolExecutor.prestartAllCoreThreads(); + + Assert.assertSame(syncPutQueuePolicy, threadPoolExecutor.getRejectedExecutionHandler()); + IntStream.range(0, 4).forEach(s -> { + threadPoolExecutor.execute(() -> ThreadUtil.sleep(500L)); + }); + threadPoolExecutor.shutdown(); + while (!threadPoolExecutor.isTerminated()) { + } + Assert.assertEquals(4, threadPoolExecutor.getCompletedTaskCount()); + } +}