|
| 1 | +package com.joyent.manta.client; |
| 2 | + |
| 3 | +import com.joyent.manta.config.TestConfigContext; |
| 4 | +import org.mockito.Mockito; |
| 5 | +import org.testng.annotations.AfterTest; |
| 6 | +import org.testng.annotations.Test; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import static org.mockito.Matchers.anyString; |
| 12 | +import static org.mockito.Mockito.doReturn; |
| 13 | +import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.spy; |
| 15 | +import static org.mockito.Mockito.verify; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +public class MantaClientTest { |
| 19 | + |
| 20 | + @AfterTest |
| 21 | + public void teardown() { |
| 22 | + Mockito.validateMockitoUsage(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void listObjectsDoesNotLeakConnectionsWhenThereAreResults() throws IOException { |
| 27 | + // BasicHttpClientConnectionManager maintains a single connection |
| 28 | + |
| 29 | + final MantaDirectoryListingIterator iteratorMock = mock(MantaDirectoryListingIterator.class); |
| 30 | + when(iteratorMock.hasNext()).thenReturn(true); |
| 31 | + |
| 32 | + final MantaClient client = new MantaClient(new TestConfigContext()); |
| 33 | + final MantaClient clientSpy = spy(client); |
| 34 | + doReturn(iteratorMock).when(clientSpy).streamingIterator(anyString()); |
| 35 | + |
| 36 | + final Stream<MantaObject> listing = clientSpy.listObjects("/"); |
| 37 | + listing.close(); |
| 38 | + |
| 39 | + verify(iteratorMock).close(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void listObjectsDoesNotLeakConnectionsWhenNoResults() throws IOException { |
| 44 | + // BasicHttpClientConnectionManager maintains a single connection |
| 45 | + |
| 46 | + final MantaDirectoryListingIterator iteratorMock = mock(MantaDirectoryListingIterator.class); |
| 47 | + when(iteratorMock.hasNext()).thenReturn(false); |
| 48 | + |
| 49 | + final MantaClient client = new MantaClient(new TestConfigContext()); |
| 50 | + final MantaClient clientSpy = spy(client); |
| 51 | + doReturn(iteratorMock).when(clientSpy).streamingIterator(anyString()); |
| 52 | + |
| 53 | + final Stream<MantaObject> listing = clientSpy.listObjects("/"); |
| 54 | + listing.close(); |
| 55 | + |
| 56 | + verify(iteratorMock).close(); |
| 57 | + } |
| 58 | +} |
0 commit comments