ReadTimeoutHandler not firing

39 views
Skip to first unread message

Roger Varley

unread,
Mar 27, 2017, 3:20:45 AM3/27/17
to Netty discussions
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();
    }

}

Norman Maurer

unread,
Mar 27, 2017, 8:25:29 AM3/27/17
to ne...@googlegroups.com
That is because the EmbeddedChannel not really support running scheduled tasks by itself as everything is just done in the calling thread. Just call  `runPendingTasks()` after the sleep and it should have the effect you expect.

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/d2f160ee-37a7-4af8-a205-ab5787ab0dff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Roger Varley

unread,
Mar 28, 2017, 2:49:27 AM3/28/17
to Netty discussions
Thanks Norman, that worked perfectly.
Reply all
Reply to author
Forward
0 new messages