public
Last active

Unit test/sample code for ListneableFutures blog

  • Download Gist
ListenableFuturesTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
 
 
import com.google.common.util.concurrent.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
 
/**
* Created by IntelliJ IDEA.
* User: bbejeck
* Date: 11/21/11
* Time: 10:50 PM
*/
 
 
public class ListenableFuturesTest {
 
private ListeningExecutorService executorService;
private CountDownLatch startSignal;
private CountDownLatch endSignal;
private static final int NUM_THREADS = 5;
private boolean callbackRan;
 
 
@Before
public void setUp() {
executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
startSignal = new CountDownLatch(1);
endSignal = new CountDownLatch(1);
callbackRan = false;
 
}
 
@After
public void tearDown() {
executorService.shutdownNow();
}
 
@Test
public void testRunListenableFutureWithCallback() throws Exception {
ListenableFuture<String> futureTask = executorService.submit(new Task());
futureTask.addListener(new Runnable() {
@Override
public void run() {
callbackRan = true;
endSignal.countDown();
}
}, executorService);
 
endSignal.await();
assertThat(callbackRan, is(true));
}
 
@Test
public void testRunListenableFutureWithFutureCallbackSuccess() throws Exception {
ListenableFuture<String> futureTask = executorService.submit(new Task(startSignal));
FutureCallbackImpl callback = new FutureCallbackImpl();
Futures.addCallback(futureTask, callback);
startSignal.countDown();
endSignal.await();
assertThat(callback.getCallbackResult(), is("Task Done successfully"));
}
 
@Test
public void testRunListenableFutureWithFutureCallbackFailure() throws Exception {
ListenableFuture<String> futureTask = executorService.submit(new Task(null));
FutureCallbackImpl callback = new FutureCallbackImpl();
Futures.addCallback(futureTask, callback);
//startSignal.countDown(); don't call countdown
endSignal.await();
assertThat(callback.getCallbackResult(), is("java.lang.NullPointerException"));
}
 
 
private class FutureCallbackImpl implements FutureCallback<String> {
 
private StringBuilder builder = new StringBuilder();
 
@Override
public void onSuccess(String result) {
builder.append(result).append(" successfully");
done();
 
}
 
@Override
public void onFailure(Throwable t) {
builder.append(t.toString());
done();
}
 
private void done() {
endSignal.countDown();
}
 
public String getCallbackResult() {
return builder.toString();
}
}
 
 
private class Task implements Callable<String> {
private CountDownLatch start;
 
public Task() {
}
 
public Task(CountDownLatch start) {
this.start = start;
}
 
@Override
public String call() throws Exception {
this.start.await(1, TimeUnit.SECONDS);
Thread.sleep(1000);
return "Task Done";
}
}
 
 
}

Most informative. Thanks!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.