Search in sources :

Example 26 with Callback

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);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleFormatter(java.util.logging.SimpleFormatter) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(okhttp3.mockwebserver.MockResponse) Level(java.util.logging.Level) Test(org.junit.Test)

Example 27 with Callback

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");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Dispatcher(okhttp3.mockwebserver.Dispatcher) Test(org.junit.Test)

Example 28 with Callback

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 29 with Callback

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");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Dispatcher(okhttp3.mockwebserver.Dispatcher) Test(org.junit.Test)

Example 30 with Callback

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"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)178 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)166 DateTime (org.joda.time.DateTime)166 ServiceCall (com.microsoft.rest.ServiceCall)140 IOException (java.io.IOException)68 Test (org.junit.Test)60 MockResponse (okhttp3.mockwebserver.MockResponse)58 List (java.util.List)54 PagedList (com.microsoft.azure.PagedList)52 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)52 Call (okhttp3.Call)49 Request (okhttp3.Request)49 Response (okhttp3.Response)48 Callback (okhttp3.Callback)41 RequestBody (okhttp3.RequestBody)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)27 CountDownLatch (java.util.concurrent.CountDownLatch)16 OkHttpClient (okhttp3.OkHttpClient)15 Call (retrofit2.Call)15 Callback (retrofit2.Callback)14