diff options
author | Koushik Dutta <koushd@gmail.com> | 2013-03-24 23:00:02 -0700 |
---|---|---|
committer | Koushik Dutta <koushd@gmail.com> | 2013-03-24 23:00:02 -0700 |
commit | 5b223b2e6634083dcda9d5eeaf22f767545f177b (patch) | |
tree | 86079597ade2c7fad66c4f0ad65f1fe63409e447 /AndroidAsyncTest | |
parent | 1474a23c97173347a0b970d3bd3f65eac622fd36 (diff) | |
download | AndroidAsync-5b223b2e6634083dcda9d5eeaf22f767545f177b.tar.gz AndroidAsync-5b223b2e6634083dcda9d5eeaf22f767545f177b.tar.bz2 AndroidAsync-5b223b2e6634083dcda9d5eeaf22f767545f177b.zip |
more Future tests
Diffstat (limited to 'AndroidAsyncTest')
-rw-r--r-- | AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java b/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java index 782bb9b..2d3ac94 100644 --- a/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java +++ b/AndroidAsyncTest/src/com/koushikdutta/async/test/FutureTests.java @@ -1,5 +1,6 @@ package com.koushikdutta.async.test; +import java.util.ArrayList; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; @@ -91,4 +92,162 @@ public class FutureTests extends TestCase { assertEquals((int)i1.get(), 2); assertEquals((int)i2.get(), 3); } + + public void testContinuationFail() throws Exception { + final Semaphore semaphore = new Semaphore(0); + final Continuation c = new Continuation(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + assertNotNull(ex); + semaphore.release(); + } + }); + + c.add(new ContinuationCallback() { + @Override + public void onContinue(Continuation continuation, CompletedCallback next) throws Exception { + throw new Exception("fail"); + } + }); + + new Thread() { + public void run() { + c.start(); + }; + }.start(); + + assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS)); + } + + public void testContinuationCancel() throws Exception { + final Semaphore semaphore = new Semaphore(0); + final Continuation c = new Continuation(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + fail(); + semaphore.release(); + } + }); + c.setCancelCallback(new Runnable() { + @Override + public void run() { + semaphore.release(); + } + }); + + c.add(new ContinuationCallback() { + @Override + public void onContinue(Continuation continuation, CompletedCallback next) throws Exception { + Thread.sleep(10000); + } + }); + + new Thread() { + public void run() { + c.start(); + }; + }.start(); + + new Thread() { + public void run() { + try { + Thread.sleep(1000); + } + catch (Exception e) { + } + c.cancel(); + }; + }.start(); + + assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS)); + } + + + public void testChildContinuationCancel() throws Exception { + final Semaphore semaphore = new Semaphore(0); + final Continuation c = new Continuation(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + fail(); + semaphore.release(); + } + }); + c.setCancelCallback(new Runnable() { + @Override + public void run() { + semaphore.release(); + } + }); + + c.add(new ContinuationCallback() { + @Override + public void onContinue(Continuation continuation, CompletedCallback next) throws Exception { + Thread.sleep(10000); + } + }); + + final Continuation child = new Continuation(); + child.add(new ContinuationCallback() { + @Override + public void onContinue(Continuation continuation, CompletedCallback next) throws Exception { + Thread.sleep(10000); + } + }); + + c.add(child); + + new Thread() { + public void run() { + c.start(); + }; + }.start(); + + new Thread() { + public void run() { + try { + Thread.sleep(1000); + } + catch (Exception e) { + } + child.cancel(); + }; + }.start(); + + assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS)); + } + + public void testContinuationArray() throws Exception { + final ArrayList<Integer> results = new ArrayList<Integer>(); + final Semaphore semaphore = new Semaphore(0); + final Continuation c = new Continuation(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + semaphore.release(); + } + }); + + for (int i = 0; i < 10; i++) { + final int j = i; + c.add(new ContinuationCallback() { + @Override + public void onContinue(Continuation continuation, CompletedCallback next) throws Exception { + results.add(j); + next.onCompleted(null); + } + }); + } + + new Thread() { + public void run() { + c.start(); + }; + }.start(); + + assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS)); + + assertEquals(10, results.size()); + for (int i = 0; i < 10; i++) { + assertEquals((int)results.get(i), i); + } + } } |