Code review comment: This should not be true unless disconnect is being called somewhere. 

Code before review:
public void testGetSizeBytesWhenFileNotFoundBatchRetry() throws Exception {
JsonFactory jsonFactory = new JacksonFactory();
String contentBoundary = "batch_foobarbaz";
String contentBoundaryLine = "--" + contentBoundary;
String endOfContentBoundaryLine = "--" + contentBoundary + "--";
GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
error.setFactory(jsonFactory);
String content =
contentBoundaryLine
+ "\n"
+ "Content-Type: application/http\n"
+ "\n"
+ "HTTP/1.1 404 Not Found\n"
+ "Content-Length: -1\n"
+ "\n"
+ error.toString()
+ "\n"
+ "\n"
+ endOfContentBoundaryLine
+ "\n";
thrown.expect(FileNotFoundException.class);
final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class);
when(mockResponse.getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);
when(mockResponse.getStatusCode()).thenReturn(429, 200);
when(mockResponse.getContent()).thenReturn(toStream(content));
MockHttpTransport mockTransport =
new MockHttpTransport.Builder()
.setLowLevelHttpRequest(
new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
return mockResponse;
}
})
.build();
GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
gcsUtil.setStorageClient(
new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}


Code after addressing the review:
public void testGetSizeBytesWhenFileNotFoundBatchRetry() throws Exception {
JsonFactory jsonFactory = new JacksonFactory();
String contentBoundary = "batch_foobarbaz";
String contentBoundaryLine = "--" + contentBoundary;
String endOfContentBoundaryLine = "--" + contentBoundary + "--";
GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
error.setFactory(jsonFactory);
String content =
contentBoundaryLine
+ "\n"
+ "Content-Type: application/http\n"
+ "\n"
+ "HTTP/1.1 404 Not Found\n"
+ "Content-Length: -1\n"
+ "\n"
+ error.toString()
+ "\n"
+ "\n"
+ endOfContentBoundaryLine
+ "\n";
thrown.expect(FileNotFoundException.class);
final LowLevelHttpResponse[] mockResponses =
new LowLevelHttpResponse[] {
Mockito.mock(LowLevelHttpResponse.class), Mockito.mock(LowLevelHttpResponse.class),
};
when(mockResponses[0].getContentType()).thenReturn("text/plain");
when(mockResponses[1].getContentType())
.thenReturn("multipart/mixed; boundary=" + contentBoundary);
when(mockResponses[0].getStatusCode()).thenReturn(429);
when(mockResponses[1].getStatusCode()).thenReturn(200);
when(mockResponses[0].getContent()).thenReturn(toStream("error"));
when(mockResponses[1].getContent()).thenReturn(toStream(content));
MockHttpTransport mockTransport =
new MockHttpTransport.Builder()
.setLowLevelHttpRequest(
new MockLowLevelHttpRequest() {
int index = 0;
@Override
public LowLevelHttpResponse execute() throws IOException {
return mockResponses[index++];
}
})
.build();
GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
gcsUtil.setStorageClient(
new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}