Skip to content

Commit

Permalink
Merge pull request #80 from newrelic/cloud_utility_fix
Browse files Browse the repository at this point in the history
ignore SocketExceptions when attempting to connect to cloud providers
  • Loading branch information
gfuller1 authored Oct 20, 2020
2 parents 014584e + 00ecb3a commit c504efc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class CloudUtility {

// Spec: All characters should be in the following character class: over U+007F
private static final int MIN_CHAR_CODEPOINT = "\u007F".codePointAt(0);
private final Function<Integer,CloseableHttpClient> httpClientCreator;
private final Function<Integer, CloseableHttpClient> httpClientCreator;

public CloudUtility() {
this(new Function<Integer, CloseableHttpClient>() {
Expand All @@ -58,7 +59,8 @@ public String httpPut(String url, int requestTimeoutMillis, String... headers) t
return makeHttpRequest(httpPut, requestTimeoutMillis, headers);
}

private String makeHttpRequest(HttpUriRequest request, int requestTimeoutMillis, String[] headers) throws IOException {
private String makeHttpRequest(HttpUriRequest request, int requestTimeoutMillis, String[] headers)
throws IOException {
try (CloseableHttpClient httpclient = httpClientCreator.apply(requestTimeoutMillis)) {
for (String header : headers) {
String[] parts = header.split(":");
Expand All @@ -70,7 +72,7 @@ private String makeHttpRequest(HttpUriRequest request, int requestTimeoutMillis,
if (response.getStatusLine().getStatusCode() <= HttpStatus.SC_MULTI_STATUS) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (ConnectTimeoutException | UnknownHostException | SocketTimeoutException ignored) {
} catch (ConnectTimeoutException | UnknownHostException | SocketTimeoutException | SocketException ignored) {
// we expect these values in situations where there is no cloud provider, or
// we're on a different cloud provider than expected.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.net.SocketException;
import java.net.SocketTimeoutException;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -111,6 +112,19 @@ public void testSocketTimeoutExceptionIsHandled() throws Exception {
assertNull(result);
}

@Test
public void testSocketExceptionIsHandled() throws Exception {
Function<Integer, CloseableHttpClient> mockClientCreator = mock(Function.class);
CloseableHttpClient explodingClient = mock(CloseableHttpClient.class);

when(mockClientCreator.apply(anyInt())).thenReturn(explodingClient);
when(explodingClient.execute(isA(HttpUriRequest.class))).thenThrow(new SocketException("oof"));

CloudUtility testClass = new CloudUtility(mockClientCreator);
String result = testClass.httpGet("https://example.com/some/path", 12, "foo:bar");
assertNull(result);
}

@Before
public void before() {
MockServiceManager mockServiceManager = new MockServiceManager();
Expand Down

0 comments on commit c504efc

Please sign in to comment.