acceptor.setSessionRecycler(recycler);
acceptor.bind(new InetSocketAddress(port));
try {
connector.setHandler(connectorHandler);
ConnectFuture future = connector.connect(new InetSocketAddress(
"localhost", port));
future.awaitUninterruptibly();
// Write whatever to trigger the acceptor.
future.getSession().write(IoBuffer.allocate(1)).awaitUninterruptibly();
// Make sure the connection is closed before recycler closes it.
while (acceptorHandler.session == null) {
Thread.yield();
}
acceptorHandler.session.close(true);
assertTrue(
acceptorHandler.session.getCloseFuture().awaitUninterruptibly(3000));
IoSession oldSession = acceptorHandler.session;
// Wait until all events are processed and clear the state.
long startTime = System.currentTimeMillis();
while (acceptorHandler.result.length() < 8) {
Thread.yield();
if (System.currentTimeMillis() - startTime > 5000) {
throw new Exception();
}
}
acceptorHandler.result.setLength(0);
acceptorHandler.session = null;
// Write whatever to trigger the acceptor again.
WriteFuture wf = future.getSession().write(
IoBuffer.allocate(1)).awaitUninterruptibly();
assertTrue(wf.isWritten());
// Make sure the connection is closed before recycler closes it.
while (acceptorHandler.session == null) {
Thread.yield();
}
acceptorHandler.session.close(true);
assertTrue(
acceptorHandler.session.getCloseFuture().awaitUninterruptibly(3000));
future.getSession().close(true).awaitUninterruptibly();
assertNotSame(oldSession, acceptorHandler.session);
} finally {
acceptor.unbind();
}