JoinBlock`3Test.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // JoinBlockTest.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.Dataflow;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Threading.Tasks.Dataflow {
  33. [TestFixture]
  34. public class JoinBlock3Test {
  35. [Test]
  36. public void BasicUsageTest ()
  37. {
  38. Tuple<int, int, int> tuple = null;
  39. var evt = new ManualResetEventSlim (false);
  40. var ablock = new ActionBlock<Tuple<int, int, int>> (t =>
  41. {
  42. tuple = t;
  43. evt.Set ();
  44. });
  45. var block = new JoinBlock<int, int, int> ();
  46. block.LinkTo (ablock);
  47. block.Target1.Post (42);
  48. evt.Wait (500);
  49. Assert.IsNull (tuple);
  50. block.Target2.Post (24);
  51. evt.Wait (500);
  52. Assert.IsNull (tuple);
  53. block.Target3.Post (44);
  54. evt.Wait ();
  55. Assert.IsNotNull (tuple);
  56. Assert.AreEqual (42, tuple.Item1);
  57. Assert.AreEqual (24, tuple.Item2);
  58. Assert.AreEqual (44, tuple.Item3);
  59. }
  60. [Test]
  61. public void CompletionTest ()
  62. {
  63. var block = new JoinBlock<int, int, int> ();
  64. Assert.IsTrue (block.Target1.Post (1));
  65. block.Complete ();
  66. Tuple<int, int, int> tuple;
  67. Assert.IsFalse (block.TryReceive (out tuple));
  68. Assert.IsTrue (block.Completion.Wait (100));
  69. }
  70. [Test]
  71. public void MaxNumberOfGroupsTest ()
  72. {
  73. var scheduler = new TestScheduler ();
  74. var block = new JoinBlock<int, int, int> (
  75. new GroupingDataflowBlockOptions
  76. { MaxNumberOfGroups = 1, TaskScheduler = scheduler });
  77. Assert.IsTrue (block.Target1.Post (1));
  78. Assert.IsFalse (block.Target1.Post (2));
  79. Assert.IsTrue (block.Target2.Post (3));
  80. Assert.IsTrue (block.Target3.Post (4));
  81. Assert.IsFalse (block.Target3.Post (4));
  82. Assert.IsFalse (block.Target2.Post (4));
  83. Tuple<int, int, int> batch;
  84. Assert.IsTrue (block.TryReceive (out batch));
  85. Assert.AreEqual (Tuple.Create (1, 3, 4), batch);
  86. Assert.IsFalse (block.TryReceive (out batch));
  87. scheduler.ExecuteAll ();
  88. Assert.IsTrue (block.Completion.Wait (100));
  89. }
  90. [Test]
  91. public void NonGreedyMaxNumberOfGroupsTest ()
  92. {
  93. var scheduler = new TestScheduler ();
  94. var block = new JoinBlock<int, int, int> (
  95. new GroupingDataflowBlockOptions
  96. { MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler });
  97. var source1 = new TestSourceBlock<int> ();
  98. var source2 = new TestSourceBlock<int> ();
  99. var source3 = new TestSourceBlock<int> ();
  100. var header1 = new DataflowMessageHeader (1);
  101. source1.AddMessage (header1, 11);
  102. source2.AddMessage (header1, 21);
  103. source3.AddMessage (header1, 31);
  104. Assert.AreEqual (DataflowMessageStatus.Postponed,
  105. block.Target1.OfferMessage (header1, 11, source1, false));
  106. Assert.AreEqual (DataflowMessageStatus.Postponed,
  107. block.Target2.OfferMessage (header1, 21, source2, false));
  108. Assert.AreEqual (DataflowMessageStatus.Postponed,
  109. block.Target3.OfferMessage (header1, 31, source3, false));
  110. scheduler.ExecuteAll ();
  111. Assert.IsTrue (source1.WasConsumed (header1));
  112. Assert.IsTrue (source2.WasConsumed (header1));
  113. Assert.IsTrue (source3.WasConsumed (header1));
  114. var header2 = new DataflowMessageHeader (2);
  115. Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
  116. block.Target1.OfferMessage (header2, 21, source1, false));
  117. Tuple<int, int, int> tuple;
  118. Assert.IsTrue (block.TryReceive (out tuple));
  119. Assert.AreEqual (Tuple.Create (11, 21, 31), tuple);
  120. Assert.IsTrue (block.Completion.Wait (100));
  121. }
  122. }
  123. }