Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore SocketExceptions when attempting to connect to cloud providers #80

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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