Search in sources :

Example 1 with RetryHandler

use of com.microsoft.rest.retry.RetryHandler in project autorest-clientruntime-for-java by Azure.

the class RequestIdHeaderInterceptorTests method sameRequestIdForRetry.

@Test
public void sameRequestIdForRetry() throws Exception {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withInterceptor(new RequestIdHeaderInterceptor()).withInterceptor(new RetryHandler()).withInterceptor(new Interceptor() {

        private String firstRequestId = null;

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (request.header(REQUEST_ID_HEADER) != null) {
                if (firstRequestId == null) {
                    firstRequestId = request.header(REQUEST_ID_HEADER);
                    return new Response.Builder().code(500).request(request).message("Error").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
                } else if (request.header(REQUEST_ID_HEADER).equals(firstRequestId)) {
                    return new Response.Builder().code(200).request(request).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
                }
            }
            return new Response.Builder().code(400).request(request).protocol(Protocol.HTTP_1_1).build();
        }
    }).build();
    AzureServiceClient serviceClient = new AzureServiceClient(restClient) {
    };
    Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) Request(okhttp3.Request) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Response(okhttp3.Response) RetryHandler(com.microsoft.rest.retry.RetryHandler) Interceptor(okhttp3.Interceptor) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Test(org.junit.Test)

Example 2 with RetryHandler

use of com.microsoft.rest.retry.RetryHandler in project autorest-clientruntime-for-java by Azure.

the class RetryHandlerTests method exponentialRetryMax.

@Test
public void exponentialRetryMax() throws Exception {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
    clientBuilder.addInterceptor(new RetryHandler());
    clientBuilder.addInterceptor(new Interceptor() {

        // Send 500 until max retry is hit
        private int count = 0;

        @Override
        public Response intercept(Chain chain) throws IOException {
            Assert.assertTrue(count++ < 5);
            return new Response.Builder().request(chain.request()).code(500).message("Error").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").get().build()).execute();
    Assert.assertEquals(500, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) RetryHandler(com.microsoft.rest.retry.RetryHandler) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 3 with RetryHandler

use of com.microsoft.rest.retry.RetryHandler in project autorest-clientruntime-for-java by Azure.

the class RetryHandlerTests method exponentialRetryEndOn501.

@Test
public void exponentialRetryEndOn501() throws Exception {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
    clientBuilder.addInterceptor(new RetryHandler());
    clientBuilder.addInterceptor(new Interceptor() {

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

        private int count = 0;

        @Override
        public Response intercept(Chain chain) throws IOException {
            return new Response.Builder().request(chain.request()).code(codes[count++]).message("Error").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").get().build()).execute();
    Assert.assertEquals(501, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) RetryHandler(com.microsoft.rest.retry.RetryHandler) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Aggregations

RetryHandler (com.microsoft.rest.retry.RetryHandler)3 Interceptor (okhttp3.Interceptor)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 OkHttpClient (okhttp3.OkHttpClient)2 Retrofit (retrofit2.Retrofit)2 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)1 RestClient (com.microsoft.rest.RestClient)1 RequestIdHeaderInterceptor (com.microsoft.rest.interceptors.RequestIdHeaderInterceptor)1