BatchedJoinBlock`3Test.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // BatchedJoinBlockTest.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.Dataflow;
  26. using NUnit.Framework;
  27. namespace MonoTests.System.Threading.Tasks.Dataflow {
  28. [TestFixture]
  29. public class BatchedJoinBlock3Test {
  30. [Test]
  31. public void BasicUsageTest ()
  32. {
  33. Tuple<IList<int>, IList<int>, IList<string>> result = null;
  34. var evt = new ManualResetEventSlim (false);
  35. var actionBlock =
  36. new ActionBlock<Tuple<IList<int>, IList<int>, IList<string>>> (r =>
  37. {
  38. result = r;
  39. evt.Set ();
  40. });
  41. var block = new BatchedJoinBlock<int, int, string> (3);
  42. block.LinkTo (actionBlock);
  43. // all targets once
  44. Assert.IsTrue (block.Target1.Post (1));
  45. Assert.IsTrue (block.Target2.Post (2));
  46. Assert.IsFalse (evt.Wait (100));
  47. Assert.IsNull (result);
  48. Assert.IsTrue (block.Target3.Post ("foo"));
  49. Assert.IsTrue (evt.Wait (100));
  50. Assert.IsNotNull (result);
  51. CollectionAssert.AreEqual (new[] { 1 }, result.Item1);
  52. CollectionAssert.AreEqual (new[] { 2 }, result.Item2);
  53. CollectionAssert.AreEqual (new[] { "foo" }, result.Item3);
  54. }
  55. [Test]
  56. public void BoundedCapacityTest ()
  57. {
  58. AssertEx.Throws<ArgumentException> (
  59. () =>
  60. new BatchedJoinBlock<int, int> (2,
  61. new GroupingDataflowBlockOptions { BoundedCapacity = 3 }));
  62. }
  63. [Test]
  64. public void CompletionTest ()
  65. {
  66. var block = new BatchedJoinBlock<int, int, int> (2);
  67. Assert.IsTrue (block.Target1.Post (1));
  68. block.Complete ();
  69. Tuple<IList<int>, IList<int>, IList<int>> batch;
  70. Assert.IsTrue (block.TryReceive (out batch), batch.ToString ());
  71. CollectionAssert.AreEqual (new[] { 1 }, batch.Item1);
  72. CollectionAssert.IsEmpty (batch.Item2);
  73. CollectionAssert.IsEmpty (batch.Item3);
  74. Assert.IsTrue (block.Completion.Wait (100));
  75. }
  76. [Test]
  77. public void MaxNumberOfGroupsTest ()
  78. {
  79. var scheduler = new TestScheduler ();
  80. var block = new BatchedJoinBlock<int, int, int> (1,
  81. new GroupingDataflowBlockOptions
  82. { MaxNumberOfGroups = 3, TaskScheduler = scheduler });
  83. Assert.IsTrue (block.Target1.Post (1));
  84. Assert.IsTrue (block.Target2.Post (2));
  85. Assert.IsTrue (block.Target3.Post (3));
  86. Assert.IsFalse (block.Target3.Post (4));
  87. Assert.IsFalse (block.Target2.Post (5));
  88. Assert.IsFalse (block.Target1.Post (6));
  89. Tuple<IList<int>, IList<int>, IList<int>> batch;
  90. Assert.IsTrue (block.TryReceive (out batch));
  91. CollectionAssert.AreEqual (new[] { 1 }, batch.Item1);
  92. CollectionAssert.IsEmpty (batch.Item2);
  93. CollectionAssert.IsEmpty (batch.Item3);
  94. Assert.IsTrue (block.TryReceive (out batch));
  95. CollectionAssert.IsEmpty (batch.Item1);
  96. CollectionAssert.AreEqual (new[] { 2 }, batch.Item2);
  97. CollectionAssert.IsEmpty (batch.Item3);
  98. Assert.IsTrue (block.TryReceive (out batch));
  99. CollectionAssert.IsEmpty (batch.Item1);
  100. CollectionAssert.IsEmpty (batch.Item2);
  101. CollectionAssert.AreEqual (new[] { 3 }, batch.Item3);
  102. scheduler.ExecuteAll ();
  103. Assert.IsTrue (block.Completion.Wait (100));
  104. }
  105. }
  106. }