7abc688cffda734c352e09e8e9bfeaee55febcfb
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / concurrent / CountingRejectedExecutionHandlerTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
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
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;
24
25 /**
26  * Unit tests for CountingRejectedExecutionHandler.
27  *
28  * @author Thomas Pantelis
29  */
30 public class CountingRejectedExecutionHandlerTest {
31
32     private ThreadPoolExecutor executor;
33
34     @After
35     public void tearDown() {
36         if (executor != null) {
37             executor.shutdownNow();
38         }
39     }
40
41     @Test
42     public void testCallerRunsPolicyHandler() throws InterruptedException {
43
44         int nTasks = 5;
45         CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
46         CountDownLatch blockLatch = new CountDownLatch( 1 );
47
48         executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
49                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
50
51         CountingRejectedExecutionHandler countingHandler =
52                 CountingRejectedExecutionHandler.newCallerRunsPolicy();
53         executor.setRejectedExecutionHandler( countingHandler );
54
55         executor.execute( new Task( tasksRunLatch, blockLatch ) );
56
57         for (int i = 0; i < nTasks - 1; i++) {
58             executor.execute( new Task( null, null, null, null, 0 ) );
59         }
60
61         assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
62
63         blockLatch.countDown();
64
65         assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );
66     }
67
68     @Test
69     public void testAbortPolicyHandler() throws InterruptedException {
70
71         int nTasks = 5;
72         CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
73         CountDownLatch blockLatch = new CountDownLatch( 1 );
74
75         executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
76                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
77
78         CountingRejectedExecutionHandler countingHandler =
79                 CountingRejectedExecutionHandler.newAbortPolicy();
80         executor.setRejectedExecutionHandler( countingHandler );
81
82         executor.execute( new Task( tasksRunLatch, blockLatch ) );
83
84         for (int i = 0; i < nTasks - 1; i++) {
85             try {
86                 executor.execute( new Task( null, null, null, null, 0 ) );
87                 fail( "Expected RejectedExecutionException" );
88             } catch( RejectedExecutionException e ) {
89                 // Expected
90             }
91         }
92
93         assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
94
95         blockLatch.countDown();
96
97         assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );
98     }
99 }