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());
}
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());
}
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());
}
Aggregations