Ejemplo n.º 1
0
		public void GreedyJoin3Test ()
		{
			var scheduler = new TestScheduler ();
			var block =
				new JoinBlock<int, int, int> (new GroupingDataflowBlockOptions
				{ TaskScheduler = scheduler });
			var source1 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source2 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source3 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source1.LinkTo (block.Target1));
			Assert.IsNotNull (source2.LinkTo (block.Target2));
			Assert.IsNotNull (source3.LinkTo (block.Target3));

			Assert.IsTrue (source1.Post (1));
			scheduler.ExecuteAll ();

			int i;
			Assert.IsFalse (source1.TryReceive (out i));

			Assert.IsTrue (source2.Post (11));
			Assert.IsTrue (source3.Post (21));
			scheduler.ExecuteAll ();

			Assert.IsFalse (source2.TryReceive (out i));
			Assert.IsFalse (source3.TryReceive (out i));

			Tuple<int, int, int> tuple;
			Assert.IsTrue (block.TryReceive (out tuple));
			Assert.AreEqual (Tuple.Create (1, 11, 21), tuple);
		}
Ejemplo n.º 2
0
        public void LinkToPredicateMaxMessagesTest()
        {
            var scheduler = new TestScheduler();
            var source    = new BufferBlock <int> (
                new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new BufferBlock <int> ();

            source.LinkTo(target, new DataflowLinkOptions {
                MaxMessages = 1
            },
                          i => i % 2 == 1);

            Assert.IsTrue(source.Post(2));
            Assert.IsTrue(source.Post(1));
            Assert.IsTrue(source.Post(3));

            scheduler.ExecuteAll();

            int item;

            Assert.IsFalse(target.TryReceive(out item));
            Assert.IsTrue(source.TryReceive(out item));
            Assert.AreEqual(2, item);

            scheduler.ExecuteAll();

            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(1, item);

            scheduler.ExecuteAll();

            Assert.IsFalse(target.TryReceive(out item));
        }
Ejemplo n.º 3
0
        public void ConditionalDecliningTest()
        {
            var scheduler = new TestScheduler();
            var source    =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new TestTargetBlock <int> {
                Decline = true
            };

            Assert.IsNotNull(source.LinkTo(target));
            Assert.AreEqual(default(int), target.DirectlyAccepted);

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();
            Assert.AreEqual(default(int), target.DirectlyAccepted);

            target.Decline = false;
            Assert.IsTrue(source.Post(43));
            scheduler.ExecuteAll();
            Assert.AreEqual(default(int), target.DirectlyAccepted);

            Assert.AreEqual(42, source.Receive(TimeSpan.FromMilliseconds(100)));
            scheduler.ExecuteAll();
            Assert.AreEqual(43, target.DirectlyAccepted);
        }
Ejemplo n.º 4
0
        public void MaxNumberOfGroupsTest()
        {
            var scheduler = new TestScheduler();

            var block = new BatchBlock <int> (2,
                                              new GroupingDataflowBlockOptions
            {
                MaxNumberOfGroups = 2, TaskScheduler = scheduler
            });

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

            Assert.IsTrue(block.Post(3));
            Assert.IsTrue(block.Post(4));

            Assert.IsFalse(block.Post(5));

            scheduler.ExecuteAll();

            int[] batch;
            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEqual(new[] { 1, 2 }, batch);

            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEqual(new[] { 3, 4 }, batch);

            scheduler.ExecuteAll();

            Assert.IsTrue(block.Completion.Wait(100));
        }
        public void DontOfferTwiceTest()
        {
            var scheduler = new TestScheduler();
            var block     = new WriteOnceBlock <int> (null,
                                                      new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target =
                new TestTargetBlock <int> {
                Postpone = true
            };

            block.LinkTo(target);

            Assert.IsFalse(target.HasPostponed);

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

            scheduler.ExecuteAll();

            Assert.IsTrue(target.HasPostponed);

            target.Postpone = false;

            int value;

            Assert.IsTrue(target.RetryPostponed(out value));
            Assert.AreEqual(1, value);

            block.LinkTo(new BufferBlock <int> ());

            scheduler.ExecuteAll();

            Assert.AreEqual(default(int), target.DirectlyAccepted);
        }
Ejemplo n.º 6
0
        public void ConsumeAfterReceiveTest()
        {
            var scheduler = new TestScheduler();
            var source    =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

            Assert.IsNotNull(source.LinkTo(target));
            Assert.IsFalse(target.HasPostponed);

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();
            Assert.IsTrue(target.HasPostponed);

            target.Postpone = false;

            Assert.AreEqual(42, source.Receive());
            Assert.IsTrue(source.Post(43));

            Assert.AreEqual(default(int), target.DirectlyAccepted);

            int i;

            Assert.IsFalse(target.RetryPostponed(out i));
            Assert.AreEqual(default(int), i);

            scheduler.ExecuteAll();

            Assert.AreEqual(43, target.DirectlyAccepted);
        }
Ejemplo n.º 7
0
        public void ReserveChangedTest()
        {
            var scheduler = new TestScheduler();
            var broadcast = new BroadcastBlock <int> (null,
                                                      new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

            broadcast.LinkTo(target);

            Assert.IsFalse(target.HasPostponed);

            Assert.IsTrue(broadcast.Post(1));

            scheduler.ExecuteAll();

            Assert.IsTrue(target.HasPostponed);

            Assert.IsTrue(broadcast.Post(2));

            scheduler.ExecuteAll();

            Assert.IsTrue(target.ReservePostponed());

            int value;

            Assert.IsTrue(target.RetryPostponed(out value));
            Assert.AreEqual(2, value);
        }
Ejemplo n.º 8
0
        public void LinkToPredicatePostponed()
        {
            var scheduler = new TestScheduler();
            var source    = new BufferBlock <int> (
                new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new BufferBlock <int> (
                new DataflowBlockOptions {
                BoundedCapacity = 1, TaskScheduler = scheduler
            });

            source.LinkTo(target, i => true);

            Assert.IsTrue(target.Post(1));
            Assert.IsTrue(source.Post(2));

            scheduler.ExecuteAll();

            int item;

            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(1, item);

            scheduler.ExecuteAll();

            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(2, item);
        }
Ejemplo n.º 9
0
        public void ReserveFaultConsumeBroadcast()
        {
            var scheduler = new TestScheduler();
            var source    = new BroadcastBlock <int> (null,
                                                      new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

            Assert.IsNotNull(source.LinkTo(target));

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();
            Assert.IsTrue(target.HasPostponed);

            Assert.IsTrue(target.ReservePostponed());

            ((IDataflowBlock)source).Fault(new Exception());

            scheduler.ExecuteAll();
            Thread.Sleep(100);

            int value;

            Assert.IsTrue(target.RetryPostponed(out value));
        }
Ejemplo n.º 10
0
        public void MaxMessagesDirectTest()
        {
            var scheduler = new TestScheduler();
            var source    =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(
                source.LinkTo(target, new DataflowLinkOptions {
                MaxMessages = 1
            }));

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();

            int item;

            Assert.IsTrue(target.TryReceive(null, out item));
            Assert.AreEqual(42, item);

            Assert.IsTrue(source.Post(43));
            scheduler.ExecuteAll();
            Assert.IsFalse(target.TryReceive(null, out item));
            Assert.IsTrue(source.TryReceive(null, out item));
            Assert.AreEqual(43, item);
        }
Ejemplo n.º 11
0
        public void NonGreedyBatchWithBoundedCapacityTriggerTest2()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchBlock <int> (3,
                                                  new GroupingDataflowBlockOptions
            {
                Greedy = false, BoundedCapacity = 3, TaskScheduler = scheduler
            });
            var source1 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source2 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source1.LinkTo(block));
            Assert.IsNotNull(source2.LinkTo(block));

            // trigger 2, then trigger another 2 and then trigger 2 once more
            // while havaing capacity of 3

            Assert.IsTrue(source1.Post(11));
            Assert.IsTrue(source2.Post(21));
            block.TriggerBatch();

            scheduler.ExecuteAll();

            Assert.IsTrue(source1.Post(12));
            Assert.IsTrue(source2.Post(22));
            block.TriggerBatch();

            scheduler.ExecuteAll();

            Assert.IsTrue(source1.Post(13));
            Assert.IsTrue(source2.Post(23));
            block.TriggerBatch();

            scheduler.ExecuteAll();

            int i;

            Assert.IsTrue(source1.TryReceive(out i));
            Assert.AreEqual(13, i);
            Assert.IsTrue(source2.TryReceive(out i));
            Assert.AreEqual(23, i);

            int[] batch;
            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEquivalent(new[] { 11, 21 }, batch);

            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEquivalent(new[] { 12, 22 }, batch);

            Assert.IsFalse(block.TryReceive(out batch));
        }
Ejemplo n.º 12
0
        public void NonGreedyJoin3WithBoundedCapacityTest()
        {
            var scheduler = new TestScheduler();
            var block     = new JoinBlock <int, int, int> (
                new GroupingDataflowBlockOptions
            {
                Greedy = false, BoundedCapacity = 1, TaskScheduler = scheduler
            });
            var source1 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source2 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source3 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source1.LinkTo(block.Target1));
            Assert.IsNotNull(source2.LinkTo(block.Target2));
            Assert.IsNotNull(source3.LinkTo(block.Target3));

            Assert.IsTrue(source1.Post(11));
            Assert.IsTrue(source2.Post(21));
            Assert.IsTrue(source3.Post(31));

            scheduler.ExecuteAll();

            Assert.IsTrue(source1.Post(12));
            Assert.IsTrue(source2.Post(22));
            Assert.IsTrue(source3.Post(32));

            scheduler.ExecuteAll();

            int i;

            Assert.IsTrue(source1.TryReceive(out i));
            Assert.AreEqual(12, i);

            Assert.IsTrue(source1.Post(13));

            Tuple <int, int, int> tuple;

            Assert.IsTrue(block.TryReceive(out tuple));
            Assert.AreEqual(Tuple.Create(11, 21, 31), tuple);

            scheduler.ExecuteAll();

            Assert.IsTrue(block.TryReceive(out tuple));
            Assert.AreEqual(Tuple.Create(13, 22, 32), tuple);
        }
Ejemplo n.º 13
0
        public void NonGreedyBatchTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchBlock <int> (3,
                                                  new GroupingDataflowBlockOptions
            {
                Greedy = false, TaskScheduler = scheduler
            });

            Assert.IsFalse(block.Post(42));

            var source =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source.LinkTo(block));

            Assert.IsTrue(source.Post(43));

            scheduler.ExecuteAll();

            int i;

            Assert.IsTrue(source.TryReceive(null, out i));
            Assert.AreEqual(43, i);

            Assert.IsTrue(source.Post(44));
            Assert.IsTrue(source.Post(45));

            var source2 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source2.LinkTo(block));

            Assert.IsTrue(source2.Post(142));

            scheduler.ExecuteAll();

            int[] batch;
            Assert.IsFalse(block.TryReceive(null, out batch));
            Assert.IsNull(batch);

            block.TriggerBatch();
            scheduler.ExecuteAll();
            Assert.IsTrue(block.TryReceive(null, out batch));
            CollectionAssert.AreEquivalent(new[] { 44, 142 }, batch);
        }
Ejemplo n.º 14
0
        public void GreedyBatchWithBoundedCapacityTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchBlock <int> (3,
                                                  new GroupingDataflowBlockOptions
            {
                Greedy = true, BoundedCapacity = 3, TaskScheduler = scheduler
            });

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

            block.TriggerBatch();

            scheduler.ExecuteAll();

            Assert.IsTrue(block.Post(3));
            Assert.IsFalse(block.Post(4));

            int[] batch;
            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEqual(new[] { 1, 2 }, batch);

            Assert.IsTrue(block.Post(5));
            Assert.IsTrue(block.Post(6));
        }
Ejemplo n.º 15
0
        public void AsyncExceptionTest()
        {
            var scheduler = new TestScheduler();
            var exception = new Exception();

            var blocks = GetExecutionBlocksWithAsyncAction(
                i =>
            {
                var tcs = new TaskCompletionSource <int> ();
                tcs.SetException(exception);
                return(tcs.Task);
            },
                new ExecutionDataflowBlockOptions {
                TaskScheduler = scheduler
            });

            foreach (var block in blocks)
            {
                Assert.IsTrue(block.Post(1));

                // the task should be executed on the configured TaskScheduler
                Assert.IsFalse(block.Completion.Wait(100));

                scheduler.ExecuteAll();

                var ae =
                    AssertEx.Throws <AggregateException> (() => block.Completion.Wait(1000)).
                    Flatten();

                Assert.AreEqual(1, ae.InnerExceptions.Count);
                Assert.AreSame(exception, ae.InnerException);
            }
        }
Ejemplo n.º 16
0
        public void QueuedMessagesTest()
        {
            var scheduler = new TestScheduler();
            var broadcast = new BroadcastBlock <int> (null,
                                                      new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new BufferBlock <int> ();

            broadcast.LinkTo(target);

            Assert.IsTrue(broadcast.Post(1));
            Assert.IsTrue(broadcast.Post(2));

            AssertEx.Throws <TimeoutException> (
                () => target.Receive(TimeSpan.FromMilliseconds(100)));

            scheduler.ExecuteAll();

            int item;

            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(1, item);
            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(2, item);
        }
Ejemplo n.º 17
0
        public void MaxNumberOfGroupsTest()
        {
            var scheduler = new TestScheduler();
            var block     = new JoinBlock <int, int> (
                new GroupingDataflowBlockOptions
            {
                MaxNumberOfGroups = 1, TaskScheduler = scheduler
            });

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

            Assert.IsFalse(block.Target1.Post(2));

            Assert.IsTrue(block.Target2.Post(3));

            Assert.IsFalse(block.Target2.Post(4));

            Tuple <int, int> batch;

            Assert.IsTrue(block.TryReceive(out batch));
            Assert.AreEqual(Tuple.Create(1, 3), batch);

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

            scheduler.ExecuteAll();

            Assert.IsTrue(block.Completion.Wait(1000));
        }
Ejemplo n.º 18
0
        public void MaxNumberOfGroupsTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchedJoinBlock <int, int> (1,
                                                             new GroupingDataflowBlockOptions
            {
                MaxNumberOfGroups = 2, TaskScheduler = scheduler
            });

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

            Assert.IsTrue(block.Target2.Post(2));

            Assert.IsFalse(block.Target2.Post(3));
            Assert.IsFalse(block.Target1.Post(4));

            Tuple <IList <int>, IList <int> > batch;

            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEqual(new[] { 1 }, batch.Item1);
            CollectionAssert.IsEmpty(batch.Item2);

            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.IsEmpty(batch.Item1);
            CollectionAssert.AreEqual(new[] { 2 }, batch.Item2);

            scheduler.ExecuteAll();

            Assert.IsTrue(block.Completion.Wait(100));
        }
Ejemplo n.º 19
0
        public void DontConsumePostponedAfterCompleteTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BufferBlock <int> (
                new DataflowBlockOptions {
                BoundedCapacity = 1, TaskScheduler = scheduler
            });
            var target = (ITargetBlock <int>)block;
            var source = new TestSourceBlock <int> ();

            Assert.IsTrue(block.Post(11));

            var header = new DataflowMessageHeader(1);

            source.AddMessage(header, 12);
            Assert.AreEqual(DataflowMessageStatus.Postponed,
                            target.OfferMessage(header, 12, source, false));

            block.Complete();

            Assert.AreEqual(11, block.Receive());

            scheduler.ExecuteAll();

            Assert.IsFalse(source.WasConsumed(header));
        }
Ejemplo n.º 20
0
        public void TransformManyOverfullTest2()
        {
            var scheduler = new TestScheduler();

            n = 0;
            var transform = new TransformManyBlock <int, int> (
                i => ComputeResults(),
                new ExecutionDataflowBlockOptions
            {
                BoundedCapacity = 100, TaskScheduler = scheduler
            });

            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(transform.Post(i));
            }

            Assert.IsFalse(transform.Post(101));

            Assert.GreaterOrEqual(scheduler.ExecuteAll(), 1);

            Assert.IsFalse(transform.Post(102));

            Assert.AreEqual(10000, Volatile.Read(ref n));
        }
Ejemplo n.º 21
0
        public void MultipleOffersTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BufferBlock <int> (
                new DataflowBlockOptions {
                BoundedCapacity = 1, TaskScheduler = scheduler
            });
            var target = (ITargetBlock <int>)block;
            var source = new TestSourceBlock <int> ();

            var header1 = new DataflowMessageHeader(1);

            Assert.AreEqual(DataflowMessageStatus.Accepted,
                            target.OfferMessage(header1, 41, source, false));

            var header2 = new DataflowMessageHeader(2);

            source.AddMessage(header2, 42);
            Assert.AreEqual(DataflowMessageStatus.Postponed,
                            target.OfferMessage(header2, 42, source, false));

            var header3 = new DataflowMessageHeader(3);

            source.AddMessage(header3, 43);
            Assert.AreEqual(DataflowMessageStatus.Postponed,
                            target.OfferMessage(header3, 43, source, false));

            Assert.AreEqual(41, block.Receive());
            scheduler.ExecuteAll();
            Assert.IsTrue(source.WasConsumed(header3));
            Assert.IsFalse(source.WasConsumed(header2));
        }
Ejemplo n.º 22
0
        public void PostAfterCancellation()
        {
            var scheduler = new TestScheduler();
            var block     =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            var tokenSource = new CancellationTokenSource();

            Task.Factory.StartNew(
                () =>
            {
                Thread.Sleep(100);
                tokenSource.Cancel();
            });

            AssertEx.Throws <OperationCanceledException> (
                () => block.Receive(tokenSource.Token));

            block.Post(1);

            scheduler.ExecuteAll();

            int item;

            Assert.IsTrue(block.TryReceive(out item));
            Assert.AreEqual(1, item);
        }
Ejemplo n.º 23
0
        public void PostponeTwoTargetsTest()
        {
            var scheduler = new TestScheduler();
            var source    =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target1 = new TestTargetBlock <int> {
                Postpone = true
            };
            var target2 = new TestTargetBlock <int> {
                Postpone = true
            };

            Assert.IsNotNull(source.LinkTo(target1));
            Assert.IsNotNull(source.LinkTo(target2));
            Assert.IsFalse(target1.HasPostponed);
            Assert.IsFalse(target2.HasPostponed);

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();
            Assert.IsTrue(target1.HasPostponed);
            Assert.IsTrue(target2.HasPostponed);

            int i;

            Assert.IsTrue(target2.RetryPostponed(out i));
            Assert.AreEqual(42, i);

            Assert.IsFalse(target1.RetryPostponed(out i));
            Assert.AreEqual(default(int), i);
        }
Ejemplo n.º 24
0
        public void ReserveTest()
        {
            var scheduler = new TestScheduler();
            var source    =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

            Assert.IsNotNull(source.LinkTo(target));
            Assert.IsFalse(target.HasPostponed);

            Assert.IsTrue(source.Post(42));
            Assert.IsTrue(source.Post(43));
            scheduler.ExecuteAll();
            Assert.IsTrue(target.HasPostponed);

            Assert.IsTrue(target.ReservePostponed());
            int i;

            Assert.IsFalse(source.TryReceive(null, out i));
            Assert.AreEqual(default(int), i);
            IList <int> items;

            Assert.IsFalse(source.TryReceiveAll(out items));
            Assert.AreEqual(default(IList <int>), items);

            Assert.IsTrue(target.RetryPostponed(out i));
            Assert.AreEqual(42, i);

            Assert.IsTrue(source.TryReceive(null, out i));
            Assert.AreEqual(43, i);
        }
Ejemplo n.º 25
0
        public void NonGreedyJoin3Test()
        {
            var scheduler = new TestScheduler();
            var block     =
                new JoinBlock <int, int, int> (new GroupingDataflowBlockOptions
            {
                TaskScheduler = scheduler, Greedy = false
            });
            var source1 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source2 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source3 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source1.LinkTo(block.Target1));
            Assert.IsNotNull(source2.LinkTo(block.Target2));
            Assert.IsNotNull(source3.LinkTo(block.Target3));

            Assert.IsTrue(source1.Post(1));
            scheduler.ExecuteAll();

            int i;

            Assert.IsTrue(source1.TryReceive(out i));

            Assert.IsTrue(source1.Post(2));
            Assert.IsTrue(source2.Post(11));
            Assert.IsTrue(source3.Post(21));
            scheduler.ExecuteAll();

            Assert.IsFalse(source1.TryReceive(out i));
            Assert.IsFalse(source2.TryReceive(out i));
            Assert.IsFalse(source3.TryReceive(out i));

            Tuple <int, int, int> tuple;

            Assert.IsTrue(block.TryReceive(out tuple));
            Assert.AreEqual(Tuple.Create(2, 11, 21), tuple);
        }
Ejemplo n.º 26
0
        public void NonGreedyBatchWithBoundedCapacityTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchBlock <int> (2,
                                                  new GroupingDataflowBlockOptions
            {
                Greedy = false, BoundedCapacity = 2, TaskScheduler = scheduler
            });
            var source1 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source2 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source1.LinkTo(block));
            Assert.IsNotNull(source2.LinkTo(block));

            Assert.IsTrue(source1.Post(11));
            Assert.IsTrue(source2.Post(21));

            scheduler.ExecuteAll();

            Assert.IsTrue(source1.Post(12));
            Assert.IsTrue(source2.Post(22));

            scheduler.ExecuteAll();

            int i;

            Assert.IsTrue(source1.TryReceive(out i));
            Assert.AreEqual(12, i);

            Assert.IsTrue(source1.Post(13));

            int[] batch;
            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEquivalent(new[] { 11, 21 }, batch);

            scheduler.ExecuteAll();

            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEquivalent(new[] { 13, 22 }, batch);
        }
Ejemplo n.º 27
0
        public void NonGreedyBatchWithMoreSourcesTest()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchBlock <int> (2,
                                                  new GroupingDataflowBlockOptions
            {
                Greedy = false, TaskScheduler = scheduler
            });

            var source1 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var source2 =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });

            Assert.IsNotNull(source1.LinkTo(block));
            Assert.IsNotNull(source2.LinkTo(block));

            Assert.IsTrue(source1.Post(43));

            scheduler.ExecuteAll();

            int i;

            Assert.IsTrue(source1.TryReceive(out i));
            Assert.AreEqual(43, i);

            Assert.IsTrue(source1.Post(44));
            Assert.IsTrue(source2.Post(45));

            scheduler.ExecuteAll();

            int[] batch;
            Assert.IsTrue(block.TryReceive(out batch));
            CollectionAssert.AreEquivalent(new[] { 44, 45 }, batch);
        }
Ejemplo n.º 28
0
        public void AsyncTest()
        {
            var tcs    = new TaskCompletionSource <int> ();
            int result = 0;

            var scheduler = new TestScheduler();

            var blocks = GetExecutionBlocksWithAsyncAction(
                i =>
                tcs.Task.ContinueWith(t => Volatile.Write(ref result, i + t.Result)),
                new ExecutionDataflowBlockOptions {
                TaskScheduler = scheduler
            });

            foreach (var block in blocks)
            {
                Assert.IsTrue(block.Post(1));

                scheduler.ExecuteAll();
                Thread.Sleep(100);
                Thread.MemoryBarrier();

                Assert.AreEqual(0, result);

                tcs.SetResult(10);

                Thread.Sleep(100);

                // the continuation should be executed on the configured TaskScheduler
                Assert.AreEqual(0, result);

                scheduler.ExecuteAll();

                Assert.AreEqual(11, result);

                tcs = new TaskCompletionSource <int> ();
                Volatile.Write(ref result, 0);
            }
        }
Ejemplo n.º 29
0
        public void MaxMessagesBroadcastTest()
        {
            var scheduler = new TestScheduler();
            var source    = new BroadcastBlock <int> (
                null, new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new BufferBlock <int>(
                new DataflowBlockOptions {
                TaskScheduler = scheduler, BoundedCapacity = 1
            });

            Assert.IsNotNull(
                source.LinkTo(target, new DataflowLinkOptions {
                MaxMessages = 2
            }));

            // should be accepted
            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();

            // should be postponed, but counted into the limit
            Assert.IsTrue(source.Post(43));
            scheduler.ExecuteAll();

            // shouldn't be even offered for now
            Assert.IsTrue(source.Post(44));
            scheduler.ExecuteAll();

            int item;

            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(42, item);

            scheduler.ExecuteAll();
            Assert.IsTrue(target.TryReceive(out item));
            Assert.AreEqual(44, item);
        }
Ejemplo n.º 30
0
		public void ImmediateTest()
		{
			var scheduler = new TestScheduler();
			var source = new BufferBlock<int>(new DataflowBlockOptions{TaskScheduler = scheduler});

			Assert.IsTrue(source.Post(1));

			var task = source.OutputAvailableAsync();

			// not necessary in MS.NET
			scheduler.ExecuteAll();

			Assert.IsTrue(task.IsCompleted);
			Assert.IsTrue(task.Result);
		}
Ejemplo n.º 31
0
        public void FaultConsumeBroadcast()
        {
            var scheduler = new TestScheduler();
            var source    = new BroadcastBlock <int> (null,
                                                      new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

            Assert.IsNotNull(source.LinkTo(target));

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();
            Assert.IsTrue(target.HasPostponed);

            var exception = new Exception();

            ((IDataflowBlock)source).Fault(exception);

            scheduler.ExecuteAll();

            try {
                source.Completion.Wait(1000);
                Assert.Fail("Task must be faulted");
            } catch (AggregateException ex) {
                Assert.AreEqual(exception, ex.InnerException, "#9");
            }

            Assert.IsTrue(source.Completion.IsFaulted);
            int value;

            Assert.IsTrue(target.RetryPostponed(out value));
            Assert.AreEqual(42, value);
        }
Ejemplo n.º 32
0
        public void TriggerBatchWhenEmpty()
        {
            var scheduler = new TestScheduler();
            var block     = new BatchBlock <int> (5,
                                                  new GroupingDataflowBlockOptions {
                TaskScheduler = scheduler
            });

            block.TriggerBatch();

            scheduler.ExecuteAll();

            int[] batch;
            Assert.IsFalse(block.TryReceive(out batch));
            Assert.IsNull(batch);
        }
Ejemplo n.º 33
0
		public void AsyncNullTest()
		{
			var scheduler = new TestScheduler ();
			var block = new ActionBlock<int> (
				i => null,
				new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });

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

			scheduler.ExecuteAll ();

			Assert.IsFalse (block.Completion.Wait (100));

			block.Complete ();

			Assert.IsTrue (block.Completion.Wait (100));
		}
Ejemplo n.º 34
0
		public void AsyncCancelledTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new TransformBlock<int, int> (
				i =>
				{
					var tcs = new TaskCompletionSource<int> ();
					tcs.SetCanceled ();
					return tcs.Task;
				}, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });

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

			scheduler.ExecuteAll ();

			Assert.IsFalse (block.Completion.Wait (100));
		}
Ejemplo n.º 35
0
		public void LinkToPredicatePostponed ()
		{
			var scheduler = new TestScheduler ();
			var source = new BufferBlock<int> (
				new DataflowBlockOptions { TaskScheduler = scheduler });
			var target = new BufferBlock<int> (
				new DataflowBlockOptions { BoundedCapacity = 1, TaskScheduler = scheduler });
			source.LinkTo (target, i => true);

			Assert.IsTrue (target.Post (1));
			Assert.IsTrue (source.Post (2));

			scheduler.ExecuteAll ();

			int item;
			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (1, item);

			scheduler.ExecuteAll ();

			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (2, item);
		}
Ejemplo n.º 36
0
		public void TryReceiveAllTest()
		{
			var scheduler = new TestScheduler();
			var block = new BroadcastBlock<int>(null, new DataflowBlockOptions { TaskScheduler = scheduler });
			IReceivableSourceBlock<int> source = block;

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

			scheduler.ExecuteAll();

			IList<int> items;
			Assert.IsTrue(source.TryReceiveAll(out items));

			CollectionAssert.AreEqual(new[] { 2 }, items);
		}
Ejemplo n.º 37
0
		public void BoundedPostponedTest ()
		{
			var scheduler = new TestScheduler ();
			var broadcast = new BroadcastBlock<int> (
				null,
				new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 });
			ITargetBlock<int> target = broadcast;
			var source = new TestSourceBlock<int> ();

			Assert.IsTrue (broadcast.Post (1));
			var header = new DataflowMessageHeader (1);
			source.AddMessage (header, 2);
			Assert.AreEqual (DataflowMessageStatus.Postponed,
				target.OfferMessage (header, 2, source, false));
			Assert.IsFalse (source.WasConsumed (header));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source.WasConsumed (header));
		}
Ejemplo n.º 38
0
		public void ReserveChangedTest ()
		{
			var scheduler = new TestScheduler ();
			var broadcast = new BroadcastBlock<int> (null,
				new DataflowBlockOptions { TaskScheduler = scheduler });
			var target = new TestTargetBlock<int> { Postpone = true };

			broadcast.LinkTo (target);

			Assert.IsFalse (target.HasPostponed);

			Assert.IsTrue (broadcast.Post (1));

			scheduler.ExecuteAll ();

			Assert.IsTrue (target.HasPostponed);

			Assert.IsTrue(broadcast.Post(2));

			scheduler.ExecuteAll ();

			Assert.IsTrue (target.ReservePostponed ());

			int value;
			Assert.IsTrue (target.RetryPostponed (out value));
			Assert.AreEqual (2, value);
		}
Ejemplo n.º 39
0
		public void NonGreedyBatchWithMoreSourcesTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (2,
				new GroupingDataflowBlockOptions
				{ Greedy = false, TaskScheduler = scheduler });

			var source1 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source2 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source1.LinkTo (block));
			Assert.IsNotNull (source2.LinkTo (block));

			Assert.IsTrue (source1.Post (43));

			scheduler.ExecuteAll ();

			int i;
			Assert.IsTrue (source1.TryReceive (out i));
			Assert.AreEqual (43, i);

			Assert.IsTrue (source1.Post (44));
			Assert.IsTrue (source2.Post (45));

			scheduler.ExecuteAll ();

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 44, 45 }, batch);
		}
Ejemplo n.º 40
0
		public void MaxMessagesPostponedUnconsumedTest ()
		{
			var scheduler = new TestScheduler ();
			var source =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var target =
				new BufferBlock<int> (
					new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 });
			Assert.IsNotNull (
				source.LinkTo (target, new DataflowLinkOptions { MaxMessages = 2 }));

			Assert.IsTrue (source.Post (42));
			Assert.IsTrue (source.Post (43));
			Assert.IsTrue (source.Post (44));
			Assert.IsTrue (source.Post (45));
			scheduler.ExecuteAll ();

			int item;
			Assert.IsTrue (source.TryReceive (null, out item));
			Assert.AreEqual (43, item);

			Assert.IsTrue (target.TryReceive (null, out item));
			Assert.AreEqual (42, item);
			Assert.IsFalse (target.TryReceive (null, out item));

			scheduler.ExecuteAll ();

			Assert.IsTrue (target.TryReceive (null, out item));
			Assert.AreEqual (44, item);

			scheduler.ExecuteAll ();

			Assert.IsFalse (target.TryReceive (null, out item));
			Assert.IsTrue (source.TryReceive (null, out item));
			Assert.AreEqual (45, item);
		}
Ejemplo n.º 41
0
		public void TriggerBatchWhenEmpty ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (5,
				new GroupingDataflowBlockOptions { TaskScheduler = scheduler });
			block.TriggerBatch ();

			scheduler.ExecuteAll ();

			int[] batch;
			Assert.IsFalse (block.TryReceive (out batch));
			Assert.IsNull (batch);
		}
Ejemplo n.º 42
0
		public void NonGreedyJoin3WithBoundedCapacityTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new JoinBlock<int, int, int> (
				new GroupingDataflowBlockOptions
				{ Greedy = false, BoundedCapacity = 1, TaskScheduler = scheduler });
			var source1 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source2 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source3 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source1.LinkTo (block.Target1));
			Assert.IsNotNull (source2.LinkTo (block.Target2));
			Assert.IsNotNull (source3.LinkTo (block.Target3));

			Assert.IsTrue (source1.Post (11));
			Assert.IsTrue (source2.Post (21));
			Assert.IsTrue (source3.Post (31));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.Post (12));
			Assert.IsTrue (source2.Post (22));
			Assert.IsTrue (source3.Post (32));

			scheduler.ExecuteAll ();

			int i;
			Assert.IsTrue (source1.TryReceive (out i));
			Assert.AreEqual (12, i);

			Assert.IsTrue (source1.Post (13));

			Tuple<int, int, int> tuple;
			Assert.IsTrue (block.TryReceive (out tuple));
			Assert.AreEqual (Tuple.Create (11, 21, 31), tuple);

			scheduler.ExecuteAll ();

			Assert.IsTrue (block.TryReceive (out tuple));
			Assert.AreEqual (Tuple.Create (13, 22, 32), tuple);
		}
Ejemplo n.º 43
0
		public void AsyncTest ()
		{
			var tcs = new TaskCompletionSource<int> ();
			int result = 0;

			var scheduler = new TestScheduler ();

			var blocks = GetExecutionBlocksWithAsyncAction (
				i =>
				tcs.Task.ContinueWith (t => Volatile.Write (ref result, i + t.Result)),
				new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });

			foreach (var block in blocks) {
				Assert.IsTrue (block.Post (1));

				scheduler.ExecuteAll ();
				Thread.Sleep (100);
				Thread.MemoryBarrier ();

				Assert.AreEqual (0, result);

				tcs.SetResult (10);

				Thread.Sleep (100);

				// the continuation should be executed on the configured TaskScheduler
				Assert.AreEqual (0, result);

				scheduler.ExecuteAll ();

				Assert.AreEqual (11, result);

				tcs = new TaskCompletionSource<int> ();
				Volatile.Write (ref result, 0);
			}
		}
Ejemplo n.º 44
0
		public void AsyncExceptionTest ()
		{
			var scheduler = new TestScheduler ();
			var exception = new Exception ();

			var blocks = GetExecutionBlocksWithAsyncAction (
				i =>
				{
					var tcs = new TaskCompletionSource<int> ();
					tcs.SetException (exception);
					return tcs.Task;
				},
				new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });

			foreach (var block in blocks) {
				Assert.IsTrue (block.Post (1));

				// the task should be executed on the configured TaskScheduler
				Assert.IsFalse (block.Completion.Wait (100));

				scheduler.ExecuteAll ();

				var ae =
					AssertEx.Throws<AggregateException> (() => block.Completion.Wait (1000)).
						Flatten ();

				Assert.AreEqual (1, ae.InnerExceptions.Count);
				Assert.AreSame (exception, ae.InnerException);
			}
		}
Ejemplo n.º 45
0
		public void MaxNumberOfGroupsTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new JoinBlock<int, int> (
				new GroupingDataflowBlockOptions
				{ MaxNumberOfGroups = 1, TaskScheduler = scheduler });

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

			Assert.IsFalse (block.Target1.Post (2));

			Assert.IsTrue (block.Target2.Post (3));

			Assert.IsFalse (block.Target2.Post (4));

			Tuple<int, int> batch;
			Assert.IsTrue (block.TryReceive (out batch));
			Assert.AreEqual (Tuple.Create (1, 3), batch);

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

			scheduler.ExecuteAll ();

			Assert.IsTrue (block.Completion.Wait (1000));
		}
Ejemplo n.º 46
0
		public void NonGreedyMaxNumberOfGroupsTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new JoinBlock<int, int> (
				new GroupingDataflowBlockOptions
				{ MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler });
			var source1 = new TestSourceBlock<int> ();
			var source2 = new TestSourceBlock<int> ();

			var header1 = new DataflowMessageHeader (1);
			source1.AddMessage (header1, 11);
			source2.AddMessage (header1, 21);

			Assert.AreEqual (DataflowMessageStatus.Postponed,
				block.Target1.OfferMessage (header1, 11, source1, false));
			Assert.AreEqual (DataflowMessageStatus.Postponed,
				block.Target2.OfferMessage (header1, 21, source2, false));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.WasConsumed (header1));
			Assert.IsTrue (source2.WasConsumed (header1));

			var header2 = new DataflowMessageHeader (2);
			Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
				block.Target1.OfferMessage (header2, 21, source1, false));

			Tuple<int, int> tuple;
			Assert.IsTrue (block.TryReceive (out tuple));
			Assert.AreEqual (Tuple.Create (11, 21), tuple);

			Assert.IsTrue (block.Completion.Wait (1000));
		}
Ejemplo n.º 47
0
		public void LinkToPredicateMaxMessagesTest ()
		{
			var scheduler = new TestScheduler ();
			var source = new BufferBlock<int> (
				new DataflowBlockOptions { TaskScheduler = scheduler });
			var target = new BufferBlock<int> ();
			source.LinkTo (target, new DataflowLinkOptions { MaxMessages = 1 },
				i => i % 2 == 1);

			Assert.IsTrue (source.Post (2));
			Assert.IsTrue (source.Post (1));
			Assert.IsTrue (source.Post (3));

			scheduler.ExecuteAll ();

			int item;
			Assert.IsFalse (target.TryReceive (out item));
			Assert.IsTrue (source.TryReceive (out item));
			Assert.AreEqual (2, item);

			scheduler.ExecuteAll ();

			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (1, item);

			scheduler.ExecuteAll ();
			
			Assert.IsFalse (target.TryReceive (out item));
		}
Ejemplo n.º 48
0
		public void TaskSchedulerTest ()
		{
			var scheduler = new TestScheduler ();

			int n = 0;

			var action = new ActionBlock<int> (
				i => Interlocked.Increment (ref n),
				new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });

			Assert.IsTrue (action.Post (1));

			Assert.AreEqual (0, Volatile.Read (ref n));

			Assert.AreEqual (1, scheduler.ExecuteAll ());
			Assert.AreEqual (1, Volatile.Read (ref n));
		}
Ejemplo n.º 49
0
		public void DefaultSchedulerIsDefaultTest ()
		{
			var scheduler = new TestScheduler ();
			var factory = new TaskFactory (scheduler);

			ActionBlock<int> action = null;

			var task = factory.StartNew (() =>
			{
				Assert.AreEqual (scheduler, TaskScheduler.Current);

				action = new ActionBlock<int> (
					i => Assert.AreNotEqual (scheduler, TaskScheduler.Current));
				Assert.IsTrue (action.Post (1));
				action.Complete ();
			});

			Assert.AreEqual (1, scheduler.ExecuteAll ());

			Assert.IsNotNull (action);

			Assert.IsTrue (action.Completion.Wait (1000));
			Assert.IsTrue (task.Wait (0));
		}
Ejemplo n.º 50
0
		public void NonGreedyBatchWithBoundedCapacityTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (2,
				new GroupingDataflowBlockOptions
				{ Greedy = false, BoundedCapacity = 2, TaskScheduler = scheduler });
			var source1 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source2 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source1.LinkTo (block));
			Assert.IsNotNull (source2.LinkTo (block));

			Assert.IsTrue (source1.Post (11));
			Assert.IsTrue (source2.Post (21));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.Post (12));
			Assert.IsTrue (source2.Post (22));

			scheduler.ExecuteAll ();

			int i;
			Assert.IsTrue (source1.TryReceive (out i));
			Assert.AreEqual (12, i);

			Assert.IsTrue (source1.Post (13));

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch);

			scheduler.ExecuteAll ();

			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 13, 22 }, batch);
		}
Ejemplo n.º 51
0
		public void MaxMessagesBroadcastTest ()
		{
			var scheduler = new TestScheduler ();
			var source = new BroadcastBlock<int> (
				null, new DataflowBlockOptions { TaskScheduler = scheduler });
			var target = new BufferBlock<int>(
				new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 });
			Assert.IsNotNull (
				source.LinkTo (target, new DataflowLinkOptions { MaxMessages = 2 }));

			// should be accepted
			Assert.IsTrue (source.Post (42));
			scheduler.ExecuteAll ();

			// should be postponed, but counted into the limit
			Assert.IsTrue (source.Post (43));
			scheduler.ExecuteAll ();

			// shouldn't be even offered for now
			Assert.IsTrue (source.Post (44));
			scheduler.ExecuteAll ();

			int item;
			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (42, item);

			scheduler.ExecuteAll ();
			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (44, item);
		}
Ejemplo n.º 52
0
		public void GreedyBatchWithBoundedCapacityTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (3,
				new GroupingDataflowBlockOptions
				{ Greedy = true, BoundedCapacity = 3, TaskScheduler = scheduler });

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

			block.TriggerBatch ();

			scheduler.ExecuteAll ();

			Assert.IsTrue (block.Post (3));
			Assert.IsFalse (block.Post (4));

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEqual (new[] { 1, 2 }, batch);

			Assert.IsTrue (block.Post (5));
			Assert.IsTrue (block.Post (6));
		}
Ejemplo n.º 53
0
		public void NonGreedyBatchTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (3,
				new GroupingDataflowBlockOptions
				{ Greedy = false, TaskScheduler = scheduler });
			Assert.IsFalse (block.Post (42));

			var source =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source.LinkTo (block));

			Assert.IsTrue (source.Post (43));

			scheduler.ExecuteAll ();

			int i;
			Assert.IsTrue (source.TryReceive (null, out i));
			Assert.AreEqual (43, i);

			Assert.IsTrue (source.Post (44));
			Assert.IsTrue (source.Post (45));

			var source2 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source2.LinkTo (block));

			Assert.IsTrue (source2.Post (142));

			scheduler.ExecuteAll ();

			int[] batch;
			Assert.IsFalse (block.TryReceive (null, out batch));
			Assert.IsNull (batch);

			block.TriggerBatch ();
			scheduler.ExecuteAll ();
			Assert.IsTrue (block.TryReceive (null, out batch));
			CollectionAssert.AreEquivalent (new[] { 44, 142 }, batch);
		}
Ejemplo n.º 54
0
		public void NonGreedyBatchWithBoundedCapacityTriggerTest2 ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (3,
				new GroupingDataflowBlockOptions
				{ Greedy = false, BoundedCapacity = 3, TaskScheduler = scheduler });
			var source1 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			var source2 =
				new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
			Assert.IsNotNull (source1.LinkTo (block));
			Assert.IsNotNull (source2.LinkTo (block));

			// trigger 2, then trigger another 2 and then trigger 2 once more
			// while havaing capacity of 3

			Assert.IsTrue (source1.Post (11));
			Assert.IsTrue (source2.Post (21));
			block.TriggerBatch ();

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.Post (12));
			Assert.IsTrue (source2.Post (22));
			block.TriggerBatch ();

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.Post (13));
			Assert.IsTrue (source2.Post (23));
			block.TriggerBatch ();

			scheduler.ExecuteAll ();

			int i;
			Assert.IsTrue (source1.TryReceive (out i));
			Assert.AreEqual (13, i);
			Assert.IsTrue (source2.TryReceive (out i));
			Assert.AreEqual (23, i);

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch);

			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 12, 22 }, batch);

			Assert.IsFalse (block.TryReceive (out batch));
		}
Ejemplo n.º 55
0
		public void QueuedMessagesTest ()
		{
			var scheduler = new TestScheduler ();
			var broadcast = new BroadcastBlock<int> (null,
				new DataflowBlockOptions { TaskScheduler = scheduler });
			var target = new BufferBlock<int> ();
			broadcast.LinkTo (target);

			Assert.IsTrue (broadcast.Post (1));
			Assert.IsTrue (broadcast.Post (2));

			AssertEx.Throws<TimeoutException> (
				() => target.Receive (TimeSpan.FromMilliseconds (100)));

			scheduler.ExecuteAll ();

			int item;
			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (1, item);
			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (2, item);
		}
Ejemplo n.º 56
0
		public void MaxNumberOfGroupsTest ()
		{
			var scheduler = new TestScheduler ();

			var block = new BatchBlock<int> (2,
				new GroupingDataflowBlockOptions
				{ MaxNumberOfGroups = 2, TaskScheduler = scheduler });

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

			Assert.IsTrue (block.Post (3));
			Assert.IsTrue (block.Post (4));

			Assert.IsFalse (block.Post (5));

			scheduler.ExecuteAll ();

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEqual (new[] { 1, 2 }, batch);

			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEqual (new[] { 3, 4 }, batch);

			scheduler.ExecuteAll ();

			Assert.IsTrue (block.Completion.Wait (1000));
		}
Ejemplo n.º 57
0
		public void TryReceiveTest()
		{
			var scheduler = new TestScheduler();
			var block = new BroadcastBlock<int>(i => i * 10, new DataflowBlockOptions { TaskScheduler = scheduler });

			int item;
			Assert.IsFalse(block.TryReceive(null, out item));

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

			scheduler.ExecuteAll();

			Assert.IsTrue(block.TryReceive(null, out item));
			Assert.AreEqual(20, item);
			// predicate is tested on original value, but returned is cloned
			Assert.IsTrue(block.TryReceive(i => i < 10, out item));
			Assert.AreEqual(20, item);
		}
Ejemplo n.º 58
0
		public void NonGreedyMaxNumberOfGroupsTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (2,
				new GroupingDataflowBlockOptions
				{ MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler });
			ITargetBlock<int> target = block;
			var source1 = new TestSourceBlock<int> ();
			var source2 = new TestSourceBlock<int> ();

			var header1 = new DataflowMessageHeader (1);
			source1.AddMessage (header1, 11);
			source2.AddMessage (header1, 21);

			Assert.AreEqual (DataflowMessageStatus.Postponed,
				target.OfferMessage (header1, 11, source1, false));
			Assert.AreEqual (DataflowMessageStatus.Postponed,
				target.OfferMessage (header1, 21, source2, false));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.WasConsumed (header1));
			Assert.IsTrue (source2.WasConsumed (header1));

			var header2 = new DataflowMessageHeader (2);
			Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
				target.OfferMessage (header2, 21, source1, false));

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch);

			Assert.IsTrue (block.Completion.Wait (1000));
		}
Ejemplo n.º 59
0
		public void DontOfferTwiceTest()
		{
			var scheduler = new TestScheduler ();
			var block = new BroadcastBlock<int> (null,
				new DataflowBlockOptions { TaskScheduler = scheduler });
			var target =
				new TestTargetBlock<int> { Postpone = true };
			block.LinkTo (target);

			Assert.IsFalse (target.HasPostponed);

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

			scheduler.ExecuteAll();

			Assert.IsTrue (target.HasPostponed);

			target.Postpone = false;

			int value;
			Assert.IsTrue(target.RetryPostponed(out value));
			Assert.AreEqual(1, value);

			block.LinkTo(new BufferBlock<int>());

			scheduler.ExecuteAll();

			Assert.AreEqual(default(int), target.DirectlyAccepted);
		}
Ejemplo n.º 60
0
		public void LinkToPredicateClonerTest ()
		{
			var scheduler = new TestScheduler ();
			var source = new BroadcastBlock<int> (i => i * 10,
				new DataflowBlockOptions { TaskScheduler = scheduler });
			var target = new BufferBlock<int> ();
			source.LinkTo (target, i => i < 10);

			Assert.IsTrue (source.Post (1));

			scheduler.ExecuteAll ();

			int item;
			Assert.IsTrue (target.TryReceive (out item));
			Assert.AreEqual (10, item);
		}