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
9 package org.opendaylight.yangtools.util.concurrent;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.fail;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.SettableFuture;
20 import java.util.concurrent.CancellationException;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicReference;
25 import org.junit.Test;
28 * Unit tests for MappingCheckedFuture.
30 * @author Thomas Pantelis
32 public class MappingCheckedFutureTest {
34 interface FutureInvoker {
35 void invokeGet( CheckedFuture<?,?> future ) throws Exception;
36 Throwable extractWrappedTestEx( Exception from );
39 @SuppressWarnings("serial")
40 static class TestException extends Exception {
41 TestException( final String message, final Throwable cause ) {
42 super( message, cause );
46 static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
47 "Test", TestException.class ) {
50 protected TestException newWithCause( final String message, final Throwable cause ) {
51 return new TestException( message, cause );
55 static final FutureInvoker GET = new FutureInvoker() {
57 public void invokeGet( final CheckedFuture<?,?> future ) throws Exception {
62 public Throwable extractWrappedTestEx( final Exception from ) {
63 if (from instanceof ExecutionException ) {
64 return ((ExecutionException)from).getCause();
71 static final FutureInvoker TIMED_GET = new FutureInvoker() {
73 public void invokeGet( final CheckedFuture<?,?> future ) throws Exception {
74 future.get( 1, TimeUnit.HOURS );
78 public Throwable extractWrappedTestEx( final Exception from ) {
79 if (from instanceof ExecutionException ) {
80 return ((ExecutionException)from).getCause();
87 static final FutureInvoker CHECKED_GET = new FutureInvoker() {
89 public void invokeGet( final CheckedFuture<?,?> future ) throws Exception {
94 public Throwable extractWrappedTestEx( final Exception from ) {
99 static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
101 public void invokeGet( final CheckedFuture<?,?> future ) throws Exception {
102 future.checkedGet( 50, TimeUnit.MILLISECONDS );
106 public Throwable extractWrappedTestEx( final Exception from ) {
112 public void testGet() throws Exception {
114 SettableFuture<String> delegate = SettableFuture.create();
115 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create( delegate, MAPPER );
116 delegate.set( "test" );
117 assertEquals( "get", "test", future.get() );
121 public void testGetWithExceptions() throws Exception {
123 testExecutionException( GET, new RuntimeException() );
124 testExecutionException( GET, new TestException( "mock", null ) );
125 testCancellationException( GET );
126 testInterruptedException( GET );
130 public void testTimedGet() throws Exception {
132 SettableFuture<String> delegate = SettableFuture.create();
133 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create( delegate, MAPPER );
134 delegate.set( "test" );
135 assertEquals( "get", "test", future.get( 50, TimeUnit.MILLISECONDS ) );
139 public void testTimedGetWithExceptions() throws Exception {
141 testExecutionException( TIMED_GET, new RuntimeException() );
142 testCancellationException( TIMED_GET );
143 testInterruptedException( TIMED_GET );
147 public void testCheckedGetWithExceptions() throws Exception {
149 testExecutionException( CHECKED_GET, new RuntimeException() );
150 testCancellationException( CHECKED_GET );
151 testInterruptedException( CHECKED_GET );
155 public void testTimedCheckedWithExceptions() throws Exception {
157 testExecutionException( TIMED_CHECKED_GET, new RuntimeException() );
158 testCancellationException( TIMED_CHECKED_GET );
159 testInterruptedException( TIMED_CHECKED_GET );
162 private static void testExecutionException( final FutureInvoker invoker, final Throwable cause ) {
164 SettableFuture<String> delegate = SettableFuture.create();
165 MappingCheckedFuture<String,TestException> mappingFuture =
166 MappingCheckedFuture.create( delegate, MAPPER );
168 delegate.setException( cause );
171 invoker.invokeGet( mappingFuture );
172 fail( "Expected exception thrown" );
173 } catch( Exception e ) {
174 Throwable expectedTestEx = invoker.extractWrappedTestEx( e );
175 assertNotNull( "Expected returned exception is null", expectedTestEx );
176 assertEquals( "Exception type", TestException.class, expectedTestEx.getClass() );
178 if (cause instanceof TestException ) {
179 assertNull( "Expected null cause", expectedTestEx.getCause() );
181 assertSame( "TestException cause", cause, expectedTestEx.getCause() );
186 private static void testCancellationException( final FutureInvoker invoker ) {
188 SettableFuture<String> delegate = SettableFuture.create();
189 MappingCheckedFuture<String,TestException> mappingFuture =
190 MappingCheckedFuture.create( delegate, MAPPER );
192 mappingFuture.cancel( false );
195 invoker.invokeGet( mappingFuture );
196 fail( "Expected exception thrown" );
197 } catch( Exception e ) {
198 Throwable expectedTestEx = invoker.extractWrappedTestEx( e );
199 assertNotNull( "Expected returned exception is null", expectedTestEx );
200 assertEquals( "Exception type", TestException.class, expectedTestEx.getClass() );
201 assertEquals( "TestException cause type", CancellationException.class,
202 expectedTestEx.getCause().getClass() );
206 private static void testInterruptedException( final FutureInvoker invoker ) throws Exception {
208 SettableFuture<String> delegate = SettableFuture.create();
209 final MappingCheckedFuture<String,TestException> mappingFuture =
210 MappingCheckedFuture.create( delegate, MAPPER );
212 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
213 final CountDownLatch doneLatch = new CountDownLatch( 1 );
214 Thread thread = new Thread() {
220 } catch( AssertionError e ) {
221 assertError.set( e );
223 doneLatch.countDown();
229 invoker.invokeGet( mappingFuture );
230 fail( "Expected exception thrown" );
231 } catch( Exception e ) {
232 Throwable expectedTestEx = invoker.extractWrappedTestEx( e );
233 assertNotNull( "Expected returned exception is null", expectedTestEx );
234 assertEquals( "Exception type", TestException.class, expectedTestEx.getClass() );
235 assertEquals( "TestException cause type", InterruptedException.class,
236 expectedTestEx.getCause().getClass() );
243 assertEquals( "get call completed", true, doneLatch.await( 5, TimeUnit.SECONDS ) );
245 if (assertError.get() != null ) {
246 throw assertError.get();