Exemplo n.º 1
0
        public async Task TestFaultingAndCancellation()
        {
            foreach (bool fault in DataflowTestHelpers.BooleanValues)
            {
                var cts = new CancellationTokenSource();
                var tb  = new TransformBlock <int, int>(i => i, new ExecutionDataflowBlockOptions {
                    CancellationToken = cts.Token
                });
                tb.PostRange(0, 4);
                Assert.Equal(expected: 0, actual: await tb.ReceiveAsync());
                Assert.Equal(expected: 1, actual: await tb.ReceiveAsync());

                if (fault)
                {
                    Assert.Throws <ArgumentNullException>(() => ((IDataflowBlock)tb).Fault(null));
                    ((IDataflowBlock)tb).Fault(new InvalidCastException());
                    await Assert.ThrowsAsync <InvalidCastException>(() => tb.Completion);
                }
                else
                {
                    cts.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => tb.Completion);
                }

                Assert.Equal(expected: 0, actual: tb.InputCount);
                Assert.Equal(expected: 0, actual: tb.OutputCount);
            }
        }
Exemplo n.º 2
0
        public async Task TenTransformsToAction()
        {
            var first = new TransformBlock <int, int>(item => item);

            TransformBlock <int, int> t = first;

            for (int i = 0; i < 9; i++)
            {
                var next = new TransformBlock <int, int>(item => item);
                t.LinkTo(next, new DataflowLinkOptions {
                    PropagateCompletion = true
                });
                t = next;
            }
            int completedCount = 0;
            var last           = new ActionBlock <int>(i => completedCount++);

            t.LinkTo(last, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            first.PostRange(0, Iterations);
            first.Complete();

            await last.Completion;

            Assert.Equal(expected: Iterations, actual: completedCount);
        }
Exemplo n.º 3
0
        private static TransformBlock <int, string> ConstructTransformWithNMessages(int messagesCount)
        {
            var block = new TransformBlock <int, string>(i => i.ToString());

            block.PostRange(0, messagesCount);
            SpinWait.SpinUntil(() => block.OutputCount == messagesCount);
            return(block);
        }
Exemplo n.º 4
0
        public async Task TransformThroughFilterToAction()
        {
            int completedCount = 0;

            var t = new TransformBlock<int, int>(i => i);
            var c = new ActionBlock<int>(i => completedCount++);
            t.LinkTo(c, new DataflowLinkOptions { PropagateCompletion = true }, i => true);

            t.PostRange(0, Iterations);
            t.Complete();

            await c.Completion;
            Assert.Equal(expected: Iterations, actual: completedCount);
        }
Exemplo n.º 5
0
        [OuterLoop] // spins waiting for a condition to be true, though it should happen very quickly
        public async Task TestCount()
        {
            var tb = new TransformBlock <int, int>(i => i);

            Assert.Equal(expected: 0, actual: tb.InputCount);
            Assert.Equal(expected: 0, actual: tb.OutputCount);

            tb.PostRange(1, 11);
            await Task.Run(() => SpinWait.SpinUntil(() => tb.OutputCount == 10));

            for (int i = 10; i > 0; i--)
            {
                int item;
                Assert.True(tb.TryReceive(out item));
                Assert.Equal(expected: 11 - i, actual: item);
                Assert.Equal(expected: i - 1, actual: tb.OutputCount);
            }
        }
Exemplo n.º 6
0
        public async Task TransformThroughFilterToAction()
        {
            int completedCount = 0;

            var t = new TransformBlock <int, int>(i => i);
            var c = new ActionBlock <int>(i => completedCount++);

            t.LinkTo(c, new DataflowLinkOptions {
                PropagateCompletion = true
            }, i => true);

            t.PostRange(0, Iterations);
            t.Complete();

            await c.Completion;

            Assert.Equal(expected: Iterations, actual: completedCount);
        }
Exemplo n.º 7
0
        public async Task TransformToAction()
        {
            var t = new TransformBlock<int, int>(i => i * 2);
            int completedCount = 0;
            int prev = -2;
            var c = new ActionBlock<int>(i =>
            {
                completedCount++;
                Assert.Equal(expected: i, actual: prev + 2);
                prev = i;
            });
            t.LinkTo(c, new DataflowLinkOptions { PropagateCompletion = true });

            t.PostRange(0, Iterations);
            t.Complete();

            await c.Completion;
            Assert.True(completedCount == Iterations);
        }
Exemplo n.º 8
0
        public async Task TenTransformsToAction()
        {
            var first = new TransformBlock<int, int>(item => item);

            TransformBlock<int, int> t = first;
            for (int i = 0; i < 9; i++)
            {
                var next = new TransformBlock<int, int>(item => item);
                t.LinkTo(next, new DataflowLinkOptions { PropagateCompletion = true });
                t = next;
            }
            int completedCount = 0;
            var last = new ActionBlock<int>(i => completedCount++);
            t.LinkTo(last, new DataflowLinkOptions { PropagateCompletion = true });

            first.PostRange(0, Iterations);
            first.Complete();

            await last.Completion;
            Assert.Equal(expected: Iterations, actual: completedCount);
        }
Exemplo n.º 9
0
        public async Task TestOrdering()
        {
            const int iters = 1000;

            foreach (int mmpt in new[] { DataflowBlockOptions.Unbounded, 1 })
            {
                foreach (int dop in new[] { 1, 2, DataflowBlockOptions.Unbounded })
                {
                    var options = new ExecutionDataflowBlockOptions {
                        MaxDegreeOfParallelism = dop, MaxMessagesPerTask = mmpt
                    };
                    var tb = new TransformBlock <int, int>(i => i, options);
                    tb.PostRange(0, iters);
                    for (int i = 0; i < iters; i++)
                    {
                        Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
                    }
                    tb.Complete();
                    await tb.Completion;
                }
            }
        }
Exemplo n.º 10
0
        public async Task TransformToAction()
        {
            var t = new TransformBlock <int, int>(i => i * 2);
            int completedCount = 0;
            int prev           = -2;
            var c = new ActionBlock <int>(i =>
            {
                completedCount++;
                Assert.Equal(expected: i, actual: prev + 2);
                prev = i;
            });

            t.LinkTo(c, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            t.PostRange(0, Iterations);
            t.Complete();

            await c.Completion;

            Assert.True(completedCount == Iterations);
        }
Exemplo n.º 11
0
        public async Task TestOrdering_Async_OrderedDisabled()
        {
            // If ordering were enabled, this test would hang.

            var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded, EnsureOrdered = false };

            var tasks = new TaskCompletionSource<int>[10];
            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = new TaskCompletionSource<int>();
            }

            var tb = new TransformBlock<int, int>(i => tasks[i].Task, options);
            tb.PostRange(0, tasks.Length);

            for (int i = tasks.Length - 1; i >= 0; i--)
            {
                tasks[i].SetResult(i);
                Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
            }

            tb.Complete();
            await tb.Completion;
        }
Exemplo n.º 12
0
        [InlineData(2, 1, false)] // no force ordered, but dop == 1, so it doesn't matter
        public async Task TestOrdering_Async_OrderedEnabled(int mmpt, int dop, bool? EnsureOrdered)
        {
            const int iters = 1000;

            var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop, MaxMessagesPerTask = mmpt };
            if (EnsureOrdered == null)
            {
                Assert.True(options.EnsureOrdered);
            }
            else
            {
                options.EnsureOrdered = EnsureOrdered.Value;
            }

            var tb = new TransformBlock<int, int>(i => Task.FromResult(i), options);
            tb.PostRange(0, iters);
            for (int i = 0; i < iters; i++)
            {
                Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
            }
            tb.Complete();
            await tb.Completion;
        }
Exemplo n.º 13
0
        public async Task TestNullTasksIgnored()
        {
            foreach (int dop in new[] { DataflowBlockOptions.Unbounded, 1, 2 })
            {
                var tb = new TransformBlock<int, int>(i => {
                    if ((i % 2) == 0) return null;
                    return Task.Run(() => i);
                }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop });

                const int Iters = 100;
                tb.PostRange(0, Iters);
                tb.Complete();

                for (int i = 0; i < Iters; i++)
                {
                    if ((i % 2) != 0)
                    {
                        Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
                    }
                }
                await tb.Completion;
            }
        }
Exemplo n.º 14
0
        public async Task TestCancellationExceptionsIgnored()
        {
            var t = new TransformBlock<int, int>(i => {
                if ((i % 2) == 0) throw new OperationCanceledException();
                return i;
            });
            t.PostRange(0, 2);
            t.Complete();
            for (int i = 0; i < 2; i++)
            {
                if ((i % 2) != 0)
                {
                    Assert.Equal(expected: i, actual: await t.ReceiveAsync());
                }
            }

            await t.Completion;
        }
Exemplo n.º 15
0
        public async Task TestFaultingAndCancellation()
        {
            foreach (bool fault in DataflowTestHelpers.BooleanValues)
            {
                var cts = new CancellationTokenSource();
                var tb = new TransformBlock<int, int>(i => i, new ExecutionDataflowBlockOptions { CancellationToken = cts.Token });
                tb.PostRange(0, 4);
                Assert.Equal(expected: 0, actual: await tb.ReceiveAsync());
                Assert.Equal(expected: 1, actual: await tb.ReceiveAsync());

                if (fault)
                {
                    Assert.Throws<ArgumentNullException>(() => ((IDataflowBlock)tb).Fault(null));
                    ((IDataflowBlock)tb).Fault(new InvalidCastException());
                    await Assert.ThrowsAsync<InvalidCastException>(() => tb.Completion);
                }
                else
                {
                    cts.Cancel();
                    await Assert.ThrowsAnyAsync<OperationCanceledException>(() => tb.Completion);
                }

                Assert.Equal(expected: 0, actual: tb.InputCount);
                Assert.Equal(expected: 0, actual: tb.OutputCount);
            }
        }
Exemplo n.º 16
0
        [OuterLoop] // spins waiting for a condition to be true, though it should happen very quickly
        public async Task TestCount()
        {
            var tb = new TransformBlock<int, int>(i => i);
            Assert.Equal(expected: 0, actual: tb.InputCount);
            Assert.Equal(expected: 0, actual: tb.OutputCount);

            tb.PostRange(1, 11);
            await Task.Run(() => SpinWait.SpinUntil(() => tb.OutputCount == 10));
            for (int i = 10; i > 0; i--)
            {
                int item;
                Assert.True(tb.TryReceive(out item));
                Assert.Equal(expected: 11 - i, actual: item);
                Assert.Equal(expected: i - 1, actual: tb.OutputCount);
            }
        }
Exemplo n.º 17
0
 public async Task TestOrdering()
 {
     const int iters = 1000;
     foreach (int mmpt in new[] { DataflowBlockOptions.Unbounded, 1 })
     foreach (int dop in new[] { 1, 2, DataflowBlockOptions.Unbounded })
     {
         var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop, MaxMessagesPerTask = mmpt };
         var tb = new TransformBlock<int, int>(i => i, options);
         tb.PostRange(0, iters);
         for (int i = 0; i < iters; i++)
         {
             Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
         }
         tb.Complete();
         await tb.Completion;
     }
 }