Reputation: 12817
Using Java 8 and Netty 4.1.1.Final, I would've expected the following test case to succeed, but it times out. What is it that I don't understand w.r.t. nettys event loop and scheduling of tasks?
public class SchedulerTest {
CountDownLatch latch;
TimerHandler handler;
static class TimerHandler extends ChannelInboundHandlerAdapter {
ChannelHandlerContext ctx;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
this.ctx = ctx;
}
private void timeout(final long ms) {
ctx.executor().schedule(() -> {
ctx.fireUserEventTriggered(ms);
}, ms, TimeUnit.MILLISECONDS);
}
}
static class TimeoutReactor extends ChannelInboundHandlerAdapter {
CountDownLatch latch;
public TimeoutReactor(CountDownLatch latch) {
super();
this.latch = latch;
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println("userEventTriggered");
latch.countDown();
super.userEventTriggered(ctx, evt);
}
}
@Before
public void setUp() throws Exception {
latch = new CountDownLatch(2);
handler = new TimerHandler();
TimeoutReactor reactor = new TimeoutReactor(latch);
new EmbeddedChannel(handler, reactor);
}
@Test(timeout = 1000)
public void test() throws InterruptedException {
handler.timeout(30);
handler.timeout(20);
latch.await();
}
}
Upvotes: 0
Views: 679
Reputation: 23567
Its because EmbeddedChannel is no "real" Channel implementation and mainly use-able for testing and embedded ChannelHandlers. You will need to call "runPendingTasks()" after the given timeframe to have it run. If you use a "real" Channel implementation it will work without any extra method calls.
Upvotes: 5