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
9 package org.opendaylight.yangtools.util.concurrent;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.LinkedBlockingQueue;
17 import java.util.concurrent.RejectedExecutionException;
18 import java.util.concurrent.ThreadPoolExecutor;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.After;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
23 import org.opendaylight.yangtools.util.concurrent.ThreadPoolExecutorTest.Task;
26 * Unit tests for CountingRejectedExecutionHandler.
28 * @author Thomas Pantelis
30 public class CountingRejectedExecutionHandlerTest {
32 private ThreadPoolExecutor executor;
35 public void tearDown() {
36 if (executor != null) {
37 executor.shutdownNow();
42 public void testCallerRunsPolicyHandler() throws InterruptedException {
44 CountDownLatch tasksRunLatch = new CountDownLatch(1);
45 CountDownLatch blockLatch = new CountDownLatch(1);
47 executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
48 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
50 CountingRejectedExecutionHandler countingHandler = CountingRejectedExecutionHandler.newCallerRunsPolicy();
51 executor.setRejectedExecutionHandler(countingHandler);
53 executor.execute(new Task(tasksRunLatch, blockLatch));
56 for (int i = 0; i < tasks - 1; i++) {
57 executor.execute(new Task(null, null, null, null, 0));
60 assertEquals("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
62 blockLatch.countDown();
64 assertTrue("Tasks complete", tasksRunLatch.await(5, TimeUnit.SECONDS));
68 public void testAbortPolicyHandler() throws InterruptedException {
70 CountDownLatch tasksRunLatch = new CountDownLatch(1);
71 CountDownLatch blockLatch = new CountDownLatch(1);
73 executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
74 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
76 CountingRejectedExecutionHandler countingHandler = CountingRejectedExecutionHandler.newAbortPolicy();
77 executor.setRejectedExecutionHandler(countingHandler);
79 executor.execute(new Task(tasksRunLatch, blockLatch));
82 for (int i = 0; i < tasks - 1; i++) {
84 executor.execute(new Task(null, null, null, null, 0));
85 fail("Expected RejectedExecutionException");
86 } catch (RejectedExecutionException e) {
91 assertEquals("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
93 blockLatch.countDown();
95 assertTrue("Tasks complete", tasksRunLatch.await(5, TimeUnit.SECONDS));