Skip to content

Commit

Permalink
Close InputStream on error during resumable upload (#1324)
Browse files Browse the repository at this point in the history
* Close streams on error

* Add a test
  • Loading branch information
SUPERCILEX authored and chingor13 committed Jun 19, 2019
1 parent 15f6916 commit 2c21249
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
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);
}
}

0 comments on commit 2c21249

Please sign in to comment.