use of okhttp3.Callback in project okhttp by square.
the class CallTest method asyncLeakedResponseBodyLogsStackTrace.
@Test
public void asyncLeakedResponseBodyLogsStackTrace() throws Exception {
server.enqueue(new MockResponse().setBody("This gets leaked."));
client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
Request request = new Request.Builder().url(server.url("/")).build();
Level original = logger.getLevel();
logger.setLevel(Level.FINE);
logHandler.setFormatter(new SimpleFormatter());
try {
final CountDownLatch latch = new CountDownLatch(1);
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
fail();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Ignore the response so it gets leaked then GC'd.
latch.countDown();
}
});
latch.await();
// There's some flakiness when triggering a GC for objects in a separate thread. Adding a
// small delay appears to ensure the objects will get GC'd.
Thread.sleep(200);
awaitGarbageCollection();
String message = logHandler.take();
assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
assertTrue(message.contains("okhttp3.RealCall.enqueue("));
assertTrue(message.contains("okhttp3.CallTest.asyncLeakedResponseBodyLogsStackTrace("));
} finally {
logger.setLevel(original);
}
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method canceledBeforeResponseReadSignalsOnFailure.
@Test
public void canceledBeforeResponseReadSignalsOnFailure() throws Exception {
Request requestA = new Request.Builder().url(server.url("/a")).build();
final Call call = client.newCall(requestA);
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
call.cancel();
return new MockResponse().setBody("A");
}
});
call.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertFailure("Canceled", "stream was reset: CANCEL", "Socket closed");
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method cancelImmediatelyAfterEnqueue.
@Test
public void cancelImmediatelyAfterEnqueue() throws Exception {
server.enqueue(new MockResponse());
Call call = client.newCall(new Request.Builder().url(server.url("/a")).build());
call.enqueue(callback);
call.cancel();
callback.await(server.url("/a")).assertFailure("Canceled", "Socket closed");
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method canceledBeforeIOSignalsOnFailure.
/**
* This test puts a request in front of one that is to be canceled, so that it is canceled before
* I/O takes place.
*/
@Test
public void canceledBeforeIOSignalsOnFailure() throws Exception {
// Force requests to be executed serially.
okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(client.dispatcher().executorService());
dispatcher.setMaxRequests(1);
client = client.newBuilder().dispatcher(dispatcher).build();
Request requestA = new Request.Builder().url(server.url("/a")).build();
Request requestB = new Request.Builder().url(server.url("/b")).build();
final Call callA = client.newCall(requestA);
final Call callB = client.newCall(requestB);
server.setDispatcher(new Dispatcher() {
char nextResponse = 'A';
@Override
public MockResponse dispatch(RecordedRequest request) {
callB.cancel();
return new MockResponse().setBody(Character.toString(nextResponse++));
}
});
callA.enqueue(callback);
callB.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertBody("A");
// At this point we know the callback is ready, and that it will receive a cancel failure.
callback.await(requestB.url()).assertFailure("Canceled", "Socket closed");
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method illegalToExecuteTwice.
@Test
public void illegalToExecuteTwice() throws Exception {
server.enqueue(new MockResponse().setBody("abc").addHeader("Content-Type: text/plain"));
Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "SyncApiTest").build();
Call call = client.newCall(request);
Response response = call.execute();
response.body().close();
try {
call.execute();
fail();
} catch (IllegalStateException e) {
assertEquals("Already Executed", e.getMessage());
}
try {
call.enqueue(callback);
fail();
} catch (IllegalStateException e) {
assertEquals("Already Executed", e.getMessage());
}
assertEquals("SyncApiTest", server.takeRequest().getHeader("User-Agent"));
}
Aggregations