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;
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 {
45 CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
46 CountDownLatch blockLatch = new CountDownLatch( 1 );
48 executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
49 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
51 CountingRejectedExecutionHandler countingHandler =
52 CountingRejectedExecutionHandler.newCallerRunsPolicy();
53 executor.setRejectedExecutionHandler( countingHandler );
55 executor.execute( new Task( tasksRunLatch, blockLatch ) );
57 for (int i = 0; i < nTasks - 1; i++) {
58 executor.execute( new Task( null, null, null, null, 0 ) );
61 assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
63 blockLatch.countDown();
65 assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );
69 public void testAbortPolicyHandler() throws InterruptedException {
72 CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
73 CountDownLatch blockLatch = new CountDownLatch( 1 );
75 executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
76 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
78 CountingRejectedExecutionHandler countingHandler =
79 CountingRejectedExecutionHandler.newAbortPolicy();
80 executor.setRejectedExecutionHandler( countingHandler );
82 executor.execute( new Task( tasksRunLatch, blockLatch ) );
84 for (int i = 0; i < nTasks - 1; i++) {
86 executor.execute( new Task( null, null, null, null, 0 ) );
87 fail( "Expected RejectedExecutionException" );
88 } catch( RejectedExecutionException e ) {
93 assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
95 blockLatch.countDown();
97 assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );