use of java.util.concurrent.Semaphore in project AndroidAsync by koush.
the class FutureTests method testContinuationArray.
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);
}
}
use of java.util.concurrent.Semaphore in project AndroidAsync by koush.
the class FutureTests method testFutureChain.
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);
}
use of java.util.concurrent.Semaphore in project AndroidAsync by koush.
the class FutureTests method testContinuationFail.
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));
}
use of java.util.concurrent.Semaphore in project AndroidAsync by koush.
the class FutureTests method testFutureCallback.
public void testFutureCallback() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final IntegerFuture future = IntegerFuture.create(20, 1000);
final Thread mainThread = Thread.currentThread();
future.setCallback(new FutureCallback<Integer>() {
@Override
public void onCompleted(Exception e, Integer result) {
assertNotSame(Thread.currentThread(), mainThread);
semaphore.release();
}
});
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
use of java.util.concurrent.Semaphore in project AndroidAsync by koush.
the class CacheTests method testFilteredDataEmitter.
public void testFilteredDataEmitter() throws Exception {
final Semaphore semaphore = new Semaphore(0);
FilteredDataEmitter f = new FilteredDataEmitter() {
@Override
public boolean isPaused() {
return false;
}
};
f.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
assertEquals(bb.readString(), "hello");
bb.recycle();
semaphore.release();
}
});
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
f.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
fail();
}
});
f.close();
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
}
Aggregations