[runtime] Actually clean up context-static data segments.
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / JoinBlock`3Test.cs
1 // 
2 // JoinBlockTest.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.Dataflow;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Threading.Tasks.Dataflow {
35         [TestFixture]
36         public class JoinBlock3Test {
37                 [Test]
38                 public void BasicUsageTest ()
39                 {
40                         Tuple<int, int, int> tuple = null;
41                         var evt = new ManualResetEventSlim (false);
42
43                         var ablock = new ActionBlock<Tuple<int, int, int>> (t =>
44                         {
45                                 tuple = t;
46                                 evt.Set ();
47                         });
48                         var block = new JoinBlock<int, int, int> ();
49                         block.LinkTo (ablock);
50
51                         block.Target1.Post (42);
52
53                         evt.Wait (500);
54                         Assert.IsNull (tuple);
55
56                         block.Target2.Post (24);
57
58                         evt.Wait (500);
59                         Assert.IsNull (tuple);
60
61                         block.Target3.Post (44);
62
63                         evt.Wait ();
64                         Assert.IsNotNull (tuple);
65                         Assert.AreEqual (42, tuple.Item1);
66                         Assert.AreEqual (24, tuple.Item2);
67                         Assert.AreEqual (44, tuple.Item3);
68                 }
69
70                 [Test]
71                 public void CompletionTest ()
72                 {
73                         var block = new JoinBlock<int, int, int> ();
74
75                         Assert.IsTrue (block.Target1.Post (1));
76
77                         block.Complete ();
78
79                         Tuple<int, int, int> tuple;
80                         Assert.IsFalse (block.TryReceive (out tuple));
81
82                         Assert.IsTrue (block.Completion.Wait (100));
83                 }
84
85                 [Test]
86                 public void MaxNumberOfGroupsTest ()
87                 {
88                         var scheduler = new TestScheduler ();
89                         var block = new JoinBlock<int, int, int> (
90                                 new GroupingDataflowBlockOptions
91                                 { MaxNumberOfGroups = 1, TaskScheduler = scheduler });
92
93                         Assert.IsTrue (block.Target1.Post (1));
94
95                         Assert.IsFalse (block.Target1.Post (2));
96
97                         Assert.IsTrue (block.Target2.Post (3));
98                         Assert.IsTrue (block.Target3.Post (4));
99
100                         Assert.IsFalse (block.Target3.Post (4));
101                         Assert.IsFalse (block.Target2.Post (4));
102
103                         Tuple<int, int, int> batch;
104                         Assert.IsTrue (block.TryReceive (out batch));
105                         Assert.AreEqual (Tuple.Create (1, 3, 4), batch);
106
107                         Assert.IsFalse (block.TryReceive (out batch));
108
109                         scheduler.ExecuteAll ();
110
111                         Assert.IsTrue (block.Completion.Wait (100));
112                 }
113
114                 [Test]
115                 public void NonGreedyMaxNumberOfGroupsTest ()
116                 {
117                         var scheduler = new TestScheduler ();
118                         var block = new JoinBlock<int, int, int> (
119                                 new GroupingDataflowBlockOptions
120                                 { MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler });
121                         var source1 = new TestSourceBlock<int> ();
122                         var source2 = new TestSourceBlock<int> ();
123                         var source3 = new TestSourceBlock<int> ();
124
125                         var header1 = new DataflowMessageHeader (1);
126                         source1.AddMessage (header1, 11);
127                         source2.AddMessage (header1, 21);
128                         source3.AddMessage (header1, 31);
129
130                         Assert.AreEqual (DataflowMessageStatus.Postponed,
131                                 block.Target1.OfferMessage (header1, 11, source1, false));
132                         Assert.AreEqual (DataflowMessageStatus.Postponed,
133                                 block.Target2.OfferMessage (header1, 21, source2, false));
134                         Assert.AreEqual (DataflowMessageStatus.Postponed,
135                                 block.Target3.OfferMessage (header1, 31, source3, false));
136
137                         scheduler.ExecuteAll ();
138
139                         Assert.IsTrue (source1.WasConsumed (header1));
140                         Assert.IsTrue (source2.WasConsumed (header1));
141                         Assert.IsTrue (source3.WasConsumed (header1));
142
143                         var header2 = new DataflowMessageHeader (2);
144                         Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
145                                 block.Target1.OfferMessage (header2, 21, source1, false));
146
147                         Tuple<int, int, int> tuple;
148                         Assert.IsTrue (block.TryReceive (out tuple));
149                         Assert.AreEqual (Tuple.Create (11, 21, 31), tuple);
150
151                         Assert.IsTrue (block.Completion.Wait (100));
152                 }
153         }
154 }