6a86840b963460aaff7da0a60125dd03bb535f5b
[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 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;
23
24 /**
25  * Unit tests for CountingRejectedExecutionHandler.
26  *
27  * @author Thomas Pantelis
28  */
29 public class CountingRejectedExecutionHandlerTest {
30
31     private ThreadPoolExecutor executor;
32
33     @After
34     public void tearDown() {
35         if (executor != null) {
36             executor.shutdownNow();
37         }
38     }
39
40     @Test
41     public void testCallerRunsPolicyHandler() throws InterruptedException {
42
43         int nTasks = 5;
44         CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
45         CountDownLatch blockLatch = new CountDownLatch( 1 );
46
47         executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
48                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
49
50         CountingRejectedExecutionHandler countingHandler =
51                 CountingRejectedExecutionHandler.newCallerRunsPolicy();
52         executor.setRejectedExecutionHandler( countingHandler );
53
54         executor.execute( new Task( tasksRunLatch, blockLatch ) );
55
56         for (int i = 0; i < nTasks - 1; i++) {
57             executor.execute( new Task( null, null, null, null, 0 ) );
58         }
59
60         assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
61
62         blockLatch.countDown();
63
64         assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );
65     }
66
67     @Test
68     public void testAbortPolicyHandler() throws InterruptedException {
69
70         int nTasks = 5;
71         CountDownLatch tasksRunLatch = new CountDownLatch( 1 );
72         CountDownLatch blockLatch = new CountDownLatch( 1 );
73
74         executor = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS,
75                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>() ) );
76
77         CountingRejectedExecutionHandler countingHandler =
78                 CountingRejectedExecutionHandler.newAbortPolicy();
79         executor.setRejectedExecutionHandler( countingHandler );
80
81         executor.execute( new Task( tasksRunLatch, blockLatch ) );
82
83         for (int i = 0; i < nTasks - 1; i++) {
84             try {
85                 executor.execute( new Task( null, null, null, null, 0 ) );
86                 fail( "Expected RejectedExecutionException" );
87             } catch( RejectedExecutionException e ) {
88                 // Expected
89             }
90         }
91
92         assertEquals( "getRejectedTaskCount", nTasks - 1, countingHandler.getRejectedTaskCount() );
93
94         blockLatch.countDown();
95
96         assertEquals( "Tasks complete", true, tasksRunLatch.await( 5, TimeUnit.SECONDS ) );
97     }
98 }