Search in sources :

Example 11 with Callback

use of okhttp3.Callback in project retrofit by square.

the class CallTest method transportProblemAsync.

@Test
public void transportProblemAsync() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.getString().enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    Throwable failure = failureRef.get();
    assertThat(failure).isInstanceOf(IOException.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 12 with Callback

use of okhttp3.Callback in project retrofit by square.

the class CallTest method requestThrowingBeforeEnqueueFailsEnqueue.

@Test
public void requestThrowingBeforeEnqueueFailsEnqueue() throws IOException, InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            throw new RuntimeException("Broken!");
        }
    };
    Call<String> call = service.postRequestBody(a);
    try {
        call.request();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            assertThat(t).isExactlyInstanceOf(RuntimeException.class).hasMessage("Broken!");
            assertThat(writeCount.get()).isEqualTo(1);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 13 with Callback

use of okhttp3.Callback in project retrofit by square.

the class CallTest method requestAfterEnqueueFailingThrows.

@Test
public void requestAfterEnqueueFailingThrows() throws IOException, InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            throw new RuntimeException("Broken!");
        }
    };
    Call<String> call = service.postRequestBody(a);
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            assertThat(t).isExactlyInstanceOf(RuntimeException.class).hasMessage("Broken!");
            assertThat(writeCount.get()).isEqualTo(1);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    try {
        call.request();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 14 with Callback

use of okhttp3.Callback in project retrofit by square.

the class CallTest method cancelRequest.

@Test
public void cancelRequest() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    Call<String> call = service.getString();
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    call.cancel();
    assertThat(call.isCanceled()).isTrue();
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(IOException.class).hasMessage("Canceled");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 15 with Callback

use of okhttp3.Callback in project okhttp by square.

the class OkHttpAsync method prepare.

@Override
public void prepare(final Benchmark benchmark) {
    concurrencyLevel = benchmark.concurrencyLevel;
    targetBacklog = benchmark.targetBacklog;
    client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
    if (benchmark.tls) {
        SslClient sslClient = SslClient.localhost();
        SSLSocketFactory socketFactory = sslClient.socketFactory;
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession session) {
                return true;
            }
        };
        client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    }
    callback = new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("Failed: " + e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            ResponseBody body = response.body();
            long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
            long finish = System.nanoTime();
            if (VERBOSE) {
                long start = (Long) response.request().tag();
                System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
            }
            requestsInFlight.decrementAndGet();
        }
    };
}
Also used : Call(okhttp3.Call) SslClient(okhttp3.internal.tls.SslClient) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) HostnameVerifier(javax.net.ssl.HostnameVerifier) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

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