2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util.concurrent;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.concurrent.RejectedExecutionException;
17 import java.util.concurrent.ThreadPoolExecutor;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.jupiter.api.AfterEach;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
22 import org.opendaylight.yangtools.util.concurrent.ThreadPoolExecutorTest.Task;
25 * Unit tests for CountingRejectedExecutionHandler.
27 * @author Thomas Pantelis
29 class CountingRejectedExecutionHandlerTest {
31 private ThreadPoolExecutor executor;
35 if (executor != null) {
36 executor.shutdownNow();
41 void testCallerRunsPolicyHandler() throws InterruptedException {
43 final var tasksRunLatch = new CountDownLatch(1);
44 final var blockLatch = new CountDownLatch(1);
46 executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
47 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
49 final var countingHandler = CountingRejectedExecutionHandler.newCallerRunsPolicy();
50 executor.setRejectedExecutionHandler(countingHandler);
52 executor.execute(new Task(tasksRunLatch, blockLatch));
55 for (int i = 0; i < tasks - 1; i++) {
56 executor.execute(new Task(null, null, null, null, 0));
59 assertEquals(tasks - 1, countingHandler.getRejectedTaskCount(), "getRejectedTaskCount");
61 blockLatch.countDown();
63 assertTrue(tasksRunLatch.await(5, TimeUnit.SECONDS), "Tasks complete");
67 void testAbortPolicyHandler() throws InterruptedException {
69 final var tasksRunLatch = new CountDownLatch(1);
70 final var blockLatch = new CountDownLatch(1);
72 executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
73 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
75 final var countingHandler = CountingRejectedExecutionHandler.newAbortPolicy();
76 executor.setRejectedExecutionHandler(countingHandler);
78 executor.execute(new Task(tasksRunLatch, blockLatch));
81 for (int i = 0; i < tasks - 1; i++) {
82 assertThrows(RejectedExecutionException.class, () -> executor.execute(new Task(null, null, null, null, 0)));
85 assertEquals(tasks - 1, countingHandler.getRejectedTaskCount(), "getRejectedTaskCount");
87 blockLatch.countDown();
89 assertTrue(tasksRunLatch.await(5, TimeUnit.SECONDS), "Tasks complete");