2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util.concurrent;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.yangtools.util.concurrent.AsyncNotifyingListeningExecutorServiceTest.testListenerCallback;
14 import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_CALLABLE;
15 import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_RUNNABLE;
16 import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_RUNNABLE_WITH_RESULT;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.ListeningExecutorService;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import com.google.common.util.concurrent.ThreadFactoryBuilder;
24 import java.io.Serial;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.Executor;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicReference;
32 import java.util.function.Supplier;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker;
39 * Unit tests for DeadlockDetectingListeningExecutorService.
41 * @author Thomas Pantelis
43 public class DeadlockDetectingListeningExecutorServiceTest {
45 interface InitialInvoker {
46 void invokeExecutor(ListeningExecutorService executor, Runnable task);
49 static final InitialInvoker SUBMIT = ListeningExecutorService::submit;
51 static final InitialInvoker EXECUTE = Executor::execute;
53 public static class TestDeadlockException extends Exception {
55 private static final long serialVersionUID = 1L;
58 private static final Supplier<Exception> DEADLOCK_EXECUTOR_SUPPLIER = TestDeadlockException::new;
60 DeadlockDetectingListeningExecutorService executor;
67 public void tearDown() {
68 if (executor != null) {
69 executor.shutdownNow();
73 DeadlockDetectingListeningExecutorService newExecutor() {
74 return new DeadlockDetectingListeningExecutorService(Executors.newSingleThreadExecutor(),
75 DEADLOCK_EXECUTOR_SUPPLIER);
79 public void testBlockingSubmitOffExecutor() throws Exception {
81 executor = newExecutor();
83 // Test submit with Callable.
85 ListenableFuture<String> future = executor.submit(() -> "foo");
87 assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS));
89 // Test submit with Runnable.
91 executor.submit(() -> { }).get();
93 // Test submit with Runnable and value.
95 future = executor.submit(() -> { }, "foo");
97 assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS));
101 @SuppressWarnings("checkstyle:illegalThrows")
102 public void testNonBlockingSubmitOnExecutorThread() throws Throwable {
104 executor = newExecutor();
106 testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_CALLABLE);
107 testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE);
108 testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT);
110 testNonBlockingSubmitOnExecutorThread(EXECUTE, SUBMIT_CALLABLE);
113 @SuppressWarnings("checkstyle:illegalThrows")
114 void testNonBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker)
117 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
118 final CountDownLatch futureCompletedLatch = new CountDownLatch(1);
120 Runnable task = () -> Futures.addCallback(invoker.invokeExecutor(executor, null), new FutureCallback<Object>() {
122 public void onSuccess(final Object result) {
123 futureCompletedLatch.countDown();
127 @SuppressWarnings("checkstyle:parameterName")
128 public void onFailure(final Throwable t) {
130 futureCompletedLatch.countDown();
132 }, MoreExecutors.directExecutor());
134 initialInvoker.invokeExecutor(executor, task);
136 assertTrue("Task did not complete - executor likely deadlocked",
137 futureCompletedLatch.await(5, TimeUnit.SECONDS));
139 if (caughtEx.get() != null) {
140 throw caughtEx.get();
145 public void testBlockingSubmitOnExecutorThread() throws InterruptedException {
147 executor = newExecutor();
149 testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_CALLABLE);
150 testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE);
151 testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT);
153 testBlockingSubmitOnExecutorThread(EXECUTE, SUBMIT_CALLABLE);
156 @SuppressWarnings("checkstyle:illegalCatch")
157 void testBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker)
158 throws InterruptedException {
160 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
161 final CountDownLatch latch = new CountDownLatch(1);
163 Runnable task = () -> {
166 invoker.invokeExecutor(executor, null).get();
167 } catch (ExecutionException e) {
168 caughtEx.set(e.getCause());
169 } catch (Throwable e) {
176 initialInvoker.invokeExecutor(executor, task);
178 assertTrue("Task did not complete - executor likely deadlocked", latch.await(5, TimeUnit.SECONDS));
179 assertNotNull("Expected exception thrown", caughtEx.get());
180 assertEquals("Caught exception type", TestDeadlockException.class, caughtEx.get().getClass());
184 public void testListenableFutureCallbackWithExecutor() throws InterruptedException {
186 String listenerThreadPrefix = "ListenerThread";
187 ExecutorService listenerExecutor = Executors.newFixedThreadPool(1,
188 new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix + "-%d").build());
190 executor = new DeadlockDetectingListeningExecutorService(
191 Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("SingleThread").build()),
192 DEADLOCK_EXECUTOR_SUPPLIER, listenerExecutor);
195 testListenerCallback(executor, SUBMIT_CALLABLE, listenerThreadPrefix);
196 testListenerCallback(executor, SUBMIT_RUNNABLE, listenerThreadPrefix);
197 testListenerCallback(executor, SUBMIT_RUNNABLE_WITH_RESULT, listenerThreadPrefix);
199 listenerExecutor.shutdownNow();