NioSslEngine implements NioFilter { @Override public synchronized void close(SocketChannel socketChannel) { if (closed) { return; } try { if (!engine.isOutboundDone()) { ByteBuffer empty = ByteBuffer.wrap(new byte[0]); engine.closeOutbound(); myNetData.clear(); SSLEngineResult result = engine.wrap(empty, myNetData); if (result.getStatus() != SSLEngineResult.Status.CLOSED) { throw new SSLHandshakeException( "Error closing SSL session. Status=" + result.getStatus()); } myNetData.flip(); while (myNetData.hasRemaining()) { socketChannel.write(myNetData); } } } catch (ClosedChannelException e) { } catch (IOException e) { throw new GemFireIOException("exception closing SSL session", e); } finally { bufferPool.releaseBuffer(TRACKED_SENDER, myNetData); bufferPool.releaseBuffer(TRACKED_RECEIVER, peerAppData); myNetData = null; this.closed = true; } } NioSslEngine(SSLEngine engine, BufferPool bufferPool); boolean handshake(SocketChannel socketChannel, int timeout,
      ByteBuffer peerNetData); @Override synchronized ByteBuffer wrap(ByteBuffer appData); @Override synchronized ByteBuffer unwrap(ByteBuffer wrappedBuffer); @Override ByteBuffer ensureWrappedCapacity(int amount, ByteBuffer wrappedBuffer,
      BufferType bufferType); @Override ByteBuffer readAtLeast(SocketChannel channel, int bytes,
      ByteBuffer wrappedBuffer); @Override ByteBuffer getUnwrappedBuffer(ByteBuffer wrappedBuffer); ByteBuffer ensureUnwrappedCapacity(int amount); @Override void doneReadingDirectAck(ByteBuffer unwrappedBuffer); @Override synchronized void close(SocketChannel socketChannel);  }
@Test public void close() throws Exception { SocketChannel mockChannel = mock(SocketChannel.class); Socket mockSocket = mock(Socket.class); when(mockChannel.socket()).thenReturn(mockSocket); when(mockSocket.isClosed()).thenReturn(false); when(mockEngine.isOutboundDone()).thenReturn(Boolean.FALSE); when(mockEngine.wrap(any(ByteBuffer.class), any(ByteBuffer.class))).thenReturn( new SSLEngineResult(CLOSED, FINISHED, 0, 0)); nioSslEngine.close(mockChannel); assertThatThrownBy(() -> nioSslEngine.checkClosed()).isInstanceOf(IOException.class) .hasMessageContaining("NioSslEngine has been closed"); nioSslEngine.close(mockChannel); }