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