From fc703c4f76512ffe7a3caffc0dde0f32f900938f Mon Sep 17 00:00:00 2001 From: Tomas Celaya Date: Wed, 10 Jan 2018 16:52:46 -0800 Subject: [PATCH] Resolve #400 by closing the iterator before returning early --- .../com/joyent/manta/client/MantaClient.java | 1 + .../joyent/manta/client/MantaClientTest.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/java-manta-client-unshaded/src/main/java/com/joyent/manta/client/MantaClient.java b/java-manta-client-unshaded/src/main/java/com/joyent/manta/client/MantaClient.java index 8d835804..d9e18bf0 100644 --- a/java-manta-client-unshaded/src/main/java/com/joyent/manta/client/MantaClient.java +++ b/java-manta-client-unshaded/src/main/java/com/joyent/manta/client/MantaClient.java @@ -773,6 +773,7 @@ public Stream listObjects(final String path) throws IOException { */ try { if (!itr.hasNext()) { + itr.close(); return Stream.empty(); } } catch (UncheckedIOException e) { diff --git a/java-manta-client-unshaded/src/test/java/com/joyent/manta/client/MantaClientTest.java b/java-manta-client-unshaded/src/test/java/com/joyent/manta/client/MantaClientTest.java index 61530052..fc61f250 100644 --- a/java-manta-client-unshaded/src/test/java/com/joyent/manta/client/MantaClientTest.java +++ b/java-manta-client-unshaded/src/test/java/com/joyent/manta/client/MantaClientTest.java @@ -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); @@ -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 listing = clientSpy.listObjects("/"); + listing.close(); + + verify(iteratorMock).close(); + } }