public async Task TestAcquireTimeout()
        {
            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, ChannelActiveHealthChecker.Instance, FixedChannelPool.AcquireTimeoutAction.Fail, TimeSpan.FromMilliseconds(500), 1, int.MaxValue);

            IChannel channel = await pool.AcquireAsync();

            try
            {
                await Assert.ThrowsAsync <TimeoutException>(async() => await pool.AcquireAsync());
            }
            finally
            {
                await sc.CloseAsync();

                await channel.CloseAsync();
            }
        }
Example #2
0
        public async Task TestCloseAsync()
        {
            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 pool = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
            await pool.AcquireAsync();
            await pool.AcquireAsync();

            var closePromise = sc.NewPromise();
            await pool.CloseAsync();

            Assert.Equal(0, pool.AcquiredChannelCount);
            await sc.CloseAsync(closePromise);

            Assert.True(closePromise.IsSuccess);
        }
        public async Task TestAcquireNewConnectionWhen()
        {
            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);
            IChannel channel1 = await pool.AcquireAsync();

            await channel1.CloseAsync();

            pool.ReleaseAsync(channel1);

            IChannel channel2 = await pool.AcquireAsync();

            Assert.NotSame(channel1, channel2);
            await sc.CloseAsync();

            await channel2.CloseAsync();
        }
Example #4
0
        public async Task TestAcquireBoundQueue()
        {
            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, 1);

            IChannel channel = await pool.AcquireAsync();
            var future = pool.AcquireAsync();
            Assert.False(future.IsCompleted);

            try
            {
                await Assert.ThrowsAsync<InvalidOperationException>(async () => await pool.AcquireAsync());
            }
            finally
            {
                await sc.CloseAsync();
                await channel.CloseAsync();
                pool.Close();
            }
        }
Example #5
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();
        }
Example #6
0
        public async Task TestAcquireNewConnection()
        {
            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,
                ChannelActiveHealthChecker.Instance,
                FixedChannelPool.AcquireTimeoutAction.New,
                TimeSpan.FromMilliseconds(500),
                1,
                int.MaxValue);

            IChannel channel = await pool.AcquireAsync();
            IChannel channel2 = await pool.AcquireAsync();
            Assert.NotSame(channel, channel2);
            await sc.CloseAsync();
            await channel.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();
        }
        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();
            }
        }
Example #9
0
        public async Task TestReleaseClosed()
        {
            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      pool    = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
            IChannel channel = await pool.AcquireAsync();

            await channel.CloseAsync();

            await pool.ReleaseAsync(channel);

            await sc.CloseAsync();

            pool.Close();
        }
Example #10
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();
        }