*/
package org.opendaylight.yangtools.util.concurrent;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.ArrayList;
-import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
-import org.junit.After;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker;
/**
*
* @author Thomas Pantelis
*/
-public class AsyncNotifyingListeningExecutorServiceTest {
+class AsyncNotifyingListeningExecutorServiceTest {
private ExecutorService listenerExecutor;
private AsyncNotifyingListeningExecutorService testExecutor;
- @After
- public void tearDown() {
+ @AfterEach
+ void tearDown() {
if (listenerExecutor != null) {
listenerExecutor.shutdownNow();
}
}
@Test
- public void testListenerCallbackWithExecutor() throws InterruptedException {
+ void testListenerCallbackWithExecutor() throws InterruptedException {
- String listenerThreadPrefix = "ListenerThread";
+ final var listenerThreadPrefix = "ListenerThread";
listenerExecutor = Executors.newFixedThreadPool(3,
new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix + "-%d").build());
}
@Test
- public void testListenerCallbackWithNoExecutor() throws InterruptedException {
+ void testListenerCallbackWithNoExecutor() throws InterruptedException {
- String listenerThreadPrefix = "SingleThread";
+ final var listenerThreadPrefix = "SingleThread";
testExecutor = new AsyncNotifyingListeningExecutorService(
Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix).build()),
static void testListenerCallback(final AsyncNotifyingListeningExecutorService executor,
final Invoker invoker, final String expListenerThreadPrefix) throws InterruptedException {
- AtomicReference<AssertionError> assertError = new AtomicReference<>();
- CountDownLatch futureNotifiedLatch = new CountDownLatch(1);
- CountDownLatch blockTaskLatch = new CountDownLatch(1);
+ final var assertError = new AtomicReference<AssertionError>();
+ var futureNotifiedLatch = new CountDownLatch(1);
+ final var blockTaskLatch = new CountDownLatch(1);
// The blockTaskLatch is used to block the task from completing until we've added
// our listener to the Future. Otherwise, if the task completes quickly and the Future is
// will immediately notify synchronously on this thread as Futures#addCallback defaults to
// a same thread executor. This would erroneously fail the test.
- ListenableFuture<?> future = invoker.invokeExecutor(executor, blockTaskLatch);
+ final var future = invoker.invokeExecutor(executor, blockTaskLatch);
addCallback(future, futureNotifiedLatch, expListenerThreadPrefix, assertError);
// Now that we've added our listener, signal the latch to let the task complete.
blockTaskLatch.countDown();
- assertTrue("ListenableFuture callback was not notified of onSuccess",
- futureNotifiedLatch.await(5, TimeUnit.SECONDS));
+ assertTrue(futureNotifiedLatch.await(5, TimeUnit.SECONDS),
+ "ListenableFuture callback was not notified of onSuccess");
if (assertError.get() != null) {
throw assertError.get();
futureNotifiedLatch = new CountDownLatch(1);
addCallback(future, futureNotifiedLatch, Thread.currentThread().getName(), assertError);
- assertTrue("ListenableFuture callback was not notified of onSuccess",
- futureNotifiedLatch.await(5, TimeUnit.SECONDS));
+ assertTrue(futureNotifiedLatch.await(5, TimeUnit.SECONDS),
+ "ListenableFuture callback was not notified of onSuccess");
if (assertError.get() != null) {
throw assertError.get();
@Override
public void onSuccess(final Object result) {
try {
- String theadName = Thread.currentThread().getName();
- assertTrue("ListenableFuture callback was not notified on the listener executor."
+ final var theadName = Thread.currentThread().getName();
+ assertTrue(theadName.startsWith(expListenerThreadPrefix),
+ "ListenableFuture callback was not notified on the listener executor."
+ " Expected thread name prefix \"" + expListenerThreadPrefix
- + "\". Actual thread name \"" + theadName + "\"",
- theadName.startsWith(expListenerThreadPrefix));
+ + "\". Actual thread name \"" + theadName + "\"");
} catch (AssertionError e) {
assertError.set(e);
} finally {
@Override
@SuppressWarnings("checkstyle:parameterName")
- public void onFailure(final Throwable t) {
+ public void onFailure(final Throwable cause) {
// Shouldn't happen
- fail("Unexpected failure " + t);
+ fail("Unexpected failure " + cause);
}
}, MoreExecutors.directExecutor());
}
@Test
- public void testDelegatedMethods() throws InterruptedException {
+ void testDelegatedMethods() throws InterruptedException {
- Runnable task = () -> { };
+ final var task = (Runnable) () -> { };
- List<Runnable> taskList = new ArrayList<>();
+ final var taskList = new ArrayList<Runnable>();
- ExecutorService mockDelegate = mock(ExecutorService.class);
+ final var mockDelegate = mock(ExecutorService.class);
doNothing().when(mockDelegate).execute(task);
doNothing().when(mockDelegate).shutdown();
doReturn(taskList).when(mockDelegate).shutdownNow();
doReturn(Boolean.TRUE).when(mockDelegate).isShutdown();
doReturn(Boolean.TRUE).when(mockDelegate).isTerminated();
- AsyncNotifyingListeningExecutorService executor = new AsyncNotifyingListeningExecutorService(
+ final var executor = new AsyncNotifyingListeningExecutorService(
mockDelegate, null);
executor.execute(task);
executor.shutdown();
- assertTrue("awaitTermination", executor.awaitTermination(3, TimeUnit.SECONDS));
- assertSame("shutdownNow", taskList, executor.shutdownNow());
- assertTrue("isShutdown", executor.isShutdown());
- assertTrue("isTerminated", executor.isTerminated());
+ assertTrue(executor.awaitTermination(3, TimeUnit.SECONDS), "awaitTermination");
+ assertSame(taskList, executor.shutdownNow(), "shutdownNow");
+ assertTrue(executor.isShutdown(), "isShutdown");
+ assertTrue(executor.isTerminated(), "isTerminated");
verify(mockDelegate).execute(task);
verify(mockDelegate).shutdown();