Skip to content

fix: close the closable response. #419

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

Merged
merged 4 commits into from
Feb 3, 2021
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,6 +24,7 @@
import com.optimizely.ab.notification.NotificationCenter;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -116,10 +117,10 @@ static ProjectConfig parseProjectConfig(String datafile) throws ConfigParseExcep
@Override
protected ProjectConfig poll() {
HttpGet httpGet = createHttpRequest();

CloseableHttpResponse response = null;
logger.debug("Fetching datafile from: {}", httpGet.getURI());
try {
HttpResponse response = httpClient.execute(httpGet);
response = httpClient.execute(httpGet);
String datafile = getDatafileFromResponse(response);
if (datafile == null) {
return null;
Expand All @@ -128,6 +129,15 @@ protected ProjectConfig poll() {
} catch (ConfigParseException | IOException e) {
logger.error("Error fetching datafile", e);
}
finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
logger.warn(e.getLocalizedMessage());
}
}
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeUnit;

Expand All @@ -48,6 +49,17 @@
@RunWith(MockitoJUnitRunner.class)
public class HttpProjectConfigManagerTest {

static class MyResponse extends BasicHttpResponse implements CloseableHttpResponse {

public MyResponse(ProtocolVersion protocolVersion, Integer status, String body) {
super(protocolVersion, status, body);
}

@Override
public void close() throws IOException {

}
}
@Mock
private OptimizelyHttpClient mockHttpClient;

Expand Down Expand Up @@ -246,7 +258,7 @@ public void testInvalidBlockingTimeout() {
@Ignore
public void testGetDatafileHttpResponse2XX() throws Exception {
String modifiedStamp = "Wed, 24 Apr 2019 07:07:07 GMT";
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 200, "TEST");
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 200, "TEST");
getResponse.setEntity(new StringEntity(datafileString));
getResponse.setHeader(HttpHeaders.LAST_MODIFIED, modifiedStamp);

Expand All @@ -260,15 +272,15 @@ public void testGetDatafileHttpResponse2XX() throws Exception {

@Test(expected = ClientProtocolException.class)
public void testGetDatafileHttpResponse3XX() throws Exception {
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 300, "TEST");
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 300, "TEST");
getResponse.setEntity(new StringEntity(datafileString));

projectConfigManager.getDatafileFromResponse(getResponse);
}

@Test
public void testGetDatafileHttpResponse304() throws Exception {
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 304, "TEST");
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 304, "TEST");
getResponse.setEntity(new StringEntity(datafileString));

String datafile = projectConfigManager.getDatafileFromResponse(getResponse);
Expand All @@ -277,15 +289,15 @@ public void testGetDatafileHttpResponse304() throws Exception {

@Test(expected = ClientProtocolException.class)
public void testGetDatafileHttpResponse4XX() throws Exception {
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 400, "TEST");
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 400, "TEST");
getResponse.setEntity(new StringEntity(datafileString));

projectConfigManager.getDatafileFromResponse(getResponse);
}

@Test(expected = ClientProtocolException.class)
public void testGetDatafileHttpResponse5XX() throws Exception {
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 500, "TEST");
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 500, "TEST");
getResponse.setEntity(new StringEntity(datafileString));

projectConfigManager.getDatafileFromResponse(getResponse);
Expand Down