Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        public void OfferMessageWithSourceTest()
        {
            var block =
                new BufferBlock <int> (new DataflowBlockOptions {
                BoundedCapacity = 1
            });
            ITargetBlock <int> target = block;
            var source = new TestSourceBlock <int> ();

            Assert.AreEqual(DataflowMessageStatus.Accepted,
                            target.OfferMessage(new DataflowMessageHeader(1), 42, source, false));
            var header = new DataflowMessageHeader(2);

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

            Assert.AreEqual(42, block.Receive(TimeSpan.FromMilliseconds(100)));

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

            Assert.IsTrue(source.WasConsumed(header));

            Assert.AreEqual(43, block.Receive(TimeSpan.FromMilliseconds(100)));

            Assert.AreEqual(DataflowMessageStatus.Accepted,
                            target.OfferMessage(new DataflowMessageHeader(3), 44, source, false));

            Assert.AreEqual(44, block.Receive());
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public void NullTargetTest()
        {
            var target = DataflowBlock.NullTarget <int> ();

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

            var source = new TestSourceBlock <int> ();
            var header = new DataflowMessageHeader(1);

            source.AddMessage(header, 2);

            Assert.IsFalse(source.WasConsumed(header));

            Assert.AreEqual(DataflowMessageStatus.Accepted,
                            target.OfferMessage(header, 2, source, true));
            Assert.IsTrue(source.WasConsumed(header));

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

            target.Complete();

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

            target.Fault(new Exception());

            Assert.IsFalse(target.Completion.Wait(100));
        }
Exemplo n.º 5
0
        public void ConsumeToAcceptTest()
        {
            var source = new TestSourceBlock <int> ();
            var buffer = new BufferBlock <int> ();
            var target = (ITargetBlock <int>)buffer;

            var header = new DataflowMessageHeader(1);

            source.AddMessage(header, 1);

            Assert.AreEqual(DataflowMessageStatus.Accepted,
                            target.OfferMessage(header, 1, source, true));

            Assert.IsTrue(source.WasConsumed(header));
            Assert.IsFalse(source.WasReserved(header));
        }
Exemplo n.º 6
0
        public void MaxNumberOfGroupsWithConsumeToAcceptTest()
        {
            ITargetBlock <int> block = new BatchBlock <int> (1,
                                                             new GroupingDataflowBlockOptions {
                MaxNumberOfGroups = 1
            });

            var evt = new ManualResetEventSlim();

            Func <Task <Tuple <DataflowMessageStatus, bool> > > startTask =
                () => Task.Factory.StartNew(
                    () =>
            {
                var sourceBlock = new TestSourceBlock <int> {
                    ConsumeWaiter = evt.Wait
                };
                var header = new DataflowMessageHeader(1);
                sourceBlock.AddMessage(header, 1);
                var status = block.OfferMessage(header, 1, sourceBlock, true);

                return(Tuple.Create(status, sourceBlock.WasConsumed(header)));
            });

            var task1 = startTask();
            var task2 = startTask();

            Thread.Sleep(100);

            Assert.IsFalse(task1.IsCompleted);
            Assert.IsFalse(task2.IsCompleted);

            evt.Set();

            Assert.IsTrue(Task.WaitAll(new Task[] { task1, task2 }, 100));

            CollectionAssert.AreEquivalent(
                new[]
            {
                Tuple.Create(DataflowMessageStatus.Accepted, true),
                Tuple.Create(DataflowMessageStatus.DecliningPermanently, false)
            },
                new[] { task1.Result, task2.Result });
        }
Exemplo n.º 7
0
        public void NonGreedyMaxNumberOfGroupsTest()
        {
            var scheduler = new TestScheduler();
            var block     = new JoinBlock <int, int, int> (
                new GroupingDataflowBlockOptions
            {
                MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler
            });
            var source1 = new TestSourceBlock <int> ();
            var source2 = new TestSourceBlock <int> ();
            var source3 = new TestSourceBlock <int> ();

            var header1 = new DataflowMessageHeader(1);

            source1.AddMessage(header1, 11);
            source2.AddMessage(header1, 21);
            source3.AddMessage(header1, 31);

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

            scheduler.ExecuteAll();

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

            var header2 = new DataflowMessageHeader(2);

            Assert.AreEqual(DataflowMessageStatus.DecliningPermanently,
                            block.Target1.OfferMessage(header2, 21, source1, false));

            Tuple <int, int, int> tuple;

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

            Assert.IsTrue(block.Completion.Wait(100));
        }
Exemplo n.º 8
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(100));
        }
Exemplo n.º 9
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));
        }