@Test
public void testSuspendResumeReadWrite() throws Exception {
ConnectFuture future = connect(port, new ClientIoHandler());
future.awaitUninterruptibly();
IoSession session = future.getSession();
// We wait for the sessionCreated() event is fired because we
// cannot guarantee that it is invoked already.
while (session.getAttribute("lock") == null) {
Thread.yield();
}
Object lock = session.getAttribute("lock");
synchronized (lock) {
write(session, "1");
assertEquals('1', read(session));
assertEquals("1", getReceived(session));
assertEquals("1", getSent(session));
session.suspendRead();
Thread.sleep(100);
write(session, "2");
assertFalse(canRead(session));
assertEquals("1", getReceived(session));
assertEquals("12", getSent(session));
session.suspendWrite();
Thread.sleep(100);
write(session, "3");
assertFalse(canRead(session));
assertEquals("1", getReceived(session));
assertEquals("12", getSent(session));
session.resumeRead();
Thread.sleep(100);
write(session, "4");
assertEquals('2', read(session));
assertEquals("12", getReceived(session));
assertEquals("12", getSent(session));
session.resumeWrite();
Thread.sleep(100);
assertEquals('3', read(session));
assertEquals('4', read(session));
write(session, "5");
assertEquals('5', read(session));
assertEquals("12345", getReceived(session));
assertEquals("12345", getSent(session));
session.suspendWrite();
Thread.sleep(100);
write(session, "6");
assertFalse(canRead(session));
assertEquals("12345", getReceived(session));
assertEquals("12345", getSent(session));
session.suspendRead();
session.resumeWrite();
Thread.sleep(100);
write(session, "7");
assertFalse(canRead(session));
assertEquals("12345", getReceived(session));
assertEquals("1234567", getSent(session));
session.resumeRead();
Thread.sleep(100);
assertEquals('6', read(session));
assertEquals('7', read(session));
assertEquals("1234567", getReceived(session));
assertEquals("1234567", getSent(session));
}
session.close(true).awaitUninterruptibly();
}