Esempio n. 1
0
        private static async Task TestAutoReadOffDuringReadOnlyReadsOneTime0(bool readOutsideEventLoopThread,
                                                                             ServerBootstrap sb, Bootstrap cb)
        {
            IChannel serverChannel = null;
            IChannel clientChannel = null;

            try
            {
                AutoReadInitializer serverInitializer = new AutoReadInitializer(!readOutsideEventLoopThread);
                AutoReadInitializer clientInitializer = new AutoReadInitializer(!readOutsideEventLoopThread);
                sb.Option(ChannelOption.SoBacklog, 1024)
                .Option(ChannelOption.AutoRead, true)
                .ChildOption(ChannelOption.AutoRead, true)
                // We want to ensure that we attempt multiple individual read operations per read loop so we can
                // test the auto read feature being turned off when data is first read.
                .ChildOption(ChannelOption.RcvbufAllocator, new TestRecvByteBufAllocator())
                .ChildHandler(serverInitializer);

                serverChannel = await sb.BindAsync();

                cb.Option(ChannelOption.AutoRead, true)
                // We want to ensure that we attempt multiple individual read operations per read loop so we can
                // test the auto read feature being turned off when data is first read.
                .Option(ChannelOption.RcvbufAllocator, new TestRecvByteBufAllocator())
                .Handler(clientInitializer);

                clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress);

                // 3 bytes means 3 independent reads for TestRecvByteBufAllocator
                clientChannel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[3])).Ignore();
                serverInitializer._autoReadHandler.AssertSingleRead();

                // 3 bytes means 3 independent reads for TestRecvByteBufAllocator
                serverInitializer._channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[3])).Ignore();
                clientInitializer._autoReadHandler.AssertSingleRead();

                if (readOutsideEventLoopThread)
                {
                    serverInitializer._channel.Read();
                }
                serverInitializer._autoReadHandler.AssertSingleReadSecondTry();

                if (readOutsideEventLoopThread)
                {
                    clientChannel.Read();
                }
                clientInitializer._autoReadHandler.AssertSingleReadSecondTry();
            }
            finally
            {
                if (clientChannel != null)
                {
                    await clientChannel.CloseAsync();
                }
                if (serverChannel != null)
                {
                    await serverChannel.CloseAsync();
                }
            }
        }
        void AutoReadOffDuringReadOnlyReadsOneTime0(bool readOutsideEventLoopThread, ServerBootstrap sb, Bootstrap cb)
        {
            var serverInitializer = new AutoReadInitializer(!readOutsideEventLoopThread);
            var clientInitializer = new AutoReadInitializer(!readOutsideEventLoopThread);

            sb.Option(ChannelOption.SoBacklog, 1024)
            .Option(ChannelOption.AutoRead, true)
            .ChildOption(ChannelOption.AutoRead, true)
            // We want to ensure that we attempt multiple individual read operations per read loop so we can
            // test the auto read feature being turned off when data is first read.
            .ChildOption(ChannelOption.RcvbufAllocator, new TestRecvByteBufAllocator())
            .ChildHandler(serverInitializer);

            // start server
            Task <IChannel> task = sb.BindAsync(LoopbackAnyPort);

            Assert.True(task.Wait(DefaultTimeout), "Server bind timed out");
            this.serverChannel = task.Result;
            Assert.NotNull(this.serverChannel.LocalAddress);
            var endPoint = (IPEndPoint)this.serverChannel.LocalAddress;

            cb.Option(ChannelOption.AutoRead, true)
            // We want to ensure that we attempt multiple individual read operations per read loop so we can
            // test the auto read feature being turned off when data is first read.
            .Option(ChannelOption.RcvbufAllocator, new TestRecvByteBufAllocator())
            .Handler(clientInitializer);

            // connect to server
            task = cb.ConnectAsync(endPoint);
            Assert.True(task.Wait(DefaultTimeout), "Connect to server timed out");
            this.clientChannel = task.Result;
            Assert.NotNull(this.clientChannel.LocalAddress);

            // 3 bytes means 3 independent reads for TestRecvByteBufAllocator
            Task writeTask = this.clientChannel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[3]));

            Assert.True(writeTask.Wait(TimeSpan.FromSeconds(5)), "Client write task timed out");
            serverInitializer.AutoReadHandler.AssertSingleRead();

            // 3 bytes means 3 independent reads for TestRecvByteBufAllocator
            writeTask = serverInitializer.Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[3]));
            Assert.True(writeTask.Wait(TimeSpan.FromSeconds(5)), "Server write task timed out");
            clientInitializer.AutoReadHandler.AssertSingleRead();

            if (readOutsideEventLoopThread)
            {
                serverInitializer.Channel.Read();
            }
            serverInitializer.AutoReadHandler.AssertSingleReadSecondTry();

            if (readOutsideEventLoopThread)
            {
                this.clientChannel.Read();
            }
            clientInitializer.AutoReadHandler.AssertSingleReadSecondTry();
        }