From ec142d7cc35dd6f0f3e58f65dacaf29de19e6d92 Mon Sep 17 00:00:00 2001 From: rnmb <19276209+rnmb@users.noreply.github.com> Date: Fri, 26 May 2023 15:06:05 +0800 Subject: [PATCH] feat(infra): add SyncPutQueuePolicyTest test class #1336 (#1344) * feat(infra): add SyncPutQueuePolicyTest test class #1336 1. add SyncPutQueuePolicyTest test class Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> * feat(infra): SyncPutQueuePolicyTest #1336 1. Add the open source agreement to the top, and leave a blank line with package Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> --- .../support/SyncPutQueuePolicyTest.java | 53 +++++++++++++++++++ 1 file changed, 53 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..c2f0a359 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.executor.support; + +import cn.hippo4j.common.toolkit.ThreadUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +/** + * Synchronous placement queue policy implementation test + */ +public class SyncPutQueuePolicyTest { + + /** + * test thread pool rejected execution + */ + @Test + public void testRejectedExecution() { + 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(200L)); + }); + threadPoolExecutor.shutdown(); + while (!threadPoolExecutor.isTerminated()) { + } + Assert.assertEquals(4, threadPoolExecutor.getCompletedTaskCount()); + } +}