Skip to content
Snippets Groups Projects
Commit b3bab716 authored by Harshika Dhoot's avatar Harshika Dhoot Committed by David Diederich
Browse files

Adding retry for jersey client for Azure ITs in case of 502 and 503.

(cherry picked from commit 7608b5e2)
parent 37845e26
No related branches found
No related tags found
2 merge requests!416M16 delta changes cherry-pick,!346Cherry-pick 'Adding retry for jersey client for Azure ITs in case of 502 and 503.' into release/0.19
Pipeline #166458 skipped
...@@ -18,6 +18,7 @@ package org.opengroup.osdu.notification.util; ...@@ -18,6 +18,7 @@ package org.opengroup.osdu.notification.util;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.net.SocketTimeoutException;
import java.net.URL; import java.net.URL;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
...@@ -29,6 +30,7 @@ import javax.net.ssl.SSLContext; ...@@ -29,6 +30,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.Client;
...@@ -78,58 +80,88 @@ public abstract class TestUtils { ...@@ -78,58 +80,88 @@ public abstract class TestUtils {
Map<String, String> headers, boolean enforceHttp) Map<String, String> headers, boolean enforceHttp)
throws Exception { throws Exception {
ClientResponse response; ClientResponse response;
try { Client client = getClient();
Client client = getClient(); client.setConnectTimeout(300000);
client.setConnectTimeout(300000); client.setReadTimeout(300000);
client.setReadTimeout(50000); client.setFollowRedirects(false);
client.setFollowRedirects(false); String url = getApiPath(path + query, enforceHttp);
String url = getApiPath(path + query, enforceHttp); System.out.println(url);
System.out.println(url); System.out.println(httpMethod);
System.out.println(httpMethod); System.out.println(requestBody);
System.out.println(requestBody); System.out.println(headers);
System.out.println(headers);
WebResource webResource = client.resource(url);
WebResource webResource = client.resource(url); final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON)
final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON) .header("Authorization", token);
.header("Authorization", token); int retryCount = 2;
headers.forEach((k, v) -> builder.header(k, v)); headers.forEach((k, v) -> builder.header(k, v));
try{
response = builder.method(httpMethod, ClientResponse.class, requestBody); response = builder.method(httpMethod, ClientResponse.class, requestBody);
while (retryCount > 0) {
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.valueOf("SERVER_ERROR"))) {
System.out.println("got resoponse : " + response.getStatusInfo());
Thread.sleep(5000);
System.out.println("Retrying.. ");
response = builder.method(httpMethod, ClientResponse.class, requestBody);
} else
break;
retryCount--;
}
System.out.println("sending response from TestUtils send method");
return response;
} catch (Exception e) { } catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.out.println("Retrying in case of socket timeout exception");
return builder.method(httpMethod, ClientResponse.class, requestBody);
}
e.printStackTrace(); e.printStackTrace();
throw new AssertionError("Error: Send request error", e); throw new AssertionError("Error: Send request error", e);
} }
logger.info("waiting on response");
return response;
} }
public static ClientResponse send(String url, String path, String httpMethod, String token, String requestBody, public static ClientResponse send(String url, String path, String httpMethod, String token, String requestBody,
String query, Map<String, String> headers, boolean enforceHttp) String query, Map<String, String> headers, boolean enforceHttp)
throws Exception { throws Exception {
ClientResponse response; ClientResponse response;
try { Client client = getClient();
Client client = getClient(); client.setConnectTimeout(300000);
client.setConnectTimeout(300000); client.setReadTimeout(300000);
client.setReadTimeout(50000); client.setFollowRedirects(false);
client.setFollowRedirects(false); String URL = getApiPath(url, path + query, enforceHttp);
String URL = getApiPath(url, path + query, enforceHttp); System.out.println(url + path);
System.out.println(url + path); System.out.println(httpMethod);
System.out.println(httpMethod); System.out.println(requestBody);
System.out.println(requestBody); System.out.println(headers);
System.out.println(headers); WebResource webResource = client.resource(URL);
WebResource webResource = client.resource(URL); final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON);
final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON); if (!token.isEmpty()) {
if (!token.isEmpty()) { logger.info("Token is not empty so adding to request header");
logger.info("Token is not empty so adding to request header"); builder.header("Authorization", token);
builder.header("Authorization", token); }
} int retryCount = 2;
headers.forEach((k, v) -> builder.header(k, v)); headers.forEach((k, v) -> builder.header(k, v));
try{
response = builder.method(httpMethod, ClientResponse.class, requestBody); response = builder.method(httpMethod, ClientResponse.class, requestBody);
while (retryCount > 0) {
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.valueOf("SERVER_ERROR"))) {
System.out.println("got resoponse : " + response.getStatusInfo());
Thread.sleep(5000);
System.out.println("Retrying.. ");
response = builder.method(httpMethod, ClientResponse.class, requestBody);
} else
break;
retryCount--;
}
System.out.println("sending response from TestUtils send method");
return response;
} catch (Exception e) { } catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.out.println("Retrying in case of socket timeout exception");
return builder.method(httpMethod, ClientResponse.class, requestBody);
}
e.printStackTrace(); e.printStackTrace();
throw new AssertionError("Error: Send request error", e); throw new AssertionError("Error: Send request error", e);
} }
logger.info("waiting on response");
return response;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment