DataflowBlockTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // DataflowBlockTest.cs
  3. //
  4. // Author:
  5. // Jérémie "garuma" Laval <[email protected]>
  6. // Petr Onderka <[email protected]>
  7. //
  8. // Copyright (c) 2011 Jérémie "garuma" Laval
  9. // Copyright (c) 2012 Petr Onderka
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. using System;
  29. using System.Threading;
  30. using System.Threading.Tasks;
  31. using System.Threading.Tasks.Dataflow;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Threading.Tasks.Dataflow
  34. {
  35. [TestFixture]
  36. public class DataflowBlockTest
  37. {
  38. [Test]
  39. public void ChooseTest ()
  40. {
  41. var source1 = new BufferBlock<int> ();
  42. var source2 = new BufferBlock<long> ();
  43. bool action1 = false;
  44. bool action2 = false;
  45. var completion = DataflowBlock.Choose (source1, (_) => action1 = true, source2, (_) => action2 = true);
  46. source1.Post (42);
  47. Thread.Sleep (1600);
  48. Assert.IsTrue (action1);
  49. Assert.IsFalse (action2);
  50. Assert.IsTrue (completion.IsCompleted);
  51. Assert.AreEqual (TaskStatus.RanToCompletion, completion.Status);
  52. Assert.AreEqual (0, completion.Result);
  53. }
  54. [Test]
  55. public void ChooseTest_3 ()
  56. {
  57. var source1 = new BufferBlock<int> ();
  58. var source2 = new BufferBlock<long> ();
  59. var source3 = new BufferBlock<object> ();
  60. bool action1 = false;
  61. bool action2 = false;
  62. bool action3 = false;
  63. var completion = DataflowBlock.Choose (source1, (_) => action1 = true, source2, (_) => action2 = true, source3, (_) => action3 = true);
  64. source3.Post (new object ());
  65. Thread.Sleep (1600);
  66. Assert.IsFalse (action1);
  67. Assert.IsFalse (action2);
  68. Assert.IsTrue (action3);
  69. Assert.IsTrue (completion.IsCompleted);
  70. Assert.AreEqual (TaskStatus.RanToCompletion, completion.Status);
  71. Assert.AreEqual (2, completion.Result);
  72. }
  73. [Test]
  74. public void TryReceiveTest ()
  75. {
  76. var block = new BufferBlock<int> ();
  77. int value = -1;
  78. block.Post (42);
  79. Thread.Sleep (500);
  80. Assert.IsTrue (block.TryReceive (out value));
  81. Assert.AreEqual (42, value);
  82. }
  83. [Test]
  84. public void ReceiveTest ()
  85. {
  86. var block = new BufferBlock<int> ();
  87. Task.Factory.StartNew (() => { Thread.Sleep (300); block.Post (42); });
  88. Assert.AreEqual (42, block.Receive ());
  89. }
  90. [Test]
  91. public void ReceiveCompletedTest ()
  92. {
  93. var block = new BufferBlock<int> ();
  94. block.Complete ();
  95. AssertEx.Throws<InvalidOperationException> (
  96. () => block.Receive (TimeSpan.FromMilliseconds (100)));
  97. }
  98. [Test]
  99. public void ReceiveTimeoutTest ()
  100. {
  101. var block = new BufferBlock<int> ();
  102. AssertEx.Throws<TimeoutException> (
  103. () => block.Receive (TimeSpan.FromMilliseconds (100)));
  104. }
  105. [Test]
  106. public void ReceiveCancelledTest ()
  107. {
  108. var block = new BufferBlock<int> ();
  109. var tokenSource = new CancellationTokenSource (200);
  110. AssertEx.Throws<OperationCanceledException> (
  111. () => block.Receive (tokenSource.Token));
  112. }
  113. [Test]
  114. public void AsyncReceiveTest ()
  115. {
  116. int result = -1;
  117. var mre = new ManualResetEventSlim (false);
  118. var block = new WriteOnceBlock<int> (null);
  119. block.ReceiveAsync ().ContinueWith (i =>
  120. {
  121. result = i.Result;
  122. mre.Set ();
  123. });
  124. Task.Factory.StartNew (() =>
  125. {
  126. Thread.Sleep (100);
  127. block.Post (42);
  128. });
  129. Assert.IsTrue (mre.Wait (300));
  130. Assert.AreEqual (42, result);
  131. }
  132. [Test]
  133. public void AsyncReceiveTestCanceled ()
  134. {
  135. var src = new CancellationTokenSource ();
  136. var block = new WriteOnceBlock<int> (null);
  137. var task = block.ReceiveAsync (src.Token);
  138. Task.Factory.StartNew (() =>
  139. {
  140. Thread.Sleep (800);
  141. block.Post (42);
  142. });
  143. Thread.Sleep (50);
  144. src.Cancel ();
  145. AggregateException ex = null;
  146. try {
  147. task.Wait ();
  148. } catch (AggregateException e) {
  149. ex = e;
  150. }
  151. Assert.IsNotNull (ex);
  152. Assert.IsNotNull (ex.InnerException);
  153. Assert.IsInstanceOfType (typeof(OperationCanceledException),
  154. ex.InnerException);
  155. Assert.IsTrue (task.IsCompleted);
  156. Assert.AreEqual (TaskStatus.Canceled, task.Status);
  157. }
  158. [Test]
  159. public void SendAsyncAcceptedTest ()
  160. {
  161. var target = new BufferBlock<int> ();
  162. var task = target.SendAsync (1);
  163. Assert.IsTrue (task.Wait (0));
  164. Assert.IsTrue (task.Result);
  165. }
  166. [Test]
  167. public void SendAsyncDeclinedTest ()
  168. {
  169. var target = new BufferBlock<int> ();
  170. target.Complete ();
  171. var task = target.SendAsync (1);
  172. Assert.IsTrue (task.Wait (0));
  173. Assert.IsFalse (task.Result);
  174. }
  175. [Test]
  176. public void SendAsyncPostponedAcceptedTest ()
  177. {
  178. var target =
  179. new BufferBlock<int> (new DataflowBlockOptions { BoundedCapacity = 1 });
  180. Assert.IsTrue (target.Post (1));
  181. var task = target.SendAsync (1);
  182. Assert.IsFalse (task.Wait (100));
  183. Assert.AreEqual (1, target.Receive ());
  184. Assert.IsTrue (task.Wait (100));
  185. Assert.IsTrue (task.Result);
  186. }
  187. [Test]
  188. public void SendAsyncPostponedDeclinedTest ()
  189. {
  190. var target =
  191. new BufferBlock<int> (new DataflowBlockOptions { BoundedCapacity = 1 });
  192. Assert.IsTrue (target.Post (1));
  193. var task = target.SendAsync (1);
  194. Assert.IsFalse (task.Wait (100));
  195. target.Complete ();
  196. Assert.IsTrue (task.Wait (100));
  197. Assert.IsFalse (task.Result);
  198. }
  199. }
  200. }