From 0e7e6ec6e76dca2187d5531eb68fef58f44a5cf0 Mon Sep 17 00:00:00 2001 From: Annibale Ippolito <101899632+annippolito@users.noreply.github.com> Date: Fri, 26 May 2023 11:46:02 +0200 Subject: [PATCH] test: Add test case for RunsOldestTaskPolicy #1335 (#1347) * test: Add test case for RunsOldestTaskPolicy #1335 * make test viewable by codecov #1335 --- .../support/RunsOldestTaskPolicyTest.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java new file mode 100644 index 00000000..0b59807a --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java @@ -0,0 +1,100 @@ +/* + * 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 java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class RunsOldestTaskPolicyTest { + + private final RunsOldestTaskPolicy runsOldestTaskPolicy = new RunsOldestTaskPolicy(); + + @Mock + private Runnable runnable; + + @Mock + private Runnable runnableInTheQueue; + + @Mock + private ThreadPoolExecutor threadPoolExecutor; + + @Mock + private BlockingQueue workQueue; + + @Test + public void testRejectedExecutionWhenExecutorIsShutDown() { + when(threadPoolExecutor.isShutdown()).thenReturn(true); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(threadPoolExecutor, never()).execute(runnable); + verify(runnable, never()).run(); + } + + @Test + public void testRejectedExecutionWhenATaskIsInTheQueueTheExecutorShouldNotExecute() { + when(threadPoolExecutor.isShutdown()).thenReturn(false); + when(threadPoolExecutor.getQueue()).thenReturn(workQueue); + when(workQueue.poll()).thenReturn(runnableInTheQueue); + when(workQueue.offer(runnable)).thenReturn(true); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(runnableInTheQueue).run(); + verify(threadPoolExecutor, never()).execute(runnable); + verify(runnable, never()).run(); + } + + @Test + public void testRejectedExecutionWhenATaskIsInTheQueueTheExecutorShouldExecute() { + when(threadPoolExecutor.isShutdown()).thenReturn(false); + when(threadPoolExecutor.getQueue()).thenReturn(workQueue); + when(workQueue.poll()).thenReturn(runnableInTheQueue); + when(workQueue.offer(runnable)).thenReturn(false); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(runnableInTheQueue).run(); + verify(threadPoolExecutor).execute(runnable); + verify(runnable, never()).run(); + } + + @Test + public void testRejectedExecutionWhenATaskIsInTheQueueAndThePollReturnANullValue() { + when(threadPoolExecutor.isShutdown()).thenReturn(false); + when(threadPoolExecutor.getQueue()).thenReturn(workQueue); + when(workQueue.poll()).thenReturn(null); + when(workQueue.offer(runnable)).thenReturn(false); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(runnableInTheQueue, never()).run(); + verify(threadPoolExecutor).execute(runnable); + verify(runnable, never()).run(); + } +} \ No newline at end of file