예제 #1
0
        public async Task TestChannelInitializerReentrance()
        {
            AtomicInteger         registeredCalled  = new AtomicInteger(0);
            ChannelHandlerAdapter handler1          = new ChannelInboundHandlerAdapter0(registeredCalled);
            AtomicInteger         initChannelCalled = new AtomicInteger(0);

            _client.Handler(new ActionChannelInitializer <IChannel>(ch =>
            {
                initChannelCalled.Increment();
                ch.Pipeline.AddLast(handler1);
                ch.Pipeline.FireChannelRegistered();
            })).LocalAddress(LocalAddress.Any);

            var channel = await _client.BindAsync();

            try
            {
                // Execute some task on the EventLoop and wait until its done to be sure all handlers are added to the
                // pipeline.
                await channel.EventLoop.SubmitAsync(() =>
                {
                    // NOOP
                    return(0);
                });

                Assert.Equal(1, initChannelCalled.Value);
                Assert.Equal(2, registeredCalled.Value);
            }
            finally
            {
                await channel.CloseAsync();
            }
        }
예제 #2
0
        public async Task TestRemoveFlowControl()
        {
            CountdownEvent latch = new CountdownEvent(3);

            ChannelHandlerAdapter handler = new TestHandler(
                onActive: ctx =>
            {
                //do the first read
                ctx.Read();
                ctx.FireChannelActive();
            },
                onRead: (ctx, msg) =>
            {
                Signal(latch);
                ctx.FireChannelRead(msg);
            }
                );

            FlowControlHandler    flow = new FlowControlHandler0();
            ChannelHandlerAdapter tail = new ChannelInboundHandlerAdapter0();

            IChannel server = await NewServer(false /* no auto read */, flow, handler, tail);

            IChannel client = await NewClient(server.LocalAddress);

            try
            {
                // Write one message
                await client.WriteAndFlushAsync(NewOneMessage());

                // We should receive 3 messages
                Assert.True(latch.Wait(TimeSpan.FromSeconds(1)));
                Assert.True(flow.IsQueueEmpty);
            }
            finally
            {
                Task.WhenAll(client.CloseAsync(), server.CloseAsync()).Wait(TimeSpan.FromSeconds(5));
            }

            void Signal(CountdownEvent evt)
            {
                if (!evt.IsSet)
                {
                    evt.Signal();
                }
            }
        }
        public void TestExceptionCaughtBothCombinedHandlers()
        {
            Exception exception           = new Exception();
            Queue <IChannelHandler> queue = new Queue <IChannelHandler>();

            var             inboundHandler  = new ChannelInboundHandlerAdapter0(exception, queue);
            var             outboundHandler = new ChannelOutboundHandlerAdapter0(exception, queue);
            var             lastHandler     = new ChannelInboundHandlerAdapter1(exception, queue);
            EmbeddedChannel channel         = new EmbeddedChannel(
                new CombinedChannelDuplexHandler <ChannelHandlerAdapter, ChannelHandlerAdapter>(
                    inboundHandler, outboundHandler), lastHandler);

            channel.Pipeline.FireExceptionCaught(exception);
            Assert.False(channel.Finish());
            Assert.Same(inboundHandler, queue.Dequeue());
            Assert.Same(outboundHandler, queue.Dequeue());
            Assert.Same(lastHandler, queue.Dequeue());
            Assert.Empty(queue);
        }