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.util.concurrent.CountDownLatch;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Executor;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicReference;
31 import java.util.function.Supplier;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker;
38 * Unit tests for DeadlockDetectingListeningExecutorService.
40 * @author Thomas Pantelis
42 public class DeadlockDetectingListeningExecutorServiceTest {
44 interface InitialInvoker {
45 void invokeExecutor(ListeningExecutorService executor, Runnable task);
48 static final InitialInvoker SUBMIT = ListeningExecutorService::submit;
50 static final InitialInvoker EXECUTE = Executor::execute;
52 public static class TestDeadlockException extends Exception {
53 private static final long serialVersionUID = 1L;
57 private static final Supplier<Exception> DEADLOCK_EXECUTOR_SUPPLIER = TestDeadlockException::new;
59 DeadlockDetectingListeningExecutorService executor;
66 public void tearDown() {
67 if (executor != null) {
68 executor.shutdownNow();
72 DeadlockDetectingListeningExecutorService newExecutor() {
73 return new DeadlockDetectingListeningExecutorService(Executors.newSingleThreadExecutor(),
74 DEADLOCK_EXECUTOR_SUPPLIER);
78 public void testBlockingSubmitOffExecutor() throws Exception {
80 executor = newExecutor();
82 // Test submit with Callable.
84 ListenableFuture<String> future = executor.submit(() -> "foo");
86 assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS));
88 // Test submit with Runnable.
90 executor.submit(() -> { }).get();
92 // Test submit with Runnable and value.
94 future = executor.submit(() -> { }, "foo");
96 assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS));
100 @SuppressWarnings("checkstyle:illegalThrows")
101 public void testNonBlockingSubmitOnExecutorThread() throws Throwable {
103 executor = newExecutor();
105 testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_CALLABLE);
106 testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE);
107 testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT);
109 testNonBlockingSubmitOnExecutorThread(EXECUTE, SUBMIT_CALLABLE);
112 @SuppressWarnings("checkstyle:illegalThrows")
113 void testNonBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker)
116 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
117 final CountDownLatch futureCompletedLatch = new CountDownLatch(1);
119 Runnable task = () -> Futures.addCallback(invoker.invokeExecutor(executor, null), new FutureCallback<Object>() {
121 public void onSuccess(final Object result) {
122 futureCompletedLatch.countDown();
126 @SuppressWarnings("checkstyle:parameterName")
127 public void onFailure(final Throwable t) {
129 futureCompletedLatch.countDown();
131 }, MoreExecutors.directExecutor());
133 initialInvoker.invokeExecutor(executor, task);
135 assertTrue("Task did not complete - executor likely deadlocked",
136 futureCompletedLatch.await(5, TimeUnit.SECONDS));
138 if (caughtEx.get() != null) {
139 throw caughtEx.get();
144 public void testBlockingSubmitOnExecutorThread() throws InterruptedException {
146 executor = newExecutor();
148 testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_CALLABLE);
149 testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE);
150 testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT);
152 testBlockingSubmitOnExecutorThread(EXECUTE, SUBMIT_CALLABLE);
155 @SuppressWarnings("checkstyle:illegalCatch")
156 void testBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker)
157 throws InterruptedException {
159 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
160 final CountDownLatch latch = new CountDownLatch(1);
162 Runnable task = () -> {
165 invoker.invokeExecutor(executor, null).get();
166 } catch (ExecutionException e) {
167 caughtEx.set(e.getCause());
168 } catch (Throwable e) {
175 initialInvoker.invokeExecutor(executor, task);
177 assertTrue("Task did not complete - executor likely deadlocked", latch.await(5, TimeUnit.SECONDS));
178 assertNotNull("Expected exception thrown", caughtEx.get());
179 assertEquals("Caught exception type", TestDeadlockException.class, caughtEx.get().getClass());
183 public void testListenableFutureCallbackWithExecutor() throws InterruptedException {
185 String listenerThreadPrefix = "ListenerThread";
186 ExecutorService listenerExecutor = Executors.newFixedThreadPool(1,
187 new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix + "-%d").build());
189 executor = new DeadlockDetectingListeningExecutorService(
190 Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("SingleThread").build()),
191 DEADLOCK_EXECUTOR_SUPPLIER, listenerExecutor);
194 testListenerCallback(executor, SUBMIT_CALLABLE, listenerThreadPrefix);
195 testListenerCallback(executor, SUBMIT_RUNNABLE, listenerThreadPrefix);
196 testListenerCallback(executor, SUBMIT_RUNNABLE_WITH_RESULT, listenerThreadPrefix);
198 listenerExecutor.shutdownNow();