Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / TransformBlockTest.cs
1 // 
2 // TransformBlockTest.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;
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 TransformBlockTest {
39                 [Test]
40                 public void BasicUsageTest ()
41                 {
42                         int[] array = new int[10];
43                         var evt = new CountdownEvent (10);
44                         var action = new ActionBlock<int> (i =>
45                         {
46                                 array [Math.Abs (i)] = i;
47                                 evt.Signal ();
48                         });
49                         var block = new TransformBlock<int, int> (i => -i);
50                         block.LinkTo (action);
51
52                         for (int i = 0; i < array.Length; ++i)
53                                 Assert.IsTrue (block.Post (i), "Not accepted");
54
55                         evt.Wait ();
56
57                         CollectionAssert.AreEqual (
58                                 new[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
59                 }
60
61                 [Test]
62                 public void DeferredUsageTest ()
63                 {
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);
67
68                         for (int i = 0; i < array.Length; ++i)
69                                 Assert.IsTrue (block.Post (i), "Not accepted");
70
71                         Thread.Sleep (300);
72                         block.LinkTo (action);
73                         Thread.Sleep (100);
74
75                         CollectionAssert.AreEqual (new[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
76                 }
77
78                 [Test]
79                 public void AsyncNullTest ()
80                 {
81                         var scheduler = new TestScheduler ();
82                         var block = new TransformBlock<int, int> (
83                                 i => null,
84                                 new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
85
86                         Assert.IsTrue (block.Post (1));
87
88                         scheduler.ExecuteAll ();
89
90                         Assert.IsFalse (block.Completion.Wait (100));
91
92                         block.Complete ();
93
94                         Assert.IsTrue (block.Completion.Wait (1000));
95                 }
96
97                 [Test]
98                 public void AsyncCancelledTest ()
99                 {
100                         var scheduler = new TestScheduler ();
101                         var block = new TransformBlock<int, int> (
102                                 i =>
103                                 {
104                                         var tcs = new TaskCompletionSource<int> ();
105                                         tcs.SetCanceled ();
106                                         return tcs.Task;
107                                 }, new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
108
109                         Assert.IsTrue (block.Post (1));
110
111                         scheduler.ExecuteAll ();
112
113                         Assert.IsFalse (block.Completion.Wait (100));
114                 }
115         }
116 }