*/
package org.opendaylight.yangtools.util.concurrent;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opendaylight.yangtools.util.concurrent.AsyncNotifyingListeningExecutorServiceTest.testListenerCallback;
import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_CALLABLE;
import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_RUNNABLE;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import java.io.Serial;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
-import org.junit.After;
-import org.junit.Before;
-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 DeadlockDetectingListeningExecutorServiceTest {
+class DeadlockDetectingListeningExecutorServiceTest {
interface InitialInvoker {
void invokeExecutor(ListeningExecutorService executor, Runnable task);
static final InitialInvoker EXECUTE = Executor::execute;
public static class TestDeadlockException extends Exception {
- @Serial
+ @java.io.Serial
private static final long serialVersionUID = 1L;
}
DeadlockDetectingListeningExecutorService executor;
- @Before
- public void setup() {
- }
-
- @After
- public void tearDown() {
+ @AfterEach
+ void tearDown() {
if (executor != null) {
executor.shutdownNow();
}
}
@Test
- public void testBlockingSubmitOffExecutor() throws Exception {
+ void testBlockingSubmitOffExecutor() throws Exception {
executor = newExecutor();
// Test submit with Callable.
- ListenableFuture<String> future = executor.submit(() -> "foo");
+ var future = executor.submit(() -> "foo");
- assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS));
+ assertEquals("foo", future.get(5, TimeUnit.SECONDS), "Future result");
// Test submit with Runnable.
future = executor.submit(() -> { }, "foo");
- assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS));
+ assertEquals("foo", future.get(5, TimeUnit.SECONDS), "Future result");
}
@Test
@SuppressWarnings("checkstyle:illegalThrows")
- public void testNonBlockingSubmitOnExecutorThread() throws Throwable {
+ void testNonBlockingSubmitOnExecutorThread() throws Throwable {
executor = newExecutor();
void testNonBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker)
throws Throwable {
- final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
- final CountDownLatch futureCompletedLatch = new CountDownLatch(1);
+ final var caughtEx = new AtomicReference<Throwable>();
+ final var futureCompletedLatch = new CountDownLatch(1);
- Runnable task = () -> Futures.addCallback(invoker.invokeExecutor(executor, null), new FutureCallback<Object>() {
- @Override
- public void onSuccess(final Object result) {
- futureCompletedLatch.countDown();
- }
+ final var task = (Runnable) () -> Futures.addCallback(invoker.invokeExecutor(executor, null),
+ new FutureCallback<Object>() {
+ @Override
+ public void onSuccess(final Object result) {
+ futureCompletedLatch.countDown();
+ }
- @Override
- @SuppressWarnings("checkstyle:parameterName")
- public void onFailure(final Throwable t) {
- caughtEx.set(t);
- futureCompletedLatch.countDown();
- }
- }, MoreExecutors.directExecutor());
+ @Override
+ @SuppressWarnings("checkstyle:parameterName")
+ public void onFailure(final Throwable t) {
+ caughtEx.set(t);
+ futureCompletedLatch.countDown();
+ }
+ }, MoreExecutors.directExecutor());
initialInvoker.invokeExecutor(executor, task);
- assertTrue("Task did not complete - executor likely deadlocked",
- futureCompletedLatch.await(5, TimeUnit.SECONDS));
+ assertTrue(futureCompletedLatch.await(5, TimeUnit.SECONDS),
+ "Task did not complete - executor likely deadlocked");
if (caughtEx.get() != null) {
throw caughtEx.get();
}
@Test
- public void testBlockingSubmitOnExecutorThread() throws InterruptedException {
+ void testBlockingSubmitOnExecutorThread() throws InterruptedException {
executor = newExecutor();
void testBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker)
throws InterruptedException {
- final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
- final CountDownLatch latch = new CountDownLatch(1);
+ final var caughtEx = new AtomicReference<Throwable>();
+ final var latch = new CountDownLatch(1);
- Runnable task = () -> {
+ final var task = (Runnable) () -> {
try {
invoker.invokeExecutor(executor, null).get();
initialInvoker.invokeExecutor(executor, task);
- assertTrue("Task did not complete - executor likely deadlocked", latch.await(5, TimeUnit.SECONDS));
- assertNotNull("Expected exception thrown", caughtEx.get());
- assertEquals("Caught exception type", TestDeadlockException.class, caughtEx.get().getClass());
+ assertTrue(latch.await(5, TimeUnit.SECONDS), "Task did not complete - executor likely deadlocked");
+ assertNotNull(caughtEx.get(), "Expected exception thrown");
+ assertEquals(TestDeadlockException.class, caughtEx.get().getClass(), "Caught exception type");
}
@Test
- public void testListenableFutureCallbackWithExecutor() throws InterruptedException {
+ void testListenableFutureCallbackWithExecutor() throws InterruptedException {
- String listenerThreadPrefix = "ListenerThread";
+ final var listenerThreadPrefix = "ListenerThread";
ExecutorService listenerExecutor = Executors.newFixedThreadPool(1,
new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix + "-%d").build());