Merge pull request #162 from garuma/tpl-dataflow-blocks
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / BatchBlockTest.cs
1 // 
2 // BatchBlockTest.cs
3 //  
4 // Author:
5 //       Jérémie "garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2011 Jérémie "garuma" Laval
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.Linq;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Threading.Tasks.Dataflow;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Threading.Tasks.Dataflow
36 {
37         [TestFixture]
38         public class BatchBlockTest
39         {
40                 [Test]
41                 public void BasicUsageTest ()
42                 {
43                         int[] array = null;
44                         var evt = new ManualResetEventSlim (false);
45
46                         var buffer = new BatchBlock<int> (10);
47                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
48                         buffer.LinkTo<int[]>(block);
49                         
50                         for (int i = 0; i < 9; i++)
51                                 Assert.IsTrue (buffer.Post (i));
52
53                         evt.Wait (1600);
54
55                         Assert.IsNull (array);
56
57                         Assert.IsTrue (buffer.Post (42));
58                         evt.Wait ();
59
60                         Assert.IsNotNull (array);
61                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 42 }, array);
62                 }
63
64                 [Test]
65                 public void TriggerBatchTest ()
66                 {
67                         int[] array = null;
68                         var evt = new ManualResetEventSlim (false);
69
70                         var buffer = new BatchBlock<int> (10);
71                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
72                         buffer.LinkTo(block);
73                         
74                         for (int i = 0; i < 9; i++)
75                                 Assert.IsTrue (buffer.Post (i));
76
77                         buffer.TriggerBatch ();
78                         evt.Wait ();
79
80                         Assert.IsNotNull (array);
81                         Assert.IsTrue (buffer.Post (42));
82                         evt.Wait (1600);
83
84                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
85                 }
86
87                 [Test]
88                 public void TriggerBatchLateBinding ()
89                 {
90                         int[] array = null;
91                         var evt = new ManualResetEventSlim (false);
92
93                         var buffer = new BatchBlock<int> (10);
94                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
95                         
96                         for (int i = 0; i < 9; i++)
97                                 Assert.IsTrue (buffer.Post (i));
98
99                         buffer.TriggerBatch ();
100                         buffer.LinkTo (block);
101
102                         evt.Wait ();
103                         Assert.IsNotNull (array);
104
105                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
106                 }
107
108                 [Test]
109                 public void LateTriggerBatchKeepCountTest ()
110                 {
111                         int[] array = null;
112                         var evt = new ManualResetEventSlim (false);
113
114                         var buffer = new BatchBlock<int> (15);
115                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
116                         
117                         for (int i = 0; i < 9; i++)
118                                 Assert.IsTrue (buffer.Post (i));
119                         buffer.TriggerBatch ();
120                         Assert.IsTrue (buffer.Post (42));
121                         buffer.LinkTo (block);
122
123                         evt.Wait ();
124
125                         Assert.IsNotNull (array);
126                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);                        
127                 }
128         }
129 }