Test TriggerBatch() when the block is empty
[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 //       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.Threading;
30 using System.Threading.Tasks.Dataflow;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Threading.Tasks.Dataflow
35 {
36         [TestFixture]
37         public class BatchBlockTest
38         {
39                 [Test]
40                 public void BasicUsageTest ()
41                 {
42                         int[] array = null;
43                         var evt = new ManualResetEventSlim (false);
44
45                         var buffer = new BatchBlock<int> (10);
46                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
47                         buffer.LinkTo<int[]>(block);
48                         
49                         for (int i = 0; i < 9; i++)
50                                 Assert.IsTrue (buffer.Post (i));
51
52                         Assert.IsFalse (evt.Wait (100));
53
54                         Assert.IsNull (array);
55
56                         Assert.IsTrue (buffer.Post (42));
57                         Assert.IsTrue (evt.Wait (100));
58
59                         Assert.IsNotNull (array);
60                         CollectionAssert.AreEqual (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 42 }, array);
61                 }
62
63                 [Test]
64                 public void TriggerBatchTest ()
65                 {
66                         int[] array = null;
67                         var evt = new ManualResetEventSlim (false);
68
69                         var buffer = new BatchBlock<int> (10);
70                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
71                         buffer.LinkTo(block);
72                         
73                         for (int i = 0; i < 9; i++)
74                                 Assert.IsTrue (buffer.Post (i));
75
76                         buffer.TriggerBatch ();
77                         evt.Wait ();
78
79                         Assert.IsNotNull (array);
80                         Assert.IsTrue (buffer.Post (42));
81                         evt.Wait (1600);
82
83                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
84                 }
85
86                 [Test]
87                 public void TriggerBatchLateBinding ()
88                 {
89                         int[] array = null;
90                         var evt = new ManualResetEventSlim (false);
91
92                         var buffer = new BatchBlock<int> (10);
93                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
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                         evt.Wait ();
102                         Assert.IsNotNull (array);
103
104                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
105                 }
106
107                 [Test]
108                 public void LateTriggerBatchKeepCountTest ()
109                 {
110                         int[] array = null;
111                         var evt = new ManualResetEventSlim (false);
112
113                         var buffer = new BatchBlock<int> (15);
114                         var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
115                         
116                         for (int i = 0; i < 9; i++)
117                                 Assert.IsTrue (buffer.Post (i));
118                         buffer.TriggerBatch ();
119                         Assert.IsTrue (buffer.Post (42));
120                         buffer.LinkTo (block);
121
122                         evt.Wait ();
123
124                         Assert.IsNotNull (array);
125                         CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);                        
126                 }
127
128                 [Test]
129                 public void TriggerBatchWhenEmpty ()
130                 {
131                         var scheduler = new TestScheduler ();
132                         var block = new BatchBlock<int> (5,
133                                 new GroupingDataflowBlockOptions { TaskScheduler = scheduler });
134                         block.TriggerBatch ();
135
136                         scheduler.ExecuteAll ();
137
138                         int[] batch;
139                         Assert.IsFalse (block.TryReceive (out batch));
140                         Assert.IsNull (batch);
141                 }
142         }
143 }