1 /* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static junit.framework.Assert.assertEquals; 21 import static junit.framework.Assert.assertFalse; 22 import static junit.framework.Assert.assertTrue; 23 import static junit.framework.Assert.fail; 24 25 import java.util.concurrent.CancellationException; 26 import java.util.concurrent.CountDownLatch; 27 import java.util.concurrent.ExecutionException; 28 import java.util.concurrent.ExecutorService; 29 import java.util.concurrent.Executors; 30 import java.util.concurrent.TimeUnit; 31 32 import javax.annotation.Nullable; 33 34 /** 35 * Used to test listenable future implementations. 36 * 37 * @author Sven Mawson 38 */ 39 public class ListenableFutureTester { 40 41 private final ExecutorService exec; 42 private final ListenableFuture<?> future; 43 private final CountDownLatch latch; 44 45 public ListenableFutureTester(ListenableFuture<?> future) { 46 this.exec = Executors.newCachedThreadPool(); 47 this.future = checkNotNull(future); 48 this.latch = new CountDownLatch(1); 49 } 50 51 public void setUp() { 52 future.addListener(new Runnable() { 53 @Override public void run() { 54 latch.countDown(); 55 } 56 }, exec); 57 58 assertEquals(1, latch.getCount()); 59 assertFalse(future.isDone()); 60 assertFalse(future.isCancelled()); 61 } 62 63 public void tearDown() { 64 exec.shutdown(); 65 } 66 67 public void testCompletedFuture(@Nullable Object expectedValue) 68 throws InterruptedException, ExecutionException { 69 assertTrue(future.isDone()); 70 assertFalse(future.isCancelled()); 71 72 assertTrue(latch.await(5, TimeUnit.SECONDS)); 73 assertTrue(future.isDone()); 74 assertFalse(future.isCancelled()); 75 76 assertEquals(expectedValue, future.get()); 77 } 78 79 public void testCancelledFuture() 80 throws InterruptedException, ExecutionException { 81 assertTrue(future.isDone()); 82 assertTrue(future.isCancelled()); 83 84 assertTrue(latch.await(5, TimeUnit.SECONDS)); 85 assertTrue(future.isDone()); 86 assertTrue(future.isCancelled()); 87 88 try { 89 future.get(); 90 fail("Future should throw CancellationException on cancel."); 91 } catch (CancellationException expected) {} 92 } 93 94 public void testFailedFuture(@Nullable String message) 95 throws InterruptedException { 96 assertTrue(future.isDone()); 97 assertFalse(future.isCancelled()); 98 99 assertTrue(latch.await(5, TimeUnit.SECONDS)); 100 assertTrue(future.isDone()); 101 assertFalse(future.isCancelled()); 102 103 try { 104 future.get(); 105 fail("Future should rethrow the exception."); 106 } catch (ExecutionException e) { 107 assertEquals(message, e.getCause().getMessage()); 108 } 109 } 110 } 111