@Override
    public void reserveSegments(int numberOfSegmentsToReserve) throws IOException {
        checkArgument(
                numberOfSegmentsToReserve <= numberOfRequiredMemorySegments,
                "Can not reserve more segments than number of required segments.");

        CompletableFuture<?> toNotify = null;
        synchronized (availableMemorySegments) {
            checkDestroyed();

            if (numberOfRequestedMemorySegments < numberOfSegmentsToReserve) {
                availableMemorySegments.addAll(
                        networkBufferPool.requestPooledMemorySegmentsBlocking(
                                numberOfSegmentsToReserve - numberOfRequestedMemorySegments));
                toNotify = availabilityHelper.getUnavailableToResetAvailable();
            }
        }
        mayNotifyAvailable(toNotify);
    }
@Test
    void testReserveSegments() throws Exception {
        NetworkBufferPool networkBufferPool =
                new NetworkBufferPool(2, memorySegmentSize, Duration.ofSeconds(2));
        try {
            BufferPool bufferPool1 = networkBufferPool.createBufferPool(1, 2);
            assertThatThrownBy(() -> bufferPool1.reserveSegments(2))
                    .isInstanceOf(IllegalArgumentException.class);

            // request all buffers
            ArrayList<Buffer> buffers = new ArrayList<>(2);
            buffers.add(bufferPool1.requestBuffer());
            buffers.add(bufferPool1.requestBuffer());
            assertThat(buffers).hasSize(2);

            BufferPool bufferPool2 = networkBufferPool.createBufferPool(1, 10);
            assertThatThrownBy(() -> bufferPool2.reserveSegments(1))
                    .isInstanceOf(IOException.class);
            assertThat(bufferPool2.isAvailable()).isFalse();

            buffers.forEach(Buffer::recycleBuffer);
            bufferPool1.lazyDestroy();
            bufferPool2.lazyDestroy();

            BufferPool bufferPool3 = networkBufferPool.createBufferPool(2, 10);
            assertThat(bufferPool3.getNumberOfAvailableMemorySegments()).isOne();
            bufferPool3.reserveSegments(2);
            assertThat(bufferPool3.getNumberOfAvailableMemorySegments()).isEqualTo(2);

            bufferPool3.lazyDestroy();
            assertThatThrownBy(() -> bufferPool3.reserveSegments(1))
                    .isInstanceOf(CancelTaskException.class);
        } finally {
            networkBufferPool.destroy();
        }
    }