@Test
  public void readAtLeastUsingSmallAppBufferAtWriteLimit() throws Exception {
    final int amountToRead = 150;
    final int individualRead = 150;

    int initialUnwrappedBufferSize = 100;
    final int preexistingBytes = initialUnwrappedBufferSize - 7;
    ByteBuffer wrappedBuffer = ByteBuffer.allocate(1000);
    SocketChannel mockChannel = mock(SocketChannel.class);

    // force buffer expansion by making a small decoded buffer appear near to being full
    ByteBuffer unwrappedBuffer = ByteBuffer.allocate(initialUnwrappedBufferSize);
    unwrappedBuffer.position(7).limit(preexistingBytes + 7); // 7 bytes of message header - ignored
    nioSslEngine.getInputBufferVendorForTestingOnly().setBufferForTestingOnly(unwrappedBuffer);

    // simulate some socket reads
    when(mockChannel.read(any(ByteBuffer.class))).thenAnswer((Answer<Integer>) invocation -> {
      ByteBuffer buffer = invocation.getArgument(0);
      buffer.position(buffer.position() + individualRead);
      return individualRead;
    });

    TestSSLEngine testSSLEngine = new TestSSLEngine();
    testSSLEngine.addReturnResult(
        new SSLEngineResult(BUFFER_OVERFLOW, NEED_UNWRAP, 0, 0),
        new SSLEngineResult(BUFFER_OVERFLOW, NEED_UNWRAP, 0, 0),
        new SSLEngineResult(BUFFER_OVERFLOW, NEED_UNWRAP, 0, 0),
        new SSLEngineResult(OK, NEED_UNWRAP, 0, 0));
    nioSslEngine.engine = testSSLEngine;

    try (final ByteBufferSharing sharedBuffer =
        nioSslEngine.readAtLeast(mockChannel, amountToRead, wrappedBuffer)) {
      ByteBuffer data = sharedBuffer.getBuffer();
      verify(mockChannel, times(1)).read(isA(ByteBuffer.class));
      assertThat(data.position()).isEqualTo(0);
      assertThat(data.limit())
          .isEqualTo(individualRead * testSSLEngine.getNumberOfUnwraps() + preexistingBytes);
    }
  }
This tests the case where a message header has been read and part of a message has been read,
but the decoded buffer is too small to hold all of the message. In this case the buffer is
completely full and should only take one overflow response to resolve the problem.
geode-core/src/test/java/org/apache/geode/internal/net/NioSslEngineTest.java