[System] Fixes UdpClient.Receive with IPv6 endpoint
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / ActionBlockTest.cs
1 // 
2 // ActionBlockTest.cs
3 //  
4 // Author:
5 //       Jérémie "garuma" Laval <jeremie.laval@gmail.com>
6 //       Petr Onderka <gsvick@gmail.com>
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
29 using System.Linq;
30 using System.Threading;
31 using System.Threading.Tasks;
32 using System.Threading.Tasks.Dataflow;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Threading.Tasks.Dataflow {
37         [TestFixture]
38         public class ActionBlockTest {
39                 [Test]
40                 public void BasicUsageTest ()
41                 {
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 (); });
45
46                         for (int i = 0; i < array.Length; ++i)
47                                 Assert.IsTrue (block.Post (i), "Not accepted");
48
49                         Assert.IsTrue (evt.Wait (500));
50                         
51                         Assert.IsTrue (array.All (b => b), "Some false");
52                 }
53
54                 [Test]
55                 public void CompleteTest ()
56                 {
57                         var block = new ActionBlock<int> (i => Thread.Sleep (100));
58
59                         for (int i = 0; i < 10; i++)
60                                 Assert.IsTrue (block.Post (i), "Not Accepted");
61
62                         block.Complete ();
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);
67                 }
68
69                 [Test]
70                 public void AsyncNullTest()
71                 {
72                         var scheduler = new TestScheduler ();
73                         var block = new ActionBlock<int> (
74                                 i => null,
75                                 new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
76
77                         Assert.IsTrue (block.Post (1));
78
79                         scheduler.ExecuteAll ();
80
81                         Assert.IsFalse (block.Completion.Wait (100));
82
83                         block.Complete ();
84
85                         Assert.IsTrue (block.Completion.Wait (100));
86                 }
87
88                 [Test]
89                 public void AsyncCancelledTest()
90                 {
91                         var scheduler = new TestScheduler ();
92                         var block = new ActionBlock<int> (
93                                 i =>
94                                 {
95                                         var tcs = new TaskCompletionSource<int> ();
96                                         tcs.SetCanceled ();
97                                         return tcs.Task;
98                                 }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
99
100                         Assert.IsTrue (block.Post (1));
101
102                         scheduler.ExecuteAll ();
103
104                         Assert.IsFalse (block.Completion.Wait (100));
105                 }
106         }
107 }