Fix eclipse/checkstyle warnings
[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.assertTrue;
13 import static org.junit.Assert.fail;
14
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;
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         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 = CountingRejectedExecutionHandler.newCallerRunsPolicy();
51         executor.setRejectedExecutionHandler(countingHandler);
52
53         executor.execute(new Task(tasksRunLatch, blockLatch));
54
55         int tasks = 5;
56         for (int i = 0; i < tasks - 1; i++) {
57             executor.execute(new Task(null, null, null, null, 0));
58         }
59
60         assertEquals("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
61
62         blockLatch.countDown();
63
64         assertTrue("Tasks complete", tasksRunLatch.await(5, TimeUnit.SECONDS));
65     }
66
67     @Test
68     public void testAbortPolicyHandler() throws InterruptedException {
69
70         CountDownLatch tasksRunLatch = new CountDownLatch(1);
71         CountDownLatch blockLatch = new CountDownLatch(1);
72
73         executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
74                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
75
76         CountingRejectedExecutionHandler countingHandler = CountingRejectedExecutionHandler.newAbortPolicy();
77         executor.setRejectedExecutionHandler(countingHandler);
78
79         executor.execute(new Task(tasksRunLatch, blockLatch));
80
81         int tasks = 5;
82         for (int i = 0; i < tasks - 1; i++) {
83             try {
84                 executor.execute(new Task(null, null, null, null, 0));
85                 fail("Expected RejectedExecutionException");
86             } catch (RejectedExecutionException e) {
87                 // Expected
88             }
89         }
90
91         assertEquals("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
92
93         blockLatch.countDown();
94
95         assertTrue("Tasks complete", tasksRunLatch.await(5, TimeUnit.SECONDS));
96     }
97 }