[System] Fixes UdpClient.Receive with IPv6 endpoint
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / TransformManyBlockTest.cs
1 // 
2 // TransformManyBlockTest.cs
3 //  
4 // Authors:
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.Collections.Generic;
30 using System.Linq;
31 using System.Threading;
32 using System.Threading.Tasks;
33 using System.Threading.Tasks.Dataflow;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Threading.Tasks.Dataflow {
38         [TestFixture]
39         public class TransformManyBlockTest {
40                 [Test]
41                 public void BasicUsageTest ()
42                 {
43                         int insIndex = -1;
44                         int[] array = new int[5 + 3];
45                         var evt = new CountdownEvent (array.Length);
46
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));
49                         trsm.LinkTo (block);
50
51                         trsm.Post (5);
52                         trsm.Post (3);
53
54                         evt.Wait ();
55                         
56                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 0, 1, 2 }, array);
57                 }
58
59                 [Test]
60                 public void DeferredUsageTest ()
61                 {
62                         int insIndex = -1;
63                         int[] array = new int[5 + 3];
64                         var evt = new CountdownEvent (array.Length);
65
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));
68
69                         trsm.Post (5);
70                         trsm.Post (3);
71
72                         trsm.LinkTo (block);
73                         evt.Wait ();
74
75                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 0, 1, 2 }, array);
76                 }
77
78                 [Test]
79                 public void NullResultTest ()
80                 {
81                         bool received = false;
82
83                         var transformMany =
84                                 new TransformManyBlock<int, int> (i => (IEnumerable<int>)null);
85                         var action = new ActionBlock<int> (i => received = true);
86                         transformMany.LinkTo (action);
87
88                         Assert.IsTrue (transformMany.Post (1), "#1");
89
90                         transformMany.Complete ();
91                         Assert.IsTrue (transformMany.Completion.Wait (100), "#2");
92                         Assert.IsFalse (received, "#3");
93                 }
94
95                 [Test]
96                 public void AsyncNullTest ()
97                 {
98                         var scheduler = new TestScheduler ();
99                         var block = new TransformManyBlock<int, int> (
100                                 i => (Task<IEnumerable<int>>)null,
101                                 new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
102
103                         Assert.IsTrue (block.Post (1));
104
105                         scheduler.ExecuteAll ();
106
107                         Assert.IsFalse (block.Completion.Wait (100));
108
109                         block.Complete ();
110
111                         Assert.IsTrue (block.Completion.Wait (100));
112                 }
113
114                 [Test]
115                 public void AsyncCancelledTest ()
116                 {
117                         var scheduler = new TestScheduler ();
118                         var block = new TransformManyBlock<int, int> (
119                                 i =>
120                                 {
121                                         var tcs = new TaskCompletionSource<IEnumerable<int>> ();
122                                         tcs.SetCanceled ();
123                                         return tcs.Task;
124                                 }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
125
126                         Assert.IsTrue (block.Post (1));
127
128                         scheduler.ExecuteAll ();
129
130                         Assert.IsFalse (block.Completion.Wait (100));
131                 }
132         }
133 }