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
17package com.google.common.util.concurrent;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertFalse;
21import static junit.framework.Assert.assertTrue;
22import static junit.framework.Assert.fail;
23
24import java.util.concurrent.CancellationException;
25import java.util.concurrent.CountDownLatch;
26import java.util.concurrent.ExecutionException;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Executors;
29import java.util.concurrent.TimeUnit;
30
31/**
32 * Used to test listenable future implementations.
33 *
34 * @author Sven Mawson
35 */
36public class ListenableFutureTester {
37
38  private final ExecutorService exec;
39  private final ListenableFuture<?> future;
40  private final CountDownLatch latch;
41
42  public ListenableFutureTester(ListenableFuture<?> future) {
43    this.exec = Executors.newCachedThreadPool();
44    this.future = future;
45    this.latch = new CountDownLatch(1);
46  }
47
48  public void setUp() {
49    future.addListener(new Runnable() {
50      @Override public void run() {
51        latch.countDown();
52      }
53    }, exec);
54
55    assertEquals(1, latch.getCount());
56    assertFalse(future.isDone());
57    assertFalse(future.isCancelled());
58  }
59
60  public void tearDown() {
61    exec.shutdown();
62  }
63
64  public void testCompletedFuture(Object expectedValue)
65      throws InterruptedException, ExecutionException {
66    assertTrue(future.isDone());
67    assertFalse(future.isCancelled());
68
69    assertTrue(latch.await(5, TimeUnit.SECONDS));
70    assertTrue(future.isDone());
71    assertFalse(future.isCancelled());
72
73    assertEquals(expectedValue, future.get());
74  }
75
76  public void testCancelledFuture()
77      throws InterruptedException, ExecutionException {
78    assertTrue(future.isDone());
79    assertTrue(future.isCancelled());
80
81    assertTrue(latch.await(5, TimeUnit.SECONDS));
82    assertTrue(future.isDone());
83    assertTrue(future.isCancelled());
84
85    try {
86      future.get();
87      fail("Future should throw CancellationException on cancel.");
88    } catch (CancellationException expected) {}
89  }
90
91  public void testFailedFuture(String message)
92      throws InterruptedException {
93    assertTrue(future.isDone());
94    assertFalse(future.isCancelled());
95
96    assertTrue(latch.await(5, TimeUnit.SECONDS));
97    assertTrue(future.isDone());
98    assertFalse(future.isCancelled());
99
100    try {
101      future.get();
102      fail("Future should rethrow the exception.");
103    } catch (ExecutionException e) {
104      assertEquals(message, e.getCause().getMessage());
105    }
106  }
107}
108