DirectBufferPool { public long availableMemory() { lock.lock(); try { return this.availableMemory + this.free.size() * this.poolableSize; } finally { lock.unlock(); } } DirectBufferPool(long memory, int poolableSize, boolean blockOnExhaustion); ByteBuffer allocate(int size); void deallocate(ByteBuffer buffer, int size); void deallocate(ByteBuffer buffer); long availableMemory(); long unallocatedMemory(); int queued(); int poolableSize(); long totalMemory();  }
@Test public void shouldSurviveHammeringOfLotsOfThreadsOnBufferPool() throws Exception { int numThreads = 10; final int iterations = 50000; final int poolableSize = 1024; final int totalMemory = numThreads / 2 * poolableSize; final DirectBufferPool pool = new DirectBufferPool(totalMemory, poolableSize, true); List<StressTestThread> threads = IntStream.range(0, numThreads) .mapToObj(i -> new StressTestThread(pool, iterations)) .collect(Collectors.toList()); threads.forEach(StressTestThread::start); threads.forEach(StressTestThread::joinQuietly); threads.forEach(thread -> assertTrue("Thread should have completed all iterations successfully.", thread.success.get())); assertEquals(totalMemory, pool.availableMemory()); }