예제 #1
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);
        }
        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);
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
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));
        }
예제 #6
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);
        }
예제 #7
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);
        }
예제 #8
0
        public void FaultExecutingConsume()
        {
            var evt    = new ManualResetEventSlim();
            var source = new TransformBlock <int, int> (i =>
            {
                if (i == 2)
                {
                    evt.Wait();
                }
                return(i);
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

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

            Assert.IsTrue(source.Post(1), "#1");
            Assert.IsTrue(source.Post(2), "#2");
            Assert.IsTrue(source.Post(3), "#3");
            target.PostponedEvent.Wait(1000);
            Assert.IsTrue(target.HasPostponed, "#4");

            var exception = new Exception();

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

            source.Completion.Wait(1000);

            Assert.IsFalse(source.Completion.IsFaulted, "#5");
            int value;

            Assert.IsTrue(target.RetryPostponed(out value), "#6");
            Assert.AreEqual(1, value, "#7");

            evt.Set();

            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, "#10");
            Assert.IsFalse(target.RetryPostponed(out value), "#11");
        }
예제 #9
0
        public void FaultExecutingConsume()
        {
            var evt    = new ManualResetEventSlim();
            var source = new TransformBlock <int, int> (i =>
            {
                if (i == 2)
                {
                    evt.Wait();
                }
                return(i);
            });
            var target = new TestTargetBlock <int> {
                Postpone = true
            };

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

            Assert.IsTrue(source.Post(1));
            Assert.IsTrue(source.Post(2));
            Assert.IsTrue(source.Post(3));
            Thread.Sleep(500);
            Assert.IsTrue(target.HasPostponed);

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

            Thread.Sleep(100);

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

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

            evt.Set();

            Thread.Sleep(100);

            Assert.IsTrue(source.Completion.IsFaulted);
            Assert.IsFalse(target.RetryPostponed(out value));
        }
예제 #10
0
        public void DecliningTest()
        {
            var scheduler = new TestScheduler();
            var source    =
                new BufferBlock <int> (new DataflowBlockOptions {
                TaskScheduler = scheduler
            });
            var target1 = new TestTargetBlock <int> {
                Decline = true
            };
            var target2 = new TestTargetBlock <int> ();

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

            Assert.IsTrue(source.Post(42));
            scheduler.ExecuteAll();
            Assert.AreEqual(default(int), target1.DirectlyAccepted);
            Assert.AreEqual(42, target2.DirectlyAccepted);
        }
예제 #11
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);
        }