Example #1
0
        public async Task TestPrecancellation3()
        {
            var b = new JoinBlock <int, int, int>(new GroupingDataflowBlockOptions
            {
                CancellationToken = new CancellationToken(canceled: true),
                MaxNumberOfGroups = 1
            });

            Assert.NotNull(b.LinkTo(DataflowBlock.NullTarget <Tuple <int, int, int> >()));
            Assert.False(b.Target1.Post(42));
            Assert.False(b.Target2.Post(43));
            Assert.False(b.Target2.Post(44));

            Task <bool> t1 = b.Target1.SendAsync(42);
            Task <bool> t2 = b.Target2.SendAsync(43);
            Task <bool> t3 = b.Target2.SendAsync(44);

            Assert.True(t1.IsCompleted);
            Assert.False(t1.Result);
            Assert.True(t2.IsCompleted);
            Assert.False(t2.Result);
            Assert.True(t3.IsCompleted);
            Assert.False(t3.Result);

            Tuple <int, int, int>          ignoredValue;
            IList <Tuple <int, int, int> > ignoredValues;

            Assert.False(b.TryReceive(out ignoredValue));
            Assert.False(b.TryReceiveAll(out ignoredValues));
            Assert.Equal(expected: 0, actual: b.OutputCount);
            Assert.NotNull(b.Completion);
            b.Complete();

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => b.Completion);
        }
Example #2
0
        public async Task TestNonGreedyDropPostponedOnCompletion()
        {
            var joinBlock = new JoinBlock <int, int>(new GroupingDataflowBlockOptions {
                Greedy = false
            });
            var source = new BufferBlock <int>();

            source.Post(1);
            source.LinkTo(joinBlock.Target1);
            joinBlock.Complete();
            await joinBlock.Completion;
        }
Example #3
0
		public void CompletionTest ()
		{
			var block = new JoinBlock<int, int, int> ();

			Assert.IsTrue (block.Target1.Post (1));

			block.Complete ();

			Tuple<int, int, int> tuple;
			Assert.IsFalse (block.TryReceive (out tuple));

			Assert.IsTrue (block.Completion.Wait (1000));
		}
        public IDictionary <string, string[]> JoinBlockUsage(int numberOfIteration)
        {
            Console.WriteLine($"Inside {nameof(TplDataflow3GroupingBlocksController)} - {nameof(JoinBlockUsage)}");

            var ouputCollection = new Dictionary <string, string[]>();

            Functions.ClearCounterForJoinBlockUsage();

            // Create the members of the pipeline.
            var broadCastBlock          = new BroadcastBlock <int>(i => i);
            var transformBlockDoNothing = new TransformBlock <int, int>(i =>
                                                                        Functions.Noop(i)
                                                                        );
            var transformBlockSquare = new TransformBlock <int, int>(i =>
                                                                     Functions.Square(i)
                                                                     );
            var transformBlockMultipleByPi = new TransformBlock <int, double>(i =>
                                                                              Functions.MultiplyByPi(i)
                                                                              );
            var joinBlock      = new JoinBlock <int, int, double>();
            var processorBlock = new ActionBlock <Tuple <int, int, double> >(tuple =>
                                                                             Functions.FormatTupleForTheOuputCollection(ouputCollection, tuple)
                                                                             );

            // Connect the dataflow blocks to form a pipeline.
            broadCastBlock.LinkTo(transformBlockDoNothing, DataflowOptions.LinkOptions);
            broadCastBlock.LinkTo(transformBlockSquare, DataflowOptions.LinkOptions);
            broadCastBlock.LinkTo(transformBlockMultipleByPi, DataflowOptions.LinkOptions);
            transformBlockDoNothing.LinkTo(joinBlock.Target1);
            transformBlockSquare.LinkTo(joinBlock.Target2);
            transformBlockMultipleByPi.LinkTo(joinBlock.Target3);
            joinBlock.LinkTo(processorBlock, DataflowOptions.LinkOptions);

            // Start JoinBlockUsage pipeline with the input values.
            for (var i = 0; i <= numberOfIteration; i++)
            {
                broadCastBlock.Post(i);
            }

            // Mark the head of the pipeline as complete.
            broadCastBlock.Complete();

            // Wait for the last block in the pipeline to process all messages.
            Task.WhenAll(transformBlockDoNothing.Completion,
                         transformBlockSquare.Completion,
                         transformBlockMultipleByPi.Completion)
            .ContinueWith(_ => joinBlock.Complete());
            processorBlock.Completion.Wait();

            return(ouputCollection);
        }
Example #5
0
        public void CompletionTest()
        {
            var block = new JoinBlock <int, int> ();

            Assert.IsTrue(block.Target1.Post(1));

            block.Complete();

            Tuple <int, int> tuple;

            Assert.IsFalse(block.TryReceive(out tuple));

            Assert.IsTrue(block.Completion.Wait(1000));
        }
Example #6
0
        public async Task TestCompletionThroughBlock()
        {
            var join2 = new JoinBlock <int, int>();

            join2.Target1.Post(1);
            join2.Complete();
            await join2.Completion;

            var join3 = new JoinBlock <int, int, int>();

            join3.Target1.Post(2);
            join3.Complete();
            await join3.Completion;
        }
Example #7
0
        static void Main(string[] args)
        {
            var joiner      = new JoinBlock <Trial, Trial>();
            var transformer = new TransformBlock <Tuple <Trial, Trial>, Trial>(t =>
            {
                Trial combined = new Trial(t.Item1.Id);
                combined.Losses.AddRange(t.Item1.Losses);
                combined.Losses.AddRange(t.Item2.Losses);
                return(combined);
            });

            var manipulator = new TransformBlock <Trial, Trial>(x =>
            {
                foreach (var loss in x.Losses)
                {
                    loss.Amount *= 0.8m;
                }

                return(x);
            });

            var sb = new StatisticsBlock();

            joiner.LinkTo(transformer, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            transformer.LinkTo(manipulator, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            manipulator.LinkTo(sb.Block, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            sb.Block.LinkTo(DataflowBlock.NullTarget <Trial>());

            Generate(10, joiner.Target1);
            Generate(100, joiner.Target2);

            joiner.Complete();

            sb.Block.Completion.Wait();

            Console.WriteLine(sb.Average());
            Console.WriteLine(sb.Tvar10());
            Console.WriteLine(sb.Poh());

            Console.WriteLine("Done");

            Console.ReadKey();
        }
Example #8
0
        public async Task TestNonGreedyReleasingFailsAtCompletion()
        {
            var joinBlock = new JoinBlock <int, int>(new GroupingDataflowBlockOptions {
                Greedy = false
            });
            var source = new DelegatePropagator <int, int>
            {
                ReserveMessageDelegate = (header, target) => true,
                ReleaseMessageDelegate = delegate { throw new FormatException(); }
            };

            joinBlock.Target1.OfferMessage(new DataflowMessageHeader(1), 1, source, consumeToAccept: true);
            joinBlock.Complete();

            await Assert.ThrowsAsync <FormatException>(() => joinBlock.Completion);
        }
Example #9
0
        public static void TestSync6()
        {
            joinB.LinkTo(ab2);

            for (int i = 0; i < 5; i++)
            {
                joinB.Target1.Post(i);
            }

            for (int i = 5; i > 0; i--)
            {
                Thread.Sleep(1000);
                joinB.Target2.Post(i.ToString());
            }

            joinB.Complete();
            Console.WriteLine("Post Finished");
            //joinB.Completion.Wait();
//            Console.WriteLine("Process Finished");
        }
Example #10
0
 public void Complete()
 {
     _joinBlock.Complete();
 }
 public void Complete()
 {
     _joinSources.Complete();
     _mergeImageBlock.Complete();
     _resultBlock.Complete();
 }
Example #12
0
        public void RunJoinBlockConformanceTests()
        {
            // Test Post/Receive single block
            {
                int iter = 2;

                var block2 = new JoinBlock <int, int>();
                for (int i = 0; i < iter; i++)
                {
                    block2.Target1.Post(i);
                    block2.Target2.Post(i);
                    var msg = block2.Receive();

                    Assert.False(msg.Item1 != i || msg.Item2 != i, string.Format("JoinBlock Post/Receive failed expected {0},{1} and actual {2},{3}", i, i, msg.Item1, msg.Item2));
                }

                var block3 = new JoinBlock <int, int, int>();
                for (int i = 0; i < iter; i++)
                {
                    block3.Target1.Post(i);
                    block3.Target2.Post(i);
                    block3.Target3.Post(i);
                    var msg = block3.Receive();
                    Assert.False(msg.Item1 != i || msg.Item2 != i || msg.Item3 != i, string.Format("JoinBlock Post/Receive failed expected {0},{1},{2} and actual {3},{4},{5}", i, i, i, msg.Item1, msg.Item2, msg.Item3));
                }
            }

            // Test PostAll then Receive single block
            {
                int iter = 2;

                var block2 = new JoinBlock <int, int>();
                for (int i = 0; i < iter; i++)
                {
                    block2.Target1.Post(i);
                    block2.Target2.Post(i);
                }
                for (int i = 0; i < iter; i++)
                {
                    var msg = block2.Receive();
                    Assert.False(msg.Item1 != msg.Item2, "JoinBlock PostAll then Receive failed expected, incorrect msg pair");
                }

                var block3 = new JoinBlock <int, int, int>();
                for (int i = 0; i < iter; i++)
                {
                    block3.Target1.Post(i);
                    block3.Target2.Post(i);
                    block3.Target3.Post(i);
                }
                for (int i = 0; i < iter; i++)
                {
                    var msg = block3.Receive();
                    Assert.False(msg.Item1 != msg.Item2 || msg.Item2 != msg.Item3, "JoinBlock PostAll then Receive failed expected, incorrect msg pair");
                }
            }

            //Test one target Post with TryReceive
            {
                var block2 = new JoinBlock <int, int>();
                block2.Target1.Post(0);
                Tuple <int, int> result2;
                Assert.False(block2.TryReceive(out result2), "JoinBlock.TryReceive failed, returned true and only one target posted a message");
                Assert.False(block2.OutputCount > 0, "JoinBlock.OutputCount failed, returned count > 0 and only one target posted a message");
                var block3 = new JoinBlock <int, int, int>();
                block3.Target1.Post(0);
                Tuple <int, int, int> result3;
                Assert.False(block3.TryReceive(out result3), "JoinBlock.TryReceive failed, returned true and only one target posted a message");
                Assert.False(block3.OutputCount > 0, "JoinBlock.OutputCount failed, returned count > 0 and only one target posted a message");
            }

            // Test JoinBlock`2 using a precanceled token
            {
                var localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    var dbo = new GroupingDataflowBlockOptions {
                        CancellationToken = cts.Token, MaxNumberOfGroups = 1
                    };
                    var jb = new JoinBlock <int, int>(dbo);

                    Tuple <int, int>          ignoredValue;
                    IList <Tuple <int, int> > ignoredValues;
                    localPassed &= jb.LinkTo(new ActionBlock <Tuple <int, int> >(delegate { })) != null;
                    localPassed &= jb.Target1.Post(42) == false;
                    localPassed &= jb.Target2.Post(42) == false;
                    localPassed &= jb.Target1.SendAsync(42).Result == false;
                    localPassed &= jb.Target2.SendAsync(42).Result == false;
                    localPassed &= jb.TryReceiveAll(out ignoredValues) == false;
                    localPassed &= jb.TryReceive(out ignoredValue) == false;
                    localPassed &= jb.OutputCount == 0;
                    localPassed &= jb.Completion != null;
                    jb.Target1.Complete();
                    jb.Target2.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }

                Assert.True(localPassed, string.Format("Precanceled tokens on JB`2 - {0}", localPassed ? "Passed" : "FAILED"));
            }

            // Test JoinBlock`3 using a precanceled token
            {
                var localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    var dbo = new GroupingDataflowBlockOptions {
                        CancellationToken = cts.Token, MaxNumberOfGroups = 1
                    };
                    var jb = new JoinBlock <int, int, int>(dbo);

                    Tuple <int, int, int>          ignoredValue;
                    IList <Tuple <int, int, int> > ignoredValues;
                    localPassed &= jb.LinkTo(new ActionBlock <Tuple <int, int, int> >(delegate { })) != null;
                    localPassed &= jb.Target1.Post(42) == false;
                    localPassed &= jb.Target2.Post(42) == false;
                    localPassed &= jb.Target3.Post(42) == false;
                    localPassed &= jb.Target1.SendAsync(42).Result == false;
                    localPassed &= jb.Target2.SendAsync(42).Result == false;
                    localPassed &= jb.Target3.SendAsync(42).Result == false;
                    localPassed &= jb.TryReceiveAll(out ignoredValues) == false;
                    localPassed &= jb.TryReceive(out ignoredValue) == false;
                    localPassed &= jb.OutputCount == 0;
                    localPassed &= jb.Completion != null;
                    jb.Target1.Complete();
                    jb.Target2.Complete();
                    jb.Target3.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }

                Assert.True(localPassed, string.Format("Precanceled tokens on JB`3 - {0}", localPassed ? "Passed" : "FAILED"));
            }

            // Test JoinBlock`2 completion through all targets
            {
                var localPassed = true;
                var join        = new JoinBlock <int, int>();
                join.Target1.Post(1);
                join.Target1.Complete();
                join.Target2.Complete();
                localPassed = join.Completion.Wait(2000);

                Assert.True(localPassed, string.Format("JoinBlock`2 completed through targets - {0}", localPassed ? "Passed" : "FAILED"));
            }

            // Test JoinBlock`3 completion through all targets
            {
                var localPassed = true;
                var join        = new JoinBlock <int, int, int>();
                join.Target1.Post(1);
                join.Target1.Complete();
                join.Target2.Complete();
                join.Target3.Complete();
                localPassed = join.Completion.Wait(2000);

                Assert.True(localPassed, string.Format("JoinBlock`3 completed through targets - {0}", localPassed ? "Passed" : "FAILED"));
            }

            // Test JoinBlock`2 completion through block
            {
                var localPassed = true;
                var join        = new JoinBlock <int, int>();
                join.Target1.Post(1);
                join.Complete();
                localPassed = join.Completion.Wait(2000);

                Assert.True(localPassed, string.Format("JoinBlock`2 completed through block - {0}", localPassed ? "Passed" : "FAILED"));
            }

            // Test JoinBlock`3 completion through block
            {
                var localPassed = true;
                var join        = new JoinBlock <int, int, int>();
                join.Target1.Post(1);
                join.Complete();
                localPassed = join.Completion.Wait(2000);

                Assert.True(localPassed, string.Format("JoinBlock`3 completed through block - {0}", localPassed ? "Passed" : "FAILED"));
            }
        }