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.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.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.After;
20 import org.junit.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 public class CountingRejectedExecutionHandlerTest {
31 private ThreadPoolExecutor executor;
34 public void tearDown() {
35 if (executor != null) {
36 executor.shutdownNow();
41 public void testCallerRunsPolicyHandler() throws InterruptedException {
43 CountDownLatch tasksRunLatch = new CountDownLatch(1);
44 CountDownLatch blockLatch = new CountDownLatch(1);
46 executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
47 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
49 CountingRejectedExecutionHandler 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("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
61 blockLatch.countDown();
63 assertTrue("Tasks complete", tasksRunLatch.await(5, TimeUnit.SECONDS));
67 public void testAbortPolicyHandler() throws InterruptedException {
69 CountDownLatch tasksRunLatch = new CountDownLatch(1);
70 CountDownLatch blockLatch = new CountDownLatch(1);
72 executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
73 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
75 CountingRejectedExecutionHandler 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("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
87 blockLatch.countDown();
89 assertTrue("Tasks complete", tasksRunLatch.await(5, TimeUnit.SECONDS));