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.fail;
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 {
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 =
51 CountingRejectedExecutionHandler.newCallerRunsPolicy();
52 executor.setRejectedExecutionHandler( countingHandler );
54 executor.execute( new Task( tasksRunLatch, blockLatch ) );
56 for (int i = 0; i < nTasks - 1; i++) {
57 executor.execute( new Task( null, null, null, null, 0 ) );
60 assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
62 blockLatch.countDown();
64 assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );
68 public void testAbortPolicyHandler() throws InterruptedException {
71 CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
72 CountDownLatch blockLatch = new CountDownLatch( 1 );
74 executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
75 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
77 CountingRejectedExecutionHandler countingHandler =
78 CountingRejectedExecutionHandler.newAbortPolicy();
79 executor.setRejectedExecutionHandler( countingHandler );
81 executor.execute( new Task( tasksRunLatch, blockLatch ) );
83 for (int i = 0; i < nTasks - 1; i++) {
85 executor.execute( new Task( null, null, null, null, 0 ) );
86 fail( "Expected RejectedExecutionException" );
87 } catch( RejectedExecutionException e ) {
92 assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
94 blockLatch.countDown();
96 assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );