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;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertTrue;
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;
37 Throwable extractWrappedTestEx(Exception from);
40 static class TestException extends Exception {
41 private static final long serialVersionUID = 1L;
43 TestException(final String message, final Throwable cause) {
44 super(message, cause);
48 static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
49 "Test", TestException.class) {
52 protected TestException newWithCause(final String message, final Throwable cause) {
53 return new TestException(message, cause);
57 static final FutureInvoker GET = new FutureInvoker() {
59 public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
64 public Throwable extractWrappedTestEx(final Exception from) {
65 if (from instanceof ExecutionException) {
66 return from.getCause();
73 static final FutureInvoker TIMED_GET = new FutureInvoker() {
75 public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
76 future.get(1, TimeUnit.HOURS);
80 public Throwable extractWrappedTestEx(final Exception from) {
81 if (from instanceof ExecutionException) {
82 return from.getCause();
89 static final FutureInvoker CHECKED_GET = new FutureInvoker() {
91 public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
96 public Throwable extractWrappedTestEx(final Exception from) {
101 static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
103 public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
104 future.checkedGet(50, TimeUnit.MILLISECONDS);
108 public Throwable extractWrappedTestEx(final Exception from) {
114 public void testGet() throws Exception {
115 SettableFuture<String> delegate = SettableFuture.create();
116 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
117 delegate.set("test");
118 assertEquals("get", "test", future.get());
122 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 {
131 SettableFuture<String> delegate = SettableFuture.create();
132 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
133 delegate.set("test");
134 assertEquals("get", "test", future.get(50, TimeUnit.MILLISECONDS));
138 public void testTimedGetWithExceptions() throws Exception {
139 testExecutionException(TIMED_GET, new RuntimeException());
140 testCancellationException(TIMED_GET);
141 testInterruptedException(TIMED_GET);
145 public void testCheckedGetWithExceptions() throws Exception {
146 testExecutionException(CHECKED_GET, new RuntimeException());
147 testCancellationException(CHECKED_GET);
148 testInterruptedException(CHECKED_GET);
152 public void testTimedCheckedWithExceptions() throws Exception {
153 testExecutionException(TIMED_CHECKED_GET, new RuntimeException());
154 testCancellationException(TIMED_CHECKED_GET);
155 testInterruptedException(TIMED_CHECKED_GET);
158 @SuppressWarnings("checkstyle:illegalCatch")
159 private static void testExecutionException(final FutureInvoker invoker, final Throwable cause) {
160 SettableFuture<String> delegate = SettableFuture.create();
161 MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
163 delegate.setException(cause);
166 invoker.invokeGet(mappingFuture);
167 fail("Expected exception thrown");
168 } catch (Exception e) {
169 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
170 assertNotNull("Expected returned exception is null", expectedTestEx);
171 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
173 if (cause instanceof TestException) {
174 assertNull("Expected null cause", expectedTestEx.getCause());
176 assertSame("TestException cause", cause, expectedTestEx.getCause());
181 @SuppressWarnings("checkstyle:illegalCatch")
182 private static void testCancellationException(final FutureInvoker invoker) {
183 SettableFuture<String> delegate = SettableFuture.create();
184 MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
186 mappingFuture.cancel(false);
189 invoker.invokeGet(mappingFuture);
190 fail("Expected exception thrown");
191 } catch (Exception e) {
192 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
193 assertNotNull("Expected returned exception is null", expectedTestEx);
194 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
195 assertEquals("TestException cause type", CancellationException.class, expectedTestEx.getCause().getClass());
199 @SuppressWarnings("checkstyle:illegalCatch")
200 private static void testInterruptedException(final FutureInvoker invoker) throws Exception {
201 SettableFuture<String> delegate = SettableFuture.create();
202 final MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
204 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
205 final CountDownLatch doneLatch = new CountDownLatch(1);
206 Thread thread = new Thread() {
211 } catch (AssertionError e) {
214 doneLatch.countDown();
220 invoker.invokeGet(mappingFuture);
221 fail("Expected exception thrown");
222 } catch (Exception e) {
223 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
224 assertNotNull("Expected returned exception is null", expectedTestEx);
225 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
226 assertEquals("TestException cause type", InterruptedException.class,
227 expectedTestEx.getCause().getClass());
234 assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS));
236 if (assertError.get() != null) {
237 throw assertError.get();