2 // TransformBlockTest.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
30 using System.Threading;
31 using System.Threading.Tasks;
32 using System.Threading.Tasks.Dataflow;
34 using NUnit.Framework;
36 namespace MonoTests.System.Threading.Tasks.Dataflow {
38 public class TransformBlockTest {
40 public void BasicUsageTest ()
42 int[] array = new int[10];
43 var evt = new CountdownEvent (10);
44 var action = new ActionBlock<int> (i =>
46 array [Math.Abs (i)] = i;
49 var block = new TransformBlock<int, int> (i => -i);
50 block.LinkTo (action);
52 for (int i = 0; i < array.Length; ++i)
53 Assert.IsTrue (block.Post (i), "Not accepted");
57 CollectionAssert.AreEqual (
58 new[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
62 public void DeferredUsageTest ()
64 int[] array = new int[10];
65 var action = new ActionBlock<int> (i => array[Math.Abs (i)] = i);
66 var block = new TransformBlock<int, int> (i => -i);
68 for (int i = 0; i < array.Length; ++i)
69 Assert.IsTrue (block.Post (i), "Not accepted");
72 block.LinkTo (action);
75 CollectionAssert.AreEqual (new[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
79 public void AsyncNullTest ()
81 var scheduler = new TestScheduler ();
82 var block = new TransformBlock<int, int> (
84 new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
86 Assert.IsTrue (block.Post (1));
88 scheduler.ExecuteAll ();
90 Assert.IsFalse (block.Completion.Wait (100));
94 Assert.IsTrue (block.Completion.Wait (1000));
98 public void AsyncCancelledTest ()
100 var scheduler = new TestScheduler ();
101 var block = new TransformBlock<int, int> (
104 var tcs = new TaskCompletionSource<int> ();
107 }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
109 Assert.IsTrue (block.Post (1));
111 scheduler.ExecuteAll ();
113 Assert.IsFalse (block.Completion.Wait (100));