public async Task TestReleaseClosed()
        {
            var       addr = new LocalAddress(LOCAL_ADDR_ID);
            Bootstrap cb   = new Bootstrap().RemoteAddress(addr).Group(this.group).Channel <LocalChannel>();

            ServerBootstrap sb = new ServerBootstrap()
                                 .Group(this.group)
                                 .Channel <LocalServerChannel>()
                                 .ChildHandler(
                new ActionChannelInitializer <LocalChannel>(
                    ch => ch.Pipeline.AddLast(new ChannelHandlerAdapter()))
                );

            // Start server
            IChannel sc = await sb.BindAsync(addr);

            var      pool    = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
            IChannel channel = await pool.AcquireAsync();

            await channel.CloseAsync();

            await pool.ReleaseAsync(channel);

            await sc.CloseAsync();
        }
Пример #2
0
        public async Task TestAcquireNewConnectionWhen()
        {
            var addr = new LocalAddress(ChannelPoolTestUtils.GetLocalAddrId());
            Bootstrap cb = new Bootstrap().RemoteAddress(addr).Group(_group).Channel<LocalChannel>();

            ServerBootstrap sb = new ServerBootstrap()
                .Group(_group)
                .Channel<LocalServerChannel>()
                .ChildHandler(
                    new ActionChannelInitializer<LocalChannel>(
                        ch => ch.Pipeline.AddLast(new ChannelHandlerAdapter()))
                );

            // Start server
            IChannel sc = await sb.BindAsync(addr);
            var handler = new TestChannelPoolHandler();
            var pool = new FixedChannelPool(cb, handler, 1);
            IChannel channel1 = await pool.AcquireAsync();
            await channel1.CloseAsync();
#pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
            pool.ReleaseAsync(channel1);
#pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法

            IChannel channel2 = await pool.AcquireAsync();

            Assert.NotSame(channel1, channel2);
            await sc.CloseAsync();
            await channel2.CloseAsync();
            pool.Close();
        }
        public async Task TestReleaseDifferentPool()
        {
            var       addr = new LocalAddress(LOCAL_ADDR_ID);
            Bootstrap cb   = new Bootstrap().RemoteAddress(addr).Group(this.group).Channel <LocalChannel>();

            ServerBootstrap sb = new ServerBootstrap()
                                 .Group(this.group)
                                 .Channel <LocalServerChannel>()
                                 .ChildHandler(
                new ActionChannelInitializer <LocalChannel>(
                    ch => ch.Pipeline.AddLast(new ChannelHandlerAdapter()))
                );

            // Start server
            IChannel sc = await sb.BindAsync(addr);

            var handler = new TestChannelPoolHandler();
            var pool    = new FixedChannelPool(cb, handler, 1, 1);
            var pool2   = new FixedChannelPool(cb, handler, 1, 1);

            IChannel channel = await pool.AcquireAsync();

            try
            {
                await Assert.ThrowsAsync <ArgumentException>(async() => await pool2.ReleaseAsync(channel));
            }
            finally
            {
                await sc.CloseAsync();

                await channel.CloseAsync();
            }
        }
Пример #4
0
        public async Task TestAcquire()
        {
            var       addr = new LocalAddress(ChannelPoolTestUtils.GetLocalAddrId());
            Bootstrap cb   = new Bootstrap().RemoteAddress(addr).Group(_group).Channel <LocalChannel>();

            ServerBootstrap sb = new ServerBootstrap()
                                 .Group(_group)
                                 .Channel <LocalServerChannel>()
                                 .ChildHandler(
                new ActionChannelInitializer <LocalChannel>(
                    ch => ch.Pipeline.AddLast(new ChannelHandlerAdapter()))
                );

            // Start server
            IChannel sc = await sb.BindAsync(addr);

            var handler = new CountingChannelPoolHandler();

            var pool = new FixedChannelPool(cb, handler, 1);

            IChannel channel = await pool.AcquireAsync();

            var future = pool.AcquireAsync();

            Assert.False(future.IsCompleted);

            await pool.ReleaseAsync(channel);

            await Task.Delay(TimeSpan.FromSeconds(1));

            Assert.True(future.IsCompleted);

            IChannel channel2 = future.Result;

            Assert.Same(channel, channel2);
            Assert.Equal(1, handler.ChannelCount);
            Assert.Equal(1, handler.AcquiredCount);
            Assert.Equal(1, handler.ReleasedCount);

            await sc.CloseAsync();

            await channel2.CloseAsync();

            pool.Close();
        }
        public async Task TestReleaseAfterClosePool()
        {
            var       addr = new LocalAddress(LOCAL_ADDR_ID);
            Bootstrap cb   = new Bootstrap().RemoteAddress(addr).Group(this.group).Channel <LocalChannel>();

            ServerBootstrap sb = new ServerBootstrap()
                                 .Group(this.group)
                                 .Channel <LocalServerChannel>()
                                 .ChildHandler(
                new ActionChannelInitializer <LocalChannel>(
                    ch => ch.Pipeline.AddLast(new ChannelHandlerAdapter()))
                );

            // Start server
            IChannel sc = await sb.BindAsync(addr);

            var      pool    = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
            IChannel channel = await pool.AcquireAsync();

            pool.Dispose();

            await group.GetNext().SubmitAsync(() => TaskEx.Completed);

            var e = await Assert.ThrowsAsync <InvalidOperationException>(async() => await pool.ReleaseAsync(channel));

            Assert.Same(FixedChannelPool.PoolClosedOnReleaseException, e);

            // Since the pool is closed, the Channel should have been closed as well.
            await channel.CloseCompletion;

            Assert.False(channel.Open, "Unexpected open channel");
            await sc.CloseAsync();
        }