From 1474a23c97173347a0b970d3bd3f65eac622fd36 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 24 Mar 2013 22:23:53 -0700 Subject: Add support for waiting for futures in continuations. Add tests for continuations and futures. --- .../com/koushikdutta/async/test/FutureTests.java | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java (limited to 'AndroidAsyncTest') diff --git a/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java b/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java new file mode 100644 index 0000000..782bb9b --- /dev/null +++ b/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java @@ -0,0 +1,94 @@ +package com.koushikdutta.async.test; + +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +import junit.framework.TestCase; + +import com.koushikdutta.async.callback.CompletedCallback; +import com.koushikdutta.async.callback.ContinuationCallback; +import com.koushikdutta.async.future.Continuation; +import com.koushikdutta.async.future.SimpleFuture; + +public class FutureTests extends TestCase { + private static class IntegerFuture extends SimpleFuture { + private IntegerFuture() { + } + + public static IntegerFuture create(final int value, final long timeout) { + final IntegerFuture ret = new IntegerFuture(); + + new Thread() { + public void run() { + try { + Thread.sleep(timeout); + ret.setComplete(value); + } + catch (Exception e) { + ret.setComplete(e); + } + }; + }.start(); + + return ret; + } + } + + public void testIntegerFuture() throws Exception { + IntegerFuture i = IntegerFuture.create(10, 500L); + assertEquals((int)i.get(), 10); + } + + public void testContinuation() throws Exception { + final Semaphore semaphore = new Semaphore(0); + final Continuation c = new Continuation(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + semaphore.release(); + } + }); + + c.add(new ContinuationCallback() { + @Override + public void onContinue(Continuation continuation, CompletedCallback next) throws Exception { + Thread.sleep(200); + next.onCompleted(null); + } + }); + + new Thread() { + public void run() { + c.start(); + }; + }.start(); + + assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS)); + } + + public void testFutureChain() throws Exception { + final Semaphore semaphore = new Semaphore(0); + final Continuation c = new Continuation(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + semaphore.release(); + } + }); + + IntegerFuture i1; + c.add(i1 = IntegerFuture.create(2, 200)); + + IntegerFuture i2; + c.add(i2 = IntegerFuture.create(3, 200)); + + new Thread() { + public void run() { + c.start(); + }; + }.start(); + + assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS)); + + assertEquals((int)i1.get(), 2); + assertEquals((int)i2.get(), 3); + } +} -- cgit v1.2.3