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 ActionBlockTest {
40 public void BasicUsageTest ()
42 bool[] array = new bool[3];
43 var evt = new CountdownEvent (array.Length);
44 var block = new ActionBlock<int> (i => { array[i] = true; evt.Signal (); });
46 for (int i = 0; i < array.Length; ++i)
47 Assert.IsTrue (block.Post (i), "Not accepted");
49 Assert.IsTrue (evt.Wait (500));
51 Assert.IsTrue (array.All (b => b), "Some false");
55 public void CompleteTest ()
57 var block = new ActionBlock<int> (i => Thread.Sleep (100));
59 for (int i = 0; i < 10; i++)
60 Assert.IsTrue (block.Post (i), "Not Accepted");
63 // Still element to be processed so Completion should be false
64 Assert.IsFalse (block.Completion.IsCompleted);
65 block.Completion.Wait ();
66 Assert.IsTrue (block.Completion.IsCompleted);
70 public void AsyncNullTest()
72 var scheduler = new TestScheduler ();
73 var block = new ActionBlock<int> (
75 new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
77 Assert.IsTrue (block.Post (1));
79 scheduler.ExecuteAll ();
81 Assert.IsFalse (block.Completion.Wait (100));
85 Assert.IsTrue (block.Completion.Wait (100));
89 public void AsyncCancelledTest()
91 var scheduler = new TestScheduler ();
92 var block = new ActionBlock<int> (
95 var tcs = new TaskCompletionSource<int> ();
98 }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
100 Assert.IsTrue (block.Post (1));
102 scheduler.ExecuteAll ();
104 Assert.IsFalse (block.Completion.Wait (100));