@Test public void shouldClearNotOwnedInodeCacheToPreventMemoryLeak() throws Exception { final int initialNotOwnedInode = 0; for(int i = 0; i < SocketOwnedByCurrentProcessFilter.MAX_NOT_OWNED_INODE_CACHE_SIZE + 1; i++) { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, i, 0L, 0L, 0L); } socketInodeRequestCount = 0; filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, initialNotOwnedInode, 0L, 0L, 0L); assertThat(socketInodeRequestCount, is(1)); }
/** * {@inheritDoc} */ @Override public void onStatisticsUpdated(final InetAddress inetAddress, final int port, final long socketIdentifier, final long inode, final long receiveQueueDepth, final long transmitQueueDepth, final long drops) { if(socketInodesNotOwnedByThisProcess.contains(inode)) { return; } if(!socketInodesOwnedByThisProcess.contains(inode)) { socketInodeRetriever.accept(socketInodesOwnedByThisProcess); if(!socketInodesOwnedByThisProcess.contains(inode)) { clearNotOwnedInodeCacheIfTooLarge(); socketInodesNotOwnedByThisProcess.add(inode); } } if(socketInodesOwnedByThisProcess.contains(inode)) { delegate.onStatisticsUpdated(inetAddress, port, socketIdentifier, inode, receiveQueueDepth, transmitQueueDepth, drops); } }
@Test public void shouldNotRequestInodeUpdateOfNotOwnedInodeWhenInodeHasBeenRequestedBefore() throws Exception { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_NOT_OWNED_BY_PROCESS, 0L, 0L, 0L); filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_NOT_OWNED_BY_PROCESS, 0L, 0L, 0L); assertThat(receivedUpdateCount, is(0)); assertThat(socketInodeRequestCount, is(1)); }
@Test public void shouldNotRequestInodeUpdateIfInodeIsAlreadyKnown() throws Exception { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_OWNED_BY_PROCESS, 0L, 0L, 0L); filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_OWNED_BY_PROCESS, 0L, 0L, 0L); assertThat(receivedUpdateCount, is(2)); assertThat(socketInodeRequestCount, is(1)); }
@Test public void shouldNotNotifyDelegateIfInodeIsNotSocketOwnedByCurrentProcess() throws Exception { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_NOT_OWNED_BY_PROCESS, 0L, 0L, 0L); assertThat(receivedUpdateCount, is(0)); }
@Test public void shouldNotifyDelegateIfInodeIsSocketOwnedByCurrentProcess() throws Exception { filter.onStatisticsUpdated(INET_ADDRESS, PORT, 17L, INODE_OWNED_BY_PROCESS, 0L, 0L, 0L); assertThat(receivedUpdateCount, is(1)); }