Search in sources :

Example 26 with HttpRequest

use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.

the class RequestIdPolicyTests method sameRequestIdForRetry.

@Test
public void sameRequestIdForRetry() {
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        String firstRequestId = null;

        @Override
        public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (firstRequestId != null) {
                String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
                Assertions.assertNotNull(newRequestId, "newRequestId should not be null");
                Assertions.assertEquals(newRequestId, firstRequestId);
            }
            firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
            if (firstRequestId == null) {
                Assertions.fail("The firstRequestId should not be null.");
            }
            httpCallback.onSuccess(mockResponse);
        // TODO: anuchan add a test with misbehaving httpclient that signal twice and assert we throw meaningful error
        // TODO: anuchan add a test that watch running calls when we schedule (i.e. ensure not holding the thread)
        // 
        // if (firstRequestId != null) {
        // String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
        // Assertions.assertNotNull(newRequestId);
        // Assertions.assertEquals(newRequestId, firstRequestId);
        // }
        // firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
        // if (firstRequestId == null) {
        // Assertions.fail();
        // }
        // httpCallback.onSuccess(mockResponse);
        }
    }).policies(new RequestIdPolicy(), new RetryPolicy(new FixedDelay(1, Duration.of(5, ChronoUnit.SECONDS)))).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "sameRequestIdForRetry");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 27 with HttpRequest

use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.

the class RetryPolicyTests method fixedDelayRetry.

@Test
public void fixedDelayRetry() {
    final int maxRetries = 5;
    final long delayMillis = 500;
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        int count = -1;

        long previousAttemptMadeAt = -1;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (count > 0) {
                Assertions.assertTrue(System.currentTimeMillis() >= previousAttemptMadeAt + delayMillis);
            }
            Assertions.assertTrue(count++ < maxRetries);
            previousAttemptMadeAt = System.currentTimeMillis();
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 500));
        }
    }).policies(new RetryPolicy(new FixedDelay(maxRetries, Duration.ofMillis(delayMillis)))).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "fixedDelayRetry");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 28 with HttpRequest

use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.

the class RetryPolicyTests method exponentialDelayRetry.

@Test
public void exponentialDelayRetry() {
    final int maxRetries = 5;
    final long baseDelayMillis = 100;
    final long maxDelayMillis = 1000;
    ExponentialBackoff exponentialBackoff = new ExponentialBackoff(maxRetries, Duration.ofMillis(baseDelayMillis), Duration.ofMillis(maxDelayMillis));
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        int count = -1;

        long previousAttemptMadeAt = -1;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (count > 0) {
                long requestMadeAt = System.currentTimeMillis();
                long expectedToBeMadeAt = previousAttemptMadeAt + ((1 << (count - 1)) * (long) (baseDelayMillis * 0.95));
                Assertions.assertTrue(requestMadeAt >= expectedToBeMadeAt);
            }
            Assertions.assertTrue(count++ < maxRetries);
            previousAttemptMadeAt = System.currentTimeMillis();
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 503));
        }
    }).policies(new RetryPolicy(exponentialBackoff)).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "exponentialDelayRetry");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 29 with HttpRequest

use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.

the class RetryPolicyTests method retryEndOn501.

@Test
public void retryEndOn501() {
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        // Send 408, 500, 502, all retried, with a 501 ending
        private final int[] codes = new int[] { 408, 500, 502, 501 };

        private int count = 0;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, codes[count++]));
        }
    }).policies(new RetryPolicy(new FixedDelay(3, Duration.of(0, ChronoUnit.MILLIS)))).build();
    final HttpResponse[] httpResponse = new HttpResponse[1];
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            httpResponse[0] = response;
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "retryEndOn501");
    assertNotNull(httpResponse[0]);
    assertEquals(501, httpResponse[0].getStatusCode());
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 30 with HttpRequest

use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.

the class ProtocolPolicy method process.

@Override
public void process(HttpPipelinePolicyChain chain) {
    HttpRequest httpRequest = chain.getRequest();
    final UrlBuilder urlBuilder = UrlBuilder.parse(httpRequest.getUrl());
    if (overwrite || urlBuilder.getScheme() == null) {
        logger.info("Setting protocol to {}", protocol);
        try {
            httpRequest.setUrl(urlBuilder.setScheme(protocol).toString());
        } catch (IllegalArgumentException error) {
            chain.completedError(error);
            return;
        }
        chain.processNextPolicy(httpRequest);
    } else {
        chain.processNextPolicy(httpRequest);
    }
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) UrlBuilder(com.azure.android.core.http.util.UrlBuilder)

Aggregations

HttpRequest (com.azure.android.core.http.HttpRequest)37 HttpResponse (com.azure.android.core.http.HttpResponse)22 HttpCallback (com.azure.android.core.http.HttpCallback)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 HttpPipeline (com.azure.android.core.http.HttpPipeline)17 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)17 CancellationToken (com.azure.android.core.util.CancellationToken)15 Test (org.junit.jupiter.api.Test)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 HttpClient (com.azure.android.core.http.HttpClient)4 UrlBuilder (com.azure.android.core.http.util.UrlBuilder)4 IOException (java.io.IOException)4 HttpHeader (com.azure.android.core.http.HttpHeader)3 ClientLogger (com.azure.android.core.logging.ClientLogger)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 List (java.util.List)3 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 LIBRARY_PACKAGE_NAME (com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME)2