Hi
I am new to Netty (using 4.0.24-Final) so I am probably missing something vital. I am trying to write a JUnit test to check that I can handle a client that simply dies in mid-transmission. I am simulating this by sending a short message that never completes. I was expecting the ReadTimeoutHandler to fire and the ExceptionHandler would close the connection. However, I can see through debug that the ReadTimeoutHandler never fires and the ExceptionHandler is never called.
This is my unit test
@Test
public void ifMessageDataLengthIsLessThanLengthDeclaredThenConnectionIsClosed() {
int MAX_EXPECTED_MESSAGE_LENGTH_IN_BYTES = 20;
int TIME_OUT_IN_SECS = 4;
byte[] bytes = new byte[] {0x09, 0x01, 0x02, 0x03, 0x04, 0x05};
EmbeddedChannel channel = new EmbeddedChannel(new ReadTimeoutHandler(TIME_OUT_IN_SECS)
, new LengthFieldBasedFrameDecoder(MAX_EXPECTED_MESSAGE_LENGTH_IN_BYTES,0,1,0,1)
, new ExceptionHandler());
channel.writeInbound(Unpooled.wrappedBuffer(bytes));
assertTrue(channel.isOpen());
sleepForMilliSecs((TIME_OUT_IN_SECS + 2) * 1000);
assertTrue(!channel.isOpen());
}
private void sleepForMilliSecs(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}
and my ExceptionHandler
public class ExceptionHandler extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("Exception handler is called with " + cause);
ctx.close();
}
}