use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class HttpClientTests method testClockworkMod.
public void testClockworkMod() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Md5 md5 = Md5.createInstance();
client.execute("http://www.clockworkmod.com", new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
// make sure gzip decoding works, as that is generally what github sends.
Assert.assertEquals("gzip", response.headers().get("Content-Encoding"));
response.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
md5.update(bb);
}
});
response.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
}
});
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class Issue59 method testIssue.
public void testIssue() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
// setting this to empty is a hacky way of telling the framework not to use
// transfer-encoding. It will get removed.
response.getHeaders().set("Transfer-Encoding", "");
response.code(200);
Util.writeAll(response, "foobarbeepboop".getBytes(), new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.end();
}
});
}
});
httpServer.listen(5959);
AsyncHttpGet get = new AsyncHttpGet("http://localhost:5959/");
get.setLogging("issue59", Log.VERBOSE);
get.getHeaders().removeAll("Connection");
get.getHeaders().removeAll("Accept-Encoding");
assertEquals("foobarbeepboop", AsyncHttpClient.getDefaultInstance().executeString(get, null).get(1000, TimeUnit.MILLISECONDS));
} finally {
httpServer.stop();
AsyncServer.getDefault().stop();
}
}
use of com.koushikdutta.async.callback.CompletedCallback 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.CompletedCallback 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.CompletedCallback 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));
}
Aggregations