2 // TransformManyBlockTest.cs
5 // Jérémie "garuma" Laval <jeremie.laval@gmail.com>
6 // Petr Onderka <gsvick@gmail.com>
8 // Copyright (c) 2011 Jérémie "garuma" Laval
9 // Copyright (c) 2012 Petr Onderka
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:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
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
29 using System.Collections.Generic;
31 using System.Threading;
32 using System.Threading.Tasks;
33 using System.Threading.Tasks.Dataflow;
35 using NUnit.Framework;
37 namespace MonoTests.System.Threading.Tasks.Dataflow {
39 public class TransformManyBlockTest {
41 public void BasicUsageTest ()
44 int[] array = new int[5 + 3];
45 var evt = new CountdownEvent (array.Length);
47 var block = new ActionBlock<int> (i => { array[Interlocked.Increment (ref insIndex)] = i; evt.Signal (); });
48 var trsm = new TransformManyBlock<int, int> (i => Enumerable.Range (0, i));
56 CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 0, 1, 2 }, array);
60 public void DeferredUsageTest ()
63 int[] array = new int[5 + 3];
64 var evt = new CountdownEvent (array.Length);
66 var block = new ActionBlock<int> (i => { array[Interlocked.Increment (ref insIndex)] = i; evt.Signal (); });
67 var trsm = new TransformManyBlock<int, int> (i => Enumerable.Range (0, i));
75 CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 0, 1, 2 }, array);
79 public void NullResultTest ()
81 bool received = false;
84 new TransformManyBlock<int, int> (i => (IEnumerable<int>)null);
85 var action = new ActionBlock<int> (i => received = true);
86 transformMany.LinkTo (action);
88 Assert.IsTrue (transformMany.Post (1), "#1");
90 transformMany.Complete ();
91 Assert.IsTrue (transformMany.Completion.Wait (100), "#2");
92 Assert.IsFalse (received, "#3");
96 public void AsyncNullTest ()
98 var scheduler = new TestScheduler ();
99 var block = new TransformManyBlock<int, int> (
100 i => (Task<IEnumerable<int>>)null,
101 new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
103 Assert.IsTrue (block.Post (1));
105 scheduler.ExecuteAll ();
107 Assert.IsFalse (block.Completion.Wait (100));
111 Assert.IsTrue (block.Completion.Wait (100));
115 public void AsyncCancelledTest ()
117 var scheduler = new TestScheduler ();
118 var block = new TransformManyBlock<int, int> (
121 var tcs = new TaskCompletionSource<IEnumerable<int>> ();
124 }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
126 Assert.IsTrue (block.Post (1));
128 scheduler.ExecuteAll ();
130 Assert.IsFalse (block.Completion.Wait (100));