MemorySegmentPool implements MemorySegmentSource { @Nullable @Override public MemorySegment nextSegment() { MemorySegment segment = pool.pollFirst(); if (segment != null) { return segment; } if (!hasRemainingCapacity()) { return null; } segment = MemorySegmentFactory.allocateUnpooledSegment(PAGE_SIZE); totalAllocatedMemory += PAGE_SIZE; return segment; } MemorySegmentPool(long inMemoryBufferSize); @Nullable @Override MemorySegment nextSegment();  }
@Test public void emptyMemorySegmentPoolDoesNotAllocateSegments() { MemorySegmentPool pool = new MemorySegmentPool(0); assertThat(pool.nextSegment(), nullValue()); }
@Test public void minimalAllocationUnitIsPageSize() { MemorySegmentPool pool = new MemorySegmentPool(MemorySegmentPool.PAGE_SIZE - 1); assertThat(pool.nextSegment(), CoreMatchers.nullValue()); }
@Test public void poolIsAbleToAllocateTheRequiredNumberOfPages() { final int pageCount = 10; MemorySegmentPool pool = new MemorySegmentPool(pageCount * MemorySegmentPool.PAGE_SIZE); for (int i = 0; i < pageCount; i++) { MemorySegment segment = pool.nextSegment(); assertThat(segment, notNullValue()); assertThat(segment.size(), is(MemorySegmentPool.PAGE_SIZE)); } assertThat(pool.nextSegment(), nullValue()); }