Exemplo n.º 1
0
        void AnyIdle(TestableIdleStateHandler idleStateHandler, params object[] expected)
        {
            Assert.True(expected.Length >= 1, "The number of expected events must be >= 1");

            var events  = new List <object>();
            var handler = new TestEventChannelInboundHandlerAdapter(events);

            var channel = new EmbeddedChannel(idleStateHandler, handler);

            try
            {
                // For each expected event advance the ticker and run() the task. Each
                // step should yield in an IdleStateEvent because we haven't written
                // or read anything from the channel.
                for (int i = 0; i < expected.Length; i++)
                {
                    idleStateHandler.TickRun();
                }

                Assert.Equal(expected.Length, events.Count);

                // Compare the expected with the actual IdleStateEvents
                for (int i = 0; i < expected.Length; i++)
                {
                    object evt = events[i];
                    Assert.Same(expected[i], evt);//"Element " + i + " is not matching"
                }
            }
            finally
            {
                channel.FinishAndReleaseAll();
            }
        }
Exemplo n.º 2
0
        void AnyNotIdle(TestableIdleStateHandler idleStateHandler,
                        Action <EmbeddedChannel> action, object expected)
        {
            var events  = new List <object>();
            var handler = new TestEventChannelInboundHandlerAdapter(events);

            var channel = new EmbeddedChannel(idleStateHandler, handler);

            try
            {
                //TODO: No NANOSECONDS(0.01 Ticks) support in DotNetty for IdleStateHandler, but is it really needed?
                idleStateHandler.DoTick(TimeSpan.FromTicks(1));
                action.Invoke(channel);

                // Advance the ticker by some fraction and run() the task.
                // There shouldn't be an IdleStateEvent getting fired because
                // we've just performed an action on the channel that is meant
                // to reset the idle task.
                TimeSpan delayInNanos = idleStateHandler.Delay;
                Assert.NotEqual(this.zeroSecond, delayInNanos);

                idleStateHandler.TickRun(TimeSpan.FromTicks(delayInNanos.Ticks / 2));
                Assert.Empty(events);

                // Advance the ticker by the full amount and it should yield
                // in an IdleStateEvent.
                idleStateHandler.TickRun();
                Assert.Single(events);
                Assert.Same(expected, events[0]);
            }
            finally
            {
                channel.FinishAndReleaseAll();
            }
        }
Exemplo n.º 3
0
        void ObserveOutputIdle(bool writer)
        {
            TimeSpan       writerIdleTime = this.zeroSecond;
            TimeSpan       allIdleTime    = this.zeroSecond;
            IdleStateEvent expected;

            if (writer)
            {
                writerIdleTime = TimeSpan.FromSeconds(5);
                expected       = IdleStateEvent.FirstWriterIdleStateEvent;
            }
            else
            {
                allIdleTime = TimeSpan.FromSeconds(5);
                expected    = IdleStateEvent.FirstAllIdleStateEvent;
            }

            var idleStateHandler = new TestableIdleStateHandler(
                true, this.zeroSecond, writerIdleTime, allIdleTime);

            var events  = new List <object>();
            var handler = new TestEventChannelInboundHandlerAdapter(events);

            var channel = new ObservableChannel(idleStateHandler, handler);

            try
            {
                // We're writing 3 messages that will be consumed at different rates!
                channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[] { 1 }));
                channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[] { 2 }));
                channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[] { 3 }));
                channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[5 * 1024]));

                // Establish a baseline. We're not consuming anything and let it idle once.
                idleStateHandler.TickRun();
                Assert.Single(events);
                Assert.Same(expected, events[0]);
                events.Clear();

                // Our ticker should be at second 5
                Assert.Equal(TimeSpan.FromSeconds(5), idleStateHandler.Tick);

                // Consume one message in 4 seconds, then be idle for 2 seconds,
                // then run the task and we shouldn't get an IdleStateEvent because
                // we haven't been idle for long enough!
                idleStateHandler.DoTick(TimeSpan.FromSeconds(4));
                AssertNotNullAndRelease(channel.Consume());

                idleStateHandler.TickRun(TimeSpan.FromSeconds(2));
                Assert.Empty(events);
                Assert.Equal(TimeSpan.FromSeconds(11), idleStateHandler.Tick); // 5s + 4s + 2s

                // Consume one message in 3 seconds, then be idle for 4 seconds,
                // then run the task and we shouldn't get an IdleStateEvent because
                // we haven't been idle for long enough!
                idleStateHandler.DoTick(TimeSpan.FromSeconds(3));
                AssertNotNullAndRelease(channel.Consume());

                idleStateHandler.TickRun(TimeSpan.FromSeconds(4));
                Assert.Empty(events);
                Assert.Equal(TimeSpan.FromSeconds(18), idleStateHandler.Tick); // 11s + 3s + 4s

                // Don't consume a message and be idle for 5 seconds.
                // We should get an IdleStateEvent!
                idleStateHandler.TickRun(TimeSpan.FromSeconds(5));
                Assert.Single(events);
                Assert.Equal(TimeSpan.FromSeconds(23), idleStateHandler.Tick); // 18s + 5s
                events.Clear();

                // Consume one message in 2 seconds, then be idle for 1 seconds,
                // then run the task and we shouldn't get an IdleStateEvent because
                // we haven't been idle for long enough!
                idleStateHandler.DoTick(TimeSpan.FromSeconds(2));
                AssertNotNullAndRelease(channel.Consume());

                idleStateHandler.TickRun(TimeSpan.FromSeconds(1));
                Assert.Empty(events);
                Assert.Equal(TimeSpan.FromSeconds(26), idleStateHandler.Tick); // 23s + 2s + 1s

                // Consume part of the message every 2 seconds, then be idle for 1 seconds,
                // then run the task and we should get an IdleStateEvent because the first trigger
                idleStateHandler.DoTick(TimeSpan.FromSeconds(2));
                AssertNotNullAndRelease(channel.ConsumePart(1024));
                idleStateHandler.DoTick(TimeSpan.FromSeconds(2));
                AssertNotNullAndRelease(channel.ConsumePart(1024));
                idleStateHandler.TickRun(TimeSpan.FromSeconds(1));
                Assert.Single(events);
                Assert.Equal(TimeSpan.FromSeconds(31), idleStateHandler.Tick); // 26s + 2s + 2s + 1s
                events.Clear();

                // Consume part of the message every 2 seconds, then be idle for 1 seconds,
                // then consume all the rest of the message, then run the task and we shouldn't
                // get an IdleStateEvent because the data is flowing and we haven't been idle for long enough!
                idleStateHandler.DoTick(TimeSpan.FromSeconds(2));
                AssertNotNullAndRelease(channel.ConsumePart(1024));
                idleStateHandler.DoTick(TimeSpan.FromSeconds(2));
                AssertNotNullAndRelease(channel.ConsumePart(1024));
                idleStateHandler.TickRun(TimeSpan.FromSeconds(1));
                Assert.Empty(events);
                Assert.Equal(TimeSpan.FromSeconds(36), idleStateHandler.Tick); // 31s + 2s + 2s + 1s
                idleStateHandler.DoTick(TimeSpan.FromSeconds(2));
                AssertNotNullAndRelease(channel.ConsumePart(1024));

                // There are no messages left! Advance the ticker by 3 seconds,
                // attempt a consume() but it will be null, then advance the
                // ticker by an another 2 seconds and we should get an IdleStateEvent
                // because we've been idle for 5 seconds.
                idleStateHandler.DoTick(TimeSpan.FromSeconds(3));
                Assert.Null(channel.Consume());

                idleStateHandler.TickRun(TimeSpan.FromSeconds(2));
                Assert.Single(events);
                Assert.Equal(TimeSpan.FromSeconds(43), idleStateHandler.Tick); // 36s + 2s + 3s + 2s

                // q.e.d.
            }
            finally
            {
                channel.FinishAndReleaseAll();
            }
        }