//hook in to key places in the server pipeline
ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
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();
final ExceptionListenerTestHandler clientExceptionListener =
(ExceptionListenerTestHandler)clientPipeline.get(
ExceptionListenerTestHandler.class.getSimpleName());
//System.err.println("Current thread: " + Thread.currentThread());
//send a request in a separate thread because the client will intentionally block to simulate
//a timeout
final ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
Thread sendThread1 = new Thread(new Runnable()
{
@Override
public void run()
{
//System.err.println(Thread.currentThread().toString() + ": sending message");
clientConn.getChannel().write(msg);
}
}, "send msg thread");
sendThread1.start();
//wait for the request to propagate
//System.err.println(Thread.currentThread().toString() + ": waiting for 10");
try {Thread.sleep(50);} catch (InterruptedException ie){};
//System.err.println(Thread.currentThread().toString() + ": done Waiting for 10");
//make sure the server has not received the message
Assert.assertNull(srvExceptionListener.getLastException(), "no server errors");
Assert.assertNull(clientExceptionListener.getLastException(), "no errors yet");
Assert.assertTrue(!"hello".equals(srvMsgReader.getMsg()), "message not read yet");
//System.err.println("Waiting for 300");
//wait for the write timeout
try {Thread.sleep(300);} catch (InterruptedException ie){};
//System.err.println("Done Waiting for 300");
//the client should have timed out and closed the connection
TestUtil.assertWithBackoff(new ConditionCheck()
{
@Override
public boolean check()
{
return null != clientExceptionListener.getLastException();
}
}, "client error", 1000, null);
Assert.assertTrue(null != clientExceptionListener.getLastException(),
"client error");
Assert.assertTrue(clientExceptionListener.getLastException() instanceof ClosedChannelException
|| clientExceptionListener.getLastException() instanceof WriteTimeoutException,
"client error test");
Assert.assertTrue(! lastSrvConnPipeline.getChannel().isConnected(), "client has disconnected");
Assert.assertTrue(! clientPipeline.getChannel().isConnected(), "closed connection to server");
}