ExtendedReadTimeoutHandler srvTimeoutHandler =
(ExtendedReadTimeoutHandler)lastSrvConnPipeline.get(
ExtendedReadTimeoutHandler.class.getSimpleName());
SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader)lastSrvConnPipeline.get(
SimpleTestMessageReader.class.getSimpleName());
ExceptionListenerTestHandler srvExceptionListener =
(ExceptionListenerTestHandler)lastSrvConnPipeline.get(
ExceptionListenerTestHandler.class.getSimpleName());
//hook in to key places in the client pipeline
ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
ExtendedReadTimeoutHandler clientTimeoutHandler =
(ExtendedReadTimeoutHandler)clientPipeline.get(
ExtendedReadTimeoutHandler.class.getSimpleName());
SimpleTestMessageReader clientMsgReader = (SimpleTestMessageReader)clientPipeline.get(
SimpleTestMessageReader.class.getSimpleName());
ExceptionListenerTestHandler clientExceptionListener =
(ExceptionListenerTestHandler)clientPipeline.get(
ExceptionListenerTestHandler.class.getSimpleName());
for (int i = 0; i < 50; ++i)
{
//send a request
ChannelBuffer msg = ChannelBuffers.wrappedBuffer(("hello" + i).getBytes(Charset.defaultCharset()));
clientConn.getChannel().write(msg);
//wait for the request to propagate
try {Thread.sleep(10);} catch (InterruptedException ie){};
Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout: " + i);
Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout: " + i);
Assert.assertEquals(srvMsgReader.getMsg(), "hello" + i, "message read: " + i);
//start server read timeout after the first message
if (0 == i) srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
}
ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
clientConn.getChannel().write(msg2);
//start the client timeout handler
clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
Assert.assertEquals(srvMsgReader.getMsg(), "eom", "message read");
//stop server read timeout
srvTimeoutHandler.stop();
for (int i = 0; i < 100; ++i)
{
String responseString = "hi there " + i;
ChannelBuffer resp = ChannelBuffers.wrappedBuffer(responseString.getBytes(Charset.defaultCharset()));
lastSrvConnPipeline.getChannel().write(resp);
//wait for the response to propagate
try {Thread.sleep(10);} catch (InterruptedException ie){};
Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout: " + i);
Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout: " + i);
Assert.assertEquals(clientMsgReader.getMsg(), responseString, "response read: " + i);
}
ChannelBuffer resp2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
lastSrvConnPipeline.getChannel().write(resp2);
//wait for the response to propagate
try {Thread.sleep(10);} catch (InterruptedException ie){};
Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
Assert.assertEquals(clientMsgReader.getMsg(), "eom", "response read");
//stop client read timeout
clientTimeoutHandler.stop();