* 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;
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;
*
* @author Thomas Pantelis
*/
-public class QueuedNotificationManagerTest {
+class QueuedNotificationManagerTest {
static class TestListener<N> {
}
void reset(final int newExpCount) {
- this.expCount = newExpCount;
+ expCount = newExpCount;
latch = new CountDownLatch(newExpCount);
actual.clear();
}
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;
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
@Override
public boolean equals(final Object obj) {
- TestListener<?> other = (TestListener<?>) obj;
+ final var other = (TestListener<?>) obj;
return other != null;
}
}
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));
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);
}
}
@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));
}
try {
- for (TestListener<Integer> listener: listeners) {
+ for (final var listener: listeners) {
listener.verifyNotifications();
LOG.info("{} succeeded", listener.name);
}
}
}
- @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")
}
};
- 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());
}
}