ReceivingTest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // ReceivingTest.cs
  2. //
  3. // Copyright (c) 2012 Petr Onderka
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Threading;
  25. using System.Threading.Tasks;
  26. using System.Threading.Tasks.Dataflow;
  27. using NUnit.Framework;
  28. namespace MonoTests.System.Threading.Tasks.Dataflow {
  29. [TestFixture]
  30. public class ReceivingTest {
  31. [Test]
  32. public void PostponeTest ()
  33. {
  34. var scheduler = new TestScheduler ();
  35. var source =
  36. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  37. var target = new TestTargetBlock<int> { Postpone = true };
  38. Assert.IsNotNull (source.LinkTo (target));
  39. Assert.IsFalse (target.HasPostponed);
  40. Assert.IsTrue (source.Post (42));
  41. scheduler.ExecuteAll ();
  42. Assert.IsTrue (target.HasPostponed);
  43. int i;
  44. Assert.IsTrue (target.RetryPostponed (out i));
  45. Assert.AreEqual (42, i);
  46. }
  47. [Test]
  48. public void PostponeTwoTargetsTest ()
  49. {
  50. var scheduler = new TestScheduler ();
  51. var source =
  52. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  53. var target1 = new TestTargetBlock<int> { Postpone = true };
  54. var target2 = new TestTargetBlock<int> { Postpone = true };
  55. Assert.IsNotNull (source.LinkTo (target1));
  56. Assert.IsNotNull (source.LinkTo (target2));
  57. Assert.IsFalse (target1.HasPostponed);
  58. Assert.IsFalse (target2.HasPostponed);
  59. Assert.IsTrue (source.Post (42));
  60. scheduler.ExecuteAll ();
  61. Assert.IsTrue (target1.HasPostponed);
  62. Assert.IsTrue (target2.HasPostponed);
  63. int i;
  64. Assert.IsTrue (target2.RetryPostponed (out i));
  65. Assert.AreEqual (42, i);
  66. Assert.IsFalse (target1.RetryPostponed (out i));
  67. Assert.AreEqual (default(int), i);
  68. }
  69. [Test]
  70. public void DecliningTest ()
  71. {
  72. var scheduler = new TestScheduler ();
  73. var source =
  74. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  75. var target1 = new TestTargetBlock<int> { Decline = true };
  76. var target2 = new TestTargetBlock<int> ();
  77. Assert.IsNotNull (source.LinkTo (target1));
  78. Assert.IsNotNull (source.LinkTo (target2));
  79. Assert.AreEqual (default(int), target1.DirectlyAccepted);
  80. Assert.AreEqual (default(int), target2.DirectlyAccepted);
  81. Assert.IsTrue (source.Post (42));
  82. scheduler.ExecuteAll ();
  83. Assert.AreEqual (default(int), target1.DirectlyAccepted);
  84. Assert.AreEqual (42, target2.DirectlyAccepted);
  85. }
  86. [Test]
  87. public void ConditionalDecliningTest ()
  88. {
  89. var scheduler = new TestScheduler ();
  90. var source =
  91. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  92. var target = new TestTargetBlock<int> { Decline = true };
  93. Assert.IsNotNull (source.LinkTo (target));
  94. Assert.AreEqual (default(int), target.DirectlyAccepted);
  95. Assert.IsTrue (source.Post (42));
  96. scheduler.ExecuteAll ();
  97. Assert.AreEqual (default(int), target.DirectlyAccepted);
  98. target.Decline = false;
  99. Assert.IsTrue (source.Post (43));
  100. scheduler.ExecuteAll ();
  101. Assert.AreEqual (default(int), target.DirectlyAccepted);
  102. Assert.AreEqual (42, source.Receive (TimeSpan.FromMilliseconds (100)));
  103. scheduler.ExecuteAll ();
  104. Assert.AreEqual (43, target.DirectlyAccepted);
  105. }
  106. [Test]
  107. public void TryReceiveWithPredicateTest ()
  108. {
  109. var source = new BufferBlock<int> ();
  110. Assert.IsTrue (source.Post (42));
  111. Assert.IsTrue (source.Post (43));
  112. int item;
  113. Assert.IsFalse (source.TryReceive (i => i == 43, out item));
  114. Assert.AreEqual (default(int), item);
  115. Assert.AreEqual (42, source.Receive ());
  116. Assert.IsTrue (source.TryReceive (i => i == 43, out item));
  117. Assert.AreEqual (43, item);
  118. }
  119. [Test]
  120. public void ReserveTest ()
  121. {
  122. var scheduler = new TestScheduler ();
  123. var source =
  124. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  125. var target = new TestTargetBlock<int> { Postpone = true };
  126. Assert.IsNotNull (source.LinkTo (target));
  127. Assert.IsFalse (target.HasPostponed);
  128. Assert.IsTrue (source.Post (42));
  129. Assert.IsTrue (source.Post (43));
  130. scheduler.ExecuteAll ();
  131. Assert.IsTrue (target.HasPostponed);
  132. Assert.IsTrue (target.ReservePostponed ());
  133. int i;
  134. Assert.IsFalse (source.TryReceive (null, out i));
  135. Assert.AreEqual (default(int), i);
  136. IList<int> items;
  137. Assert.IsFalse (source.TryReceiveAll (out items));
  138. Assert.AreEqual (default(IList<int>), items);
  139. Assert.IsTrue (target.RetryPostponed (out i));
  140. Assert.AreEqual (42, i);
  141. Assert.IsTrue (source.TryReceive (null, out i));
  142. Assert.AreEqual (43, i);
  143. }
  144. [Test]
  145. public void ConsumeAfterReceiveTest ()
  146. {
  147. var scheduler = new TestScheduler ();
  148. var source =
  149. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  150. var target = new TestTargetBlock<int> { Postpone = true };
  151. Assert.IsNotNull (source.LinkTo (target));
  152. Assert.IsFalse (target.HasPostponed);
  153. Assert.IsTrue (source.Post (42));
  154. scheduler.ExecuteAll ();
  155. Assert.IsTrue (target.HasPostponed);
  156. target.Postpone = false;
  157. Assert.AreEqual (42, source.Receive ());
  158. Assert.IsTrue (source.Post (43));
  159. Assert.AreEqual (default(int), target.DirectlyAccepted);
  160. int i;
  161. Assert.IsFalse (target.RetryPostponed (out i));
  162. Assert.AreEqual (default(int), i);
  163. scheduler.ExecuteAll ();
  164. Assert.AreEqual (43, target.DirectlyAccepted);
  165. }
  166. [Test]
  167. public void FaultConsume()
  168. {
  169. var scheduler = new TestScheduler ();
  170. var source =
  171. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  172. var target = new TestTargetBlock<int> { Postpone = true };
  173. Assert.IsNotNull (source.LinkTo (target));
  174. Assert.IsTrue (source.Post (42));
  175. scheduler.ExecuteAll ();
  176. Assert.IsTrue (target.HasPostponed);
  177. ((IDataflowBlock)source).Fault (new Exception ());
  178. scheduler.ExecuteAll ();
  179. Thread.Sleep (100);
  180. int value;
  181. Assert.IsFalse (target.RetryPostponed (out value));
  182. }
  183. [Test]
  184. public void ReserveFaultConsume()
  185. {
  186. var scheduler = new TestScheduler ();
  187. var source =
  188. new BufferBlock<int> (new DataflowBlockOptions { TaskScheduler = scheduler });
  189. var target = new TestTargetBlock<int> { Postpone = true };
  190. Assert.IsNotNull (source.LinkTo (target));
  191. Assert.IsTrue (source.Post (42));
  192. scheduler.ExecuteAll ();
  193. Assert.IsTrue (target.HasPostponed);
  194. Assert.IsTrue (target.ReservePostponed ());
  195. ((IDataflowBlock)source).Fault (new Exception ());
  196. scheduler.ExecuteAll ();
  197. Thread.Sleep (100);
  198. int value;
  199. Assert.IsTrue (target.RetryPostponed (out value));
  200. }
  201. }
  202. class TestTargetBlock<T> : ITargetBlock<T> {
  203. public bool Postpone { get; set; }
  204. public bool Decline { get; set; }
  205. Tuple<ISourceBlock<T>, DataflowMessageHeader> postponed;
  206. public T DirectlyAccepted { get; private set; }
  207. public DataflowMessageStatus OfferMessage (
  208. DataflowMessageHeader messageHeader, T messageValue, ISourceBlock<T> source,
  209. bool consumeToAccept)
  210. {
  211. if (Decline)
  212. return DataflowMessageStatus.Declined;
  213. if (Postpone) {
  214. postponed = Tuple.Create (source, messageHeader);
  215. return DataflowMessageStatus.Postponed;
  216. }
  217. DirectlyAccepted = messageValue;
  218. return DataflowMessageStatus.Accepted;
  219. }
  220. public bool HasPostponed
  221. {
  222. get { return postponed != null; }
  223. }
  224. public bool RetryPostponed (out T value)
  225. {
  226. bool consumed;
  227. value = postponed.Item1.ConsumeMessage (
  228. postponed.Item2, this, out consumed);
  229. postponed = null;
  230. return consumed;
  231. }
  232. public bool ReservePostponed ()
  233. {
  234. return postponed.Item1.ReserveMessage (postponed.Item2, this);
  235. }
  236. public void Complete ()
  237. {
  238. }
  239. public void Fault (Exception exception)
  240. {
  241. }
  242. public Task Completion { get; private set; }
  243. }
  244. }