use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.
the class FutureTests method testContinuation.
public void testContinuation() throws Exception {
final Semaphore semaphore = new Semaphore(0);
someValue = 0;
final Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
assertNull(ex);
semaphore.release();
}
});
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, final CompletedCallback next) throws Exception {
new Thread() {
public void run() {
someValue++;
next.onCompleted(null);
}
;
}.start();
}
});
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, final CompletedCallback next) throws Exception {
new Thread() {
public void run() {
someValue++;
next.onCompleted(null);
}
;
}.start();
}
});
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, final CompletedCallback next) throws Exception {
someValue++;
next.onCompleted(null);
}
});
new Thread() {
public void run() {
c.start();
}
;
}.start();
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
assertEquals(someValue, 3);
}
use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.
the class FutureTests method testContinuationCancel.
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));
}
use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.
the class FutureTests method testChildContinuationCancel.
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));
}
use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.
the class MultipartFormDataBody method write.
@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
if (mParts == null)
return;
Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
completed.onCompleted(ex);
// if (ex == null)
// sink.end();
// else
// sink.close();
}
});
for (final Part part : mParts) {
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
byte[] bytes = part.getRawHeaders().toPrefixString(getBoundaryStart()).getBytes();
com.koushikdutta.async.Util.writeAll(sink, bytes, next);
written += bytes.length;
}
}).add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
long partLength = part.length();
if (partLength >= 0)
written += partLength;
part.write(sink, next);
}
}).add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
byte[] bytes = "\r\n".getBytes();
com.koushikdutta.async.Util.writeAll(sink, bytes, next);
written += bytes.length;
}
});
}
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
byte[] bytes = (getBoundaryEnd()).getBytes();
com.koushikdutta.async.Util.writeAll(sink, bytes, next);
written += bytes.length;
assert written == totalToWrite;
}
});
c.start();
}
Aggregations