ActionBlockTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // ActionBlockTest.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.Linq;
  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. [TestFixture]
  35. public class ActionBlockTest {
  36. [Test]
  37. public void BasicUsageTest ()
  38. {
  39. bool[] array = new bool[3];
  40. var evt = new CountdownEvent (array.Length);
  41. var block = new ActionBlock<int> (i => { array[i] = true; evt.Signal (); });
  42. for (int i = 0; i < array.Length; ++i)
  43. Assert.IsTrue (block.Post (i), "Not accepted");
  44. Assert.IsTrue (evt.Wait (1000));
  45. Assert.IsTrue (array.All (b => b), "Some false");
  46. }
  47. [Test]
  48. public void CompleteTest ()
  49. {
  50. var block = new ActionBlock<int> (i => Thread.Sleep (100));
  51. for (int i = 0; i < 10; i++)
  52. Assert.IsTrue (block.Post (i), "Not Accepted");
  53. block.Complete ();
  54. // Still element to be processed so Completion should be false
  55. Assert.IsFalse (block.Completion.IsCompleted);
  56. block.Completion.Wait ();
  57. Assert.IsTrue (block.Completion.IsCompleted);
  58. }
  59. [Test]
  60. public void AsyncNullTest()
  61. {
  62. var scheduler = new TestScheduler ();
  63. var block = new ActionBlock<int> (
  64. i => null,
  65. new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
  66. Assert.IsTrue (block.Post (1));
  67. scheduler.ExecuteAll ();
  68. Assert.IsFalse (block.Completion.Wait (100));
  69. block.Complete ();
  70. Assert.IsTrue (block.Completion.Wait (1000));
  71. }
  72. [Test]
  73. public void AsyncCancelledTest()
  74. {
  75. var scheduler = new TestScheduler ();
  76. var block = new ActionBlock<int> (
  77. i =>
  78. {
  79. var tcs = new TaskCompletionSource<int> ();
  80. tcs.SetCanceled ();
  81. return tcs.Task;
  82. }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
  83. Assert.IsTrue (block.Post (1));
  84. scheduler.ExecuteAll ();
  85. Assert.IsFalse (block.Completion.Wait (100));
  86. }
  87. }
  88. }