Example #1
0
        public void SelectManyDecorator_Test()
        {
            var count = 0;

            var notification = new TestChainBuilderNotifier();
            var services     = new ChainBuilderSetupServices(notification);
            var builder      = new ChainBuilder <OuterMessage>();

            builder.SelectMany(m => m.Messages).Skip(2).Take(2).Handler(m => count++);
            var chain = builder.BuildFunc(services);

            Assert.False(notification.IsDisposed);
            Assert.Equal(0, count);

            chain(
                new OuterMessage
            {
                Messages = new[] { 1, 2, 3 }
            },
                CancellationToken.None);

            Assert.False(notification.IsDisposed);
            Assert.Equal(1, count);

            chain(
                new OuterMessage
            {
                Messages = new[] { 4, 5, 6 }
            },
                CancellationToken.None);

            Assert.Equal(2, count);
            Assert.True(notification.IsDisposed);
        }
        public void TakeWhileDecorator_Tests()
        {
            var count = 0;

            var notification = new TestChainBuilderNotifier();
            var services     = new ChainBuilderSetupServices(notification);

            var chain = new ChainBuilder <int>().TakeWhile(m => m <= 2).Handler(m => count++).BuildFunc(services);

            // The notification is disposed when the Take handler has processed 2 messages, trying to process the 3rd
            Assert.False(notification.IsDisposed);
            Assert.Equal(0, count);

            chain(1, CancellationToken.None);
            chain(2, CancellationToken.None);

            // 2 should be skipped taken
            Assert.Equal(2, count);

            chain(3, CancellationToken.None);

            Assert.True(notification.IsDisposed);

            chain(4, CancellationToken.None);

            // Skip the rest - still 2
            Assert.Equal(2, count);
        }