Migrate common/util to JUnit5
[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 package org.opendaylight.yangtools.util.concurrent;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
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.jupiter.api.AfterEach;
20 import org.junit.jupiter.api.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 class CountingRejectedExecutionHandlerTest {
30
31     private ThreadPoolExecutor executor;
32
33     @AfterEach
34     void tearDown() {
35         if (executor != null) {
36             executor.shutdownNow();
37         }
38     }
39
40     @Test
41     void testCallerRunsPolicyHandler() throws InterruptedException {
42
43         final var tasksRunLatch = new CountDownLatch(1);
44         final var blockLatch = new CountDownLatch(1);
45
46         executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
47                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
48
49         final var countingHandler = CountingRejectedExecutionHandler.newCallerRunsPolicy();
50         executor.setRejectedExecutionHandler(countingHandler);
51
52         executor.execute(new Task(tasksRunLatch, blockLatch));
53
54         final var tasks = 5;
55         for (int i = 0; i < tasks - 1; i++) {
56             executor.execute(new Task(null, null, null, null, 0));
57         }
58
59         assertEquals(tasks - 1, countingHandler.getRejectedTaskCount(), "getRejectedTaskCount");
60
61         blockLatch.countDown();
62
63         assertTrue(tasksRunLatch.await(5, TimeUnit.SECONDS), "Tasks complete");
64     }
65
66     @Test
67     void testAbortPolicyHandler() throws InterruptedException {
68
69         final var tasksRunLatch = new CountDownLatch(1);
70         final var blockLatch = new CountDownLatch(1);
71
72         executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
73                 ExecutorServiceUtil.offerFailingBlockingQueue(new LinkedBlockingQueue<>()));
74
75         final var countingHandler = CountingRejectedExecutionHandler.newAbortPolicy();
76         executor.setRejectedExecutionHandler(countingHandler);
77
78         executor.execute(new Task(tasksRunLatch, blockLatch));
79
80         final var tasks = 5;
81         for (int i = 0; i < tasks - 1; i++) {
82             assertThrows(RejectedExecutionException.class, () -> executor.execute(new Task(null, null, null, null, 0)));
83         }
84
85         assertEquals(tasks - 1, countingHandler.getRejectedTaskCount(), "getRejectedTaskCount");
86
87         blockLatch.countDown();
88
89         assertTrue(tasksRunLatch.await(5, TimeUnit.SECONDS), "Tasks complete");
90     }
91 }