OutputAvailableTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // OutputAvailableTest.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.Threading;
  24. using System.Threading.Tasks.Dataflow;
  25. using NUnit.Framework;
  26. namespace MonoTests.System.Threading.Tasks.Dataflow {
  27. [TestFixture]
  28. public class OutputAvailableTest {
  29. [Test]
  30. public void ImmediateTest()
  31. {
  32. var scheduler = new TestScheduler();
  33. var source = new BufferBlock<int>(new DataflowBlockOptions{TaskScheduler = scheduler});
  34. Assert.IsTrue(source.Post(1));
  35. var task = source.OutputAvailableAsync();
  36. // not necessary in MS.NET
  37. scheduler.ExecuteAll();
  38. Assert.IsTrue(task.IsCompleted);
  39. Assert.IsTrue(task.Result);
  40. }
  41. [Test]
  42. public void PostponedTest()
  43. {
  44. var source = new BufferBlock<int>(new DataflowBlockOptions());
  45. var task = source.OutputAvailableAsync();
  46. Assert.IsFalse(task.Wait(100));
  47. Assert.IsTrue(source.Post(1));
  48. Assert.IsTrue(task.Wait(1000));
  49. Assert.IsTrue(task.Result);
  50. }
  51. [Test]
  52. public void CompletedImmediateTest()
  53. {
  54. var source = new BufferBlock<int>();
  55. source.Complete();
  56. var task = source.OutputAvailableAsync();
  57. Assert.IsTrue(task.Wait(1000));
  58. Assert.IsFalse(task.Result);
  59. }
  60. [Test]
  61. public void CompletedPostponedTest()
  62. {
  63. var source = new BufferBlock<int>(new DataflowBlockOptions());
  64. var task = source.OutputAvailableAsync();
  65. Assert.IsFalse(task.Wait(100));
  66. source.Complete();
  67. Assert.IsTrue(task.Wait(1000));
  68. Assert.IsFalse(task.Result);
  69. }
  70. [Test]
  71. public void FaultedImmediateTest()
  72. {
  73. var source = new BufferBlock<int>();
  74. ((IDataflowBlock)source).Fault(new Exception());
  75. var task = source.OutputAvailableAsync();
  76. Assert.IsTrue(task.Wait(1000));
  77. Assert.IsFalse(task.Result);
  78. }
  79. [Test]
  80. public void FaultedPostponedTest()
  81. {
  82. var source = new BufferBlock<int>();
  83. var task = source.OutputAvailableAsync();
  84. Assert.IsFalse(task.Wait(100));
  85. ((IDataflowBlock)source).Fault(new Exception());
  86. Assert.IsTrue(task.Wait(1000));
  87. Assert.IsFalse(task.Result);
  88. }
  89. [Test]
  90. public void WithTargetTest()
  91. {
  92. var source = new BufferBlock<int>();
  93. var target = new BufferBlock<int>();
  94. source.LinkTo(target);
  95. var task = source.OutputAvailableAsync();
  96. Assert.IsTrue(source.Post(1));
  97. Assert.IsFalse(task.Wait(100));
  98. }
  99. [Test]
  100. public void WithDecliningTargetTest()
  101. {
  102. var source = new BufferBlock<int>();
  103. var target = new BufferBlock<int>();
  104. source.LinkTo(target, i => false);
  105. var task = source.OutputAvailableAsync();
  106. Assert.IsTrue(source.Post(1));
  107. Assert.IsTrue(task.Wait(1000));
  108. Assert.IsTrue(task.Result);
  109. }
  110. [Test]
  111. public void CancellationTest()
  112. {
  113. var source = new BufferBlock<int>();
  114. var tokenSource = new CancellationTokenSource();
  115. var task = source.OutputAvailableAsync(tokenSource.Token);
  116. tokenSource.Cancel();
  117. AssertEx.Throws<AggregateException>(() => task.Wait(1000));
  118. Assert.IsTrue(task.IsCanceled);
  119. }
  120. }
  121. }