2016-07-16 10 views
0

Java 8とNetty 4.1.1.Finalを使用して、次のテストケースが成功すると予想しましたが、タイムアウトします。私がw.r.tを理解していないのは何ですか? nettysイベントループとタスクのスケジュール?Nettyがスケジュールされたタスクを実行しないのはなぜですか?

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(); 
} 

} 

答えて

2

これはEmbeddedChannelが「実際の」チャネル実装ではなく、主にテストや組み込みChannelHandlerで使用できるためです。実行させるには、所定の時間枠の後に "runPendingTasks()"を呼び出す必要があります。 「本当の」Channel実装を使用すると、余分なメソッド呼び出しがなくても動作します。

+0

OK。 LocalAddressとBootstrapのペアを設定しなくても、LocalChannelでパイプラインを使用する「安価な」方法だと思いました。 –

関連する問題