| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- // ReceivingTest.cs
- //
- // Copyright (c) 2012 Petr Onderka
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Threading.Tasks.Dataflow;
- using NUnit.Framework;
- namespace MonoTests.System.Threading.Tasks.Dataflow {
- [TestFixture]
- public class ReceivingTest {
- [Test]
- public void PostponeTest ()
- {
- 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);
- int i;
- Assert.IsTrue (target.RetryPostponed (out i));
- Assert.AreEqual (42, i);
- }
- [Test]
- 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);
- }
- [Test]
- 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);
- }
- [Test]
- 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);
- }
- [Test]
- public void TryReceiveWithPredicateTest ()
- {
- var source = new BufferBlock<int> ();
- Assert.IsTrue (source.Post (42));
- Assert.IsTrue (source.Post (43));
- int item;
- Assert.IsFalse (source.TryReceive (i => i == 43, out item));
- Assert.AreEqual (default(int), item);
- Assert.AreEqual (42, source.Receive ());
- Assert.IsTrue (source.TryReceive (i => i == 43, out item));
- Assert.AreEqual (43, item);
- }
- [Test]
- 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);
- }
- [Test]
- 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);
- }
- [Test]
- public void FaultConsume()
- {
- 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.IsTrue (source.Post (42));
- scheduler.ExecuteAll ();
- Assert.IsTrue (target.HasPostponed);
- ((IDataflowBlock)source).Fault (new Exception ());
- scheduler.ExecuteAll ();
- Thread.Sleep (100);
- int value;
- Assert.IsFalse (target.RetryPostponed (out value));
- }
- [Test]
- public void ReserveFaultConsume()
- {
- 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.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));
- }
- }
- class TestTargetBlock<T> : ITargetBlock<T> {
- public bool Postpone { get; set; }
- public bool Decline { get; set; }
- Tuple<ISourceBlock<T>, DataflowMessageHeader> postponed;
- public T DirectlyAccepted { get; private set; }
- public DataflowMessageStatus OfferMessage (
- DataflowMessageHeader messageHeader, T messageValue, ISourceBlock<T> source,
- bool consumeToAccept)
- {
- if (Decline)
- return DataflowMessageStatus.Declined;
- if (Postpone) {
- postponed = Tuple.Create (source, messageHeader);
- return DataflowMessageStatus.Postponed;
- }
- DirectlyAccepted = messageValue;
- return DataflowMessageStatus.Accepted;
- }
- public bool HasPostponed
- {
- get { return postponed != null; }
- }
- public bool RetryPostponed (out T value)
- {
- bool consumed;
- value = postponed.Item1.ConsumeMessage (
- postponed.Item2, this, out consumed);
- postponed = null;
- return consumed;
- }
- public bool ReservePostponed ()
- {
- return postponed.Item1.ReserveMessage (postponed.Item2, this);
- }
- public void Complete ()
- {
- }
- public void Fault (Exception exception)
- {
- }
- public Task Completion { get; private set; }
- }
- }
|