X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=common%2Futil%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Futil%2Fconcurrent%2FDeadlockDetectingListeningExecutorServiceTest.java;h=7ca99127a194df4cb474f257e2e97bd4f198938b;hb=1eb87c819cc6881ca7be74917a5d0bb7a9cad41f;hp=6bba351dcabb28a56a7c3cf434c32f29b63adfae;hpb=8289f8e18a621ab0e85c8660b4d5926a62ef92c5;p=yangtools.git diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/DeadlockDetectingListeningExecutorServiceTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/DeadlockDetectingListeningExecutorServiceTest.java index 6bba351dca..7ca99127a1 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/DeadlockDetectingListeningExecutorServiceTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/DeadlockDetectingListeningExecutorServiceTest.java @@ -8,32 +8,31 @@ package org.opendaylight.yangtools.util.concurrent; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.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 static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_RUNNABLE_WITH_RESULT; -import java.util.concurrent.Callable; +import com.google.common.base.Supplier; +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.ThreadFactoryBuilder; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; 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.Before; import org.junit.Test; - -import com.google.common.base.Function; -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.ThreadFactoryBuilder; - -import static org.opendaylight.yangtools.util.concurrent.AsyncNotifyingListeningExecutorServiceTest.testListenerCallback; -import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker; -import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_CALLABLE; -import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_RUNNABLE; -import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_RUNNABLE_WITH_RESULT; +import org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker; /** * Unit tests for DeadlockDetectingListeningExecutorService. @@ -43,33 +42,19 @@ import static org.opendaylight.yangtools.util.concurrent.CommonTestUtils.SUBMIT_ public class DeadlockDetectingListeningExecutorServiceTest { interface InitialInvoker { - void invokeExecutor( ListeningExecutorService executor, Runnable task ); - }; + void invokeExecutor(ListeningExecutorService executor, Runnable task); + } - static final InitialInvoker SUBMIT = new InitialInvoker() { - @Override - public void invokeExecutor( ListeningExecutorService executor, Runnable task ) { - executor.submit( task ); - } - }; + static final InitialInvoker SUBMIT = ListeningExecutorService::submit; - static final InitialInvoker EXECUTE = new InitialInvoker() { - @Override - public void invokeExecutor( ListeningExecutorService executor, Runnable task ) { - executor.execute( task ); - } - }; + static final InitialInvoker EXECUTE = Executor::execute; - @SuppressWarnings("serial") public static class TestDeadlockException extends Exception { + private static final long serialVersionUID = 1L; + } - public static Function DEADLOCK_EXECUTOR_FUNCTION = new Function() { - @Override - public Exception apply( Void notUsed ) { - return new TestDeadlockException(); - } - }; + private static final Supplier DEADLOCK_EXECUTOR_SUPPLIER = TestDeadlockException::new; DeadlockDetectingListeningExecutorService executor; @@ -79,14 +64,14 @@ public class DeadlockDetectingListeningExecutorServiceTest { @After public void tearDown() { - if( executor != null ) { + if (executor != null) { executor.shutdownNow(); } } DeadlockDetectingListeningExecutorService newExecutor() { - return new DeadlockDetectingListeningExecutorService( Executors.newSingleThreadExecutor(), - DEADLOCK_EXECUTOR_FUNCTION ); + return new DeadlockDetectingListeningExecutorService(Executors.newSingleThreadExecutor(), + DEADLOCK_EXECUTOR_SUPPLIER); } @Test @@ -96,32 +81,20 @@ public class DeadlockDetectingListeningExecutorServiceTest { // Test submit with Callable. - ListenableFuture future = executor.submit( new Callable() { - @Override - public String call() throws Exception{ - return "foo"; - } - } ); + ListenableFuture future = executor.submit(() -> "foo"); - assertEquals( "Future result", "foo", future.get( 5, TimeUnit.SECONDS ) ); + assertEquals( "Future result", "foo", future.get( 5, TimeUnit.SECONDS)); // Test submit with Runnable. - executor.submit( new Runnable() { - @Override - public void run(){ - } - } ).get(); + executor.submit(() -> { + }).get(); // Test submit with Runnable and value. - future = executor.submit( new Runnable() { - @Override - public void run(){ - } - }, "foo" ); + future = executor.submit(() -> { }, "foo"); - assertEquals( "Future result", "foo", future.get( 5, TimeUnit.SECONDS ) ); + assertEquals("Future result", "foo", future.get(5, TimeUnit.SECONDS)); } @Test @@ -129,46 +102,38 @@ public class DeadlockDetectingListeningExecutorServiceTest { executor = newExecutor(); - testNonBlockingSubmitOnExecutorThread( SUBMIT, SUBMIT_CALLABLE ); - testNonBlockingSubmitOnExecutorThread( SUBMIT, SUBMIT_RUNNABLE ); - testNonBlockingSubmitOnExecutorThread( SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT ); + testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_CALLABLE); + testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE); + testNonBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT); - testNonBlockingSubmitOnExecutorThread( EXECUTE, SUBMIT_CALLABLE ); + testNonBlockingSubmitOnExecutorThread(EXECUTE, SUBMIT_CALLABLE); } - void testNonBlockingSubmitOnExecutorThread( InitialInvoker initialInvoker, - final Invoker invoker ) throws Throwable { + void testNonBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker) + throws Throwable { final AtomicReference caughtEx = new AtomicReference<>(); - final CountDownLatch futureCompletedLatch = new CountDownLatch( 1 ); + final CountDownLatch futureCompletedLatch = new CountDownLatch(1); - Runnable task = new Runnable() { - @SuppressWarnings({ "unchecked", "rawtypes" }) + Runnable task = () -> Futures.addCallback(invoker.invokeExecutor(executor, null), new FutureCallback() { @Override - public void run() { - - Futures.addCallback( invoker.invokeExecutor( executor, null ), new FutureCallback() { - @Override - public void onSuccess( Object result ) { - futureCompletedLatch.countDown(); - } - - @Override - public void onFailure( Throwable t ) { - caughtEx.set( t ); - futureCompletedLatch.countDown(); - } - } ); + public void onSuccess(final Object result) { + futureCompletedLatch.countDown(); } - }; + @Override + public void onFailure(final Throwable t) { + caughtEx.set(t); + futureCompletedLatch.countDown(); + } + }); - initialInvoker.invokeExecutor( executor, task ); + initialInvoker.invokeExecutor(executor, task); - assertTrue( "Task did not complete - executor likely deadlocked", - futureCompletedLatch.await( 5, TimeUnit.SECONDS ) ); + assertTrue("Task did not complete - executor likely deadlocked", + futureCompletedLatch.await(5, TimeUnit.SECONDS)); - if( caughtEx.get() != null ) { + if (caughtEx.get() != null) { throw caughtEx.get(); } } @@ -178,61 +143,54 @@ public class DeadlockDetectingListeningExecutorServiceTest { executor = newExecutor(); - testBlockingSubmitOnExecutorThread( SUBMIT, SUBMIT_CALLABLE ); - testBlockingSubmitOnExecutorThread( SUBMIT, SUBMIT_RUNNABLE ); - testBlockingSubmitOnExecutorThread( SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT ); + testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_CALLABLE); + testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE); + testBlockingSubmitOnExecutorThread(SUBMIT, SUBMIT_RUNNABLE_WITH_RESULT); - testBlockingSubmitOnExecutorThread( EXECUTE, SUBMIT_CALLABLE ); + testBlockingSubmitOnExecutorThread(EXECUTE, SUBMIT_CALLABLE); } - void testBlockingSubmitOnExecutorThread( InitialInvoker initialInvoker, - final Invoker invoker ) throws Exception { + void testBlockingSubmitOnExecutorThread(final InitialInvoker initialInvoker, final Invoker invoker) + throws Exception { final AtomicReference caughtEx = new AtomicReference<>(); - final CountDownLatch latch = new CountDownLatch( 1 ); - - Runnable task = new Runnable() { - @Override - public void run() { - - try { - invoker.invokeExecutor( executor, null ).get(); - } catch( ExecutionException e ) { - caughtEx.set( e.getCause() ); - } catch( Throwable e ) { - caughtEx.set( e ); - } finally { - latch.countDown(); - } + final CountDownLatch latch = new CountDownLatch(1); + + Runnable task = () -> { + + try { + invoker.invokeExecutor(executor, null).get(); + } catch(ExecutionException e) { + caughtEx.set(e.getCause()); + } catch(Throwable e) { + caughtEx.set(e); + } finally { + latch.countDown(); } - }; - initialInvoker.invokeExecutor( executor, task ); - - assertTrue( "Task did not complete - executor likely deadlocked", - latch.await( 5, TimeUnit.SECONDS ) ); + initialInvoker.invokeExecutor(executor, task); - assertNotNull( "Expected exception thrown", caughtEx.get() ); - assertEquals( "Caught exception type", TestDeadlockException.class, caughtEx.get().getClass() ); + 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()); } @Test public void testListenableFutureCallbackWithExecutor() throws InterruptedException { String listenerThreadPrefix = "ListenerThread"; - ExecutorService listenerExecutor = Executors.newFixedThreadPool( 1, - new ThreadFactoryBuilder().setNameFormat( listenerThreadPrefix + "-%d" ).build() ); + ExecutorService listenerExecutor = Executors.newFixedThreadPool(1, + new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix + "-%d").build()); executor = new DeadlockDetectingListeningExecutorService( - Executors.newSingleThreadExecutor( - new ThreadFactoryBuilder().setNameFormat( "SingleThread" ).build() ), - DEADLOCK_EXECUTOR_FUNCTION, listenerExecutor ); + Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("SingleThread").build()), + DEADLOCK_EXECUTOR_SUPPLIER, listenerExecutor); try { - testListenerCallback( executor, SUBMIT_CALLABLE, listenerThreadPrefix ); - testListenerCallback( executor, SUBMIT_RUNNABLE, listenerThreadPrefix ); - testListenerCallback( executor, SUBMIT_RUNNABLE_WITH_RESULT, listenerThreadPrefix ); + testListenerCallback(executor, SUBMIT_CALLABLE, listenerThreadPrefix); + testListenerCallback(executor, SUBMIT_RUNNABLE, listenerThreadPrefix); + testListenerCallback(executor, SUBMIT_RUNNABLE_WITH_RESULT, listenerThreadPrefix); } finally { listenerExecutor.shutdownNow(); }