Skip to content

Commit

Permalink
Resolve #400 by closing the iterator before returning early (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcelaya authored Jan 11, 2018
1 parent cdcf64e commit fa2a5e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ public Stream<MantaObject> listObjects(final String path) throws IOException {
*/
try {
if (!itr.hasNext()) {
itr.close();
return Stream.empty();
}
} catch (UncheckedIOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void teardown() {
}

@Test
public void listObjectsDoesNotLeakConnections() throws IOException {
public void listObjectsDoesNotLeakConnectionsWhenThereAreResults() throws IOException {
// BasicHttpClientConnectionManager maintains a single connection

final MantaDirectoryListingIterator iteratorMock = mock(MantaDirectoryListingIterator.class);
Expand All @@ -38,4 +38,21 @@ public void listObjectsDoesNotLeakConnections() throws IOException {

verify(iteratorMock).close();
}

@Test
public void listObjectsDoesNotLeakConnectionsWhenNoResults() throws IOException {
// BasicHttpClientConnectionManager maintains a single connection

final MantaDirectoryListingIterator iteratorMock = mock(MantaDirectoryListingIterator.class);
when(iteratorMock.hasNext()).thenReturn(false);

final MantaClient client = new MantaClient(new TestConfigContext());
final MantaClient clientSpy = spy(client);
doReturn(iteratorMock).when(clientSpy).streamingIterator(anyString());

final Stream<MantaObject> listing = clientSpy.listObjects("/");
listing.close();

verify(iteratorMock).close();
}
}

0 comments on commit fa2a5e3

Please sign in to comment.