Migrate common/util to JUnit5
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / concurrent / QueuedNotificationManagerTest.java
index 0d5cd77ff1688dfbce1d0dd2d311c44d8edb2418..3007b1efbd17183019fa048cec7dfd4a13bb6aad 100644 (file)
@@ -5,12 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.util.concurrent;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 import static org.mockito.Mockito.mock;
 
 import com.google.common.base.Stopwatch;
@@ -27,8 +26,9 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
-import org.junit.After;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.BatchedInvoker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory;
  *
  * @author Thomas Pantelis
  */
-public class QueuedNotificationManagerTest {
+class QueuedNotificationManagerTest {
 
     static class TestListener<N> {
 
@@ -58,7 +58,7 @@ public class QueuedNotificationManagerTest {
         }
 
         void reset(final int newExpCount) {
-            this.expCount = newExpCount;
+            expCount = newExpCount;
             latch = new CountDownLatch(newExpCount);
             actual.clear();
         }
@@ -74,13 +74,13 @@ public class QueuedNotificationManagerTest {
                     actual.addAll(data);
                 }
 
-                RuntimeException localRuntimeEx = runtimeEx;
+                final var localRuntimeEx = runtimeEx;
                 if (localRuntimeEx != null) {
                     runtimeEx = null;
                     throw localRuntimeEx;
                 }
 
-                Error localJvmError = jvmError;
+                final var localJvmError = jvmError;
                 if (localJvmError != null) {
                     jvmError = null;
                     throw localJvmError;
@@ -101,7 +101,7 @@ public class QueuedNotificationManagerTest {
 
         void verifyNotifications(final List<N> expected) {
             verifyNotifications();
-            assertEquals(name + ": Notifications", expected, actual);
+            assertEquals(expected, actual, name + ": Notifications");
         }
 
         // Implement bad hashCode/equals methods to verify it doesn't screw up the
@@ -113,7 +113,7 @@ public class QueuedNotificationManagerTest {
 
         @Override
         public boolean equals(final Object obj) {
-            TestListener<?> other = (TestListener<?>) obj;
+            final var other = (TestListener<?>) obj;
             return other != null;
         }
     }
@@ -140,23 +140,24 @@ public class QueuedNotificationManagerTest {
     private static final Logger LOG = LoggerFactory.getLogger(QueuedNotificationManagerTest.class);
     private ExecutorService queueExecutor;
 
-    @After
-    public void tearDown() {
+    @AfterEach
+    void tearDown() {
         if (queueExecutor != null) {
             queueExecutor.shutdownNow();
         }
     }
 
-    @Test(timeout = 10000)
-    public void testNotificationsWithSingleListener() {
+    @Test
+    @Timeout(10000)
+    void testNotificationsWithSingleListener() {
 
         queueExecutor = Executors.newFixedThreadPool(2);
-        NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(queueExecutor,
-                new TestNotifier<>(), 10, "TestMgr");
+        final NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(
+                queueExecutor, new TestNotifier<>(), 10, "TestMgr");
 
-        int count = 100;
+        final var count = 100;
 
-        TestListener<Integer> listener = new TestListener<>(count, 1);
+        final var listener = new TestListener<Integer>(count, 1);
         listener.sleepTime = 20;
 
         manager.submitNotifications(listener, Arrays.asList(1, 2));
@@ -172,11 +173,11 @@ public class QueuedNotificationManagerTest {
 
         listener.sleepTime = 0;
 
-        List<Integer> expNotifications = new ArrayList<>(count);
+        final var expNotifications = new ArrayList<Integer>(count);
         expNotifications.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
-        int initialCount = 6;
+        final var initialCount = 6;
         for (int i = 1; i <= count - initialCount; i++) {
-            Integer val = Integer.valueOf(initialCount + i);
+            Integer val = initialCount + i;
             expNotifications.add(val);
             manager.submitNotification(listener, val);
         }
@@ -185,35 +186,35 @@ public class QueuedNotificationManagerTest {
     }
 
     @Test
-    public void testNotificationsWithMultipleListeners() throws InterruptedException {
+    void testNotificationsWithMultipleListeners() throws InterruptedException {
 
-        int count = 10;
+        final var count = 10;
         queueExecutor = Executors.newFixedThreadPool(count);
-        final ExecutorService stagingExecutor = Executors.newFixedThreadPool(count);
+        final var stagingExecutor = Executors.newFixedThreadPool(count);
         final NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(
                 queueExecutor, new TestNotifier<>(), 5000, "TestMgr");
 
-        final int nNotifications = 100000;
+        final var nNotifications = 100000;
 
         LOG.info("Testing {} listeners with {} notifications each...", count, nNotifications);
 
-        final Integer[] notifications = new Integer[nNotifications];
+        final var notifications = new Integer[nNotifications];
         for (int i = 1; i <= nNotifications; i++) {
-            notifications[i - 1] = Integer.valueOf(i);
+            notifications[i - 1] = i;
         }
 
         Stopwatch stopWatch = Stopwatch.createStarted();
 
-        List<TestListener<Integer>> listeners = new ArrayList<>();
-        List<Thread> threads = new ArrayList<>();
+        final var listeners = new ArrayList<TestListener<Integer>>();
+        final var threads = new ArrayList<Thread>();
         for (int i = 1; i <= count; i++) {
-            final TestListener<Integer> listener =
-                    i == 2 ? new TestListener2<>(nNotifications, i) :
-                    i == 3 ? new TestListener3<>(nNotifications, i) :
-                            new TestListener<>(nNotifications, i);
+            final var listener =
+                    i == 2 ? new TestListener2<Integer>(nNotifications, i) :
+                    i == 3 ? new TestListener3<Integer>(nNotifications, i) :
+                            new TestListener<Integer>(nNotifications, i);
             listeners.add(listener);
 
-            final Thread t = new Thread(() -> {
+            final var t = new Thread(() -> {
                 for (int j = 1; j <= nNotifications; j++) {
                     final Integer n = notifications[j - 1];
                     stagingExecutor.execute(() -> manager.submitNotification(listener, n));
@@ -226,7 +227,7 @@ public class QueuedNotificationManagerTest {
         }
 
         try {
-            for (TestListener<Integer> listener: listeners) {
+            for (final var listener: listeners) {
                 listener.verifyNotifications();
                 LOG.info("{} succeeded", listener.name);
             }
@@ -244,29 +245,31 @@ public class QueuedNotificationManagerTest {
         }
     }
 
-    @Test(timeout = 10000)
-    public void testNotificationsWithListenerRuntimeEx() {
+    @Test
+    @Timeout(10000)
+    void testNotificationsWithListenerRuntimeEx() {
 
         queueExecutor = Executors.newFixedThreadPool(1);
-        NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(queueExecutor,
-            new TestNotifier<>(), 10, "TestMgr");
+        final NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(
+                queueExecutor, new TestNotifier<>(), 10, "TestMgr");
 
-        TestListener<Integer> listener = new TestListener<>(2, 1);
-        final RuntimeException mockedRuntimeException = new RuntimeException("mock");
+        final var listener = new TestListener<Integer>(2, 1);
+        final var mockedRuntimeException = new RuntimeException("mock");
         listener.runtimeEx = mockedRuntimeException;
 
         manager.submitNotification(listener, 1);
         manager.submitNotification(listener, 2);
 
         listener.verifyNotifications();
-        List<Runnable> tasks = queueExecutor.shutdownNow();
+        final var tasks = queueExecutor.shutdownNow();
         assertTrue(tasks.isEmpty());
     }
 
-    @Test(timeout = 10000)
-    public void testNotificationsWithListenerJVMError() {
+    @Test
+    @Timeout(10000)
+    void testNotificationsWithListenerJVMError() {
 
-        final CountDownLatch errorCaughtLatch = new CountDownLatch(1);
+        final var errorCaughtLatch = new CountDownLatch(1);
         queueExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>()) {
             @Override
             @SuppressWarnings("checkstyle:illegalCatch")
@@ -281,20 +284,20 @@ public class QueuedNotificationManagerTest {
             }
         };
 
-        NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(queueExecutor,
-                new TestNotifier<>(), 10, "TestMgr");
+        final NotificationManager<TestListener<Integer>, Integer> manager = QueuedNotificationManager.create(
+                queueExecutor, new TestNotifier<>(), 10, "TestMgr");
 
-        TestListener<Integer> listener = new TestListener<>(2, 1);
+        final var listener = new TestListener<Integer>(2, 1);
         listener.jvmError = mock(Error.class);
 
         manager.submitNotification(listener, 1);
 
-        assertTrue("JVM Error caught", Uninterruptibles.awaitUninterruptibly(errorCaughtLatch, 5, TimeUnit.SECONDS));
+        assertTrue(Uninterruptibles.awaitUninterruptibly(errorCaughtLatch, 5, TimeUnit.SECONDS), "JVM Error caught");
 
         manager.submitNotification(listener, 2);
 
         listener.verifyNotifications();
-        List<Runnable> tasks = queueExecutor.shutdownNow();
+        final var tasks = queueExecutor.shutdownNow();
         assertTrue(tasks.isEmpty());
     }
 }