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

Close streams on error #1324

Merged
merged 2 commits into from
Jun 19, 2019
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 @@ -434,6 +434,9 @@ private HttpResponse resumableUpload(GenericUrl initiationRequestUrl) throws IOE
}

if (response.getStatusCode() != 308) {
if (mediaContent.getCloseInputStream()) {
contentInputStream.close();
}
returningResponse = true;
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public void subtestUpload_ResumableWithError(ErrorType error, int contentLength,
fakeTransport.force308OnRangeQueryResponse = force308OnRangeQueryResponse;
byte[] testedData = new byte[contentLength];
new Random().nextBytes(testedData);
InputStream is = new ByteArrayInputStream(testedData);
TestingInputStream is = new TestingInputStream(testedData);
InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
if (contentLengthKnown) {
mediaContent.setLength(contentLength);
Expand All @@ -782,6 +782,7 @@ public void subtestUpload_ResumableWithError(ErrorType error, int contentLength,
assertEquals(calls, fakeTransport.lowLevelExecCalls);

assertTrue(Arrays.equals(testedData, fakeTransport.bytesReceived));
assertTrue(is.isClosed);
}

public void testUploadIOException_WithoutIOExceptionHandler() throws Exception {
Expand Down Expand Up @@ -1169,4 +1170,80 @@ public void testResumableSlowUpload() throws Exception {
uploader.setDirectUploadEnabled(false);
uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
}


static class ResumableErrorMediaTransport extends MockHttpTransport {

ResumableErrorMediaTransport() {}

@Override
public boolean supportsMethod(String method) throws IOException {
return true;
}

@Override
public LowLevelHttpRequest buildRequest(final String method, String url) {
// First request should be to the resumable request url
if (method.equals("POST")) {
assertEquals(TEST_RESUMABLE_REQUEST_URL, url);

return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
assertEquals(TEST_CONTENT_TYPE, getFirstHeaderValue("x-upload-content-type"));

// This is the initiation call.
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
// Return 200 with the upload URI.
response.setStatusCode(200);
response.addHeader("Location", TEST_UPLOAD_URL);
return response;
}
};
}

// Fake an error when uploading chunks
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(500);
return response;
}
};
}
}

class TestingInputStream extends ByteArrayInputStream {
boolean isClosed;

TestingInputStream(byte[] testData) {
super(testData);
}

@Override
public void close() throws IOException {
isClosed = true;
super.close();
}
}

public void testResumable_BadResponse() throws IOException {
int contentLength = 3;
ResumableErrorMediaTransport fakeTransport = new ResumableErrorMediaTransport();
byte[] testedData = new byte[contentLength];
new Random().nextBytes(testedData);
TestingInputStream is = new TestingInputStream(testedData);
InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
mediaContent.setLength(contentLength);
MediaHttpUploader uploader =
new MediaHttpUploader(mediaContent, fakeTransport, new ZeroBackOffRequestInitializer());

// disable GZip - so we would be able to test byte received by server.
uploader.setDisableGZipContent(true);
HttpResponse response = uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
assertEquals(500, response.getStatusCode());

assertTrue("input stream should be closed", is.isClosed);
}
}