James
James

Reputation: 339

Netty - Unit testing idle state of handlers using IdleStateHandler

I'm attempting to test my own handlers, which listen to io.netty.handler.timeout.IdleStateEvent from io.netty.handler.timeout.IdleStateHandler. I'm don't see anyone else having this issue, but when I sleep in my code to trigger the idle events, the idle events are not triggering. I believe that's due to the embedded channel running on the same thread? Below is an example test.

@Test
public void userEventTriggered_IdleStateEvent() throws Exception {
    // given
    IdleStateHandler idleStateHandler = new IdleStateHandler(
            1L,0L,0L, TimeUnit.SECONDS);
    EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler, myHandler);

    // when
    TimeUnit.SECONDS.sleep(2);

    // then
    assertFalse(channel.isActive());

    channel.finish();
}

I have noticed in the unit tests for IdleStateHandler that a testable handler has been created which makes use of package-scoped methods to test the handler.

Is there any way to test the idle events from outside the package? I need to ensure coverage for these events. Any tips are appreciated. Thanks!

Upvotes: 2

Views: 629

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

You should call channel.runPendingTasks() after sleeping. This should execute the scheduled tasks that are now ready.

Upvotes: 3

Related Questions