5 // Jérémie "garuma" Laval <jeremie.laval@gmail.com>
6 // Petr Onderka <gsvick@gmail.com>
8 // Copyright (c) 2011 Jérémie "garuma" Laval
9 // Copyright (c) 2012 Petr Onderka
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:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
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
30 using System.Threading;
31 using System.Threading.Tasks.Dataflow;
32 using NUnit.Framework;
34 namespace MonoTests.System.Threading.Tasks.Dataflow {
36 public class JoinBlock3Test {
38 public void BasicUsageTest ()
40 Tuple<int, int, int> tuple = null;
41 var evt = new ManualResetEventSlim (false);
43 var ablock = new ActionBlock<Tuple<int, int, int>> (t =>
48 var block = new JoinBlock<int, int, int> ();
49 block.LinkTo (ablock);
51 block.Target1.Post (42);
54 Assert.IsNull (tuple);
56 block.Target2.Post (24);
59 Assert.IsNull (tuple);
61 block.Target3.Post (44);
64 Assert.IsNotNull (tuple);
65 Assert.AreEqual (42, tuple.Item1);
66 Assert.AreEqual (24, tuple.Item2);
67 Assert.AreEqual (44, tuple.Item3);
71 public void CompletionTest ()
73 var block = new JoinBlock<int, int, int> ();
75 Assert.IsTrue (block.Target1.Post (1));
79 Tuple<int, int, int> tuple;
80 Assert.IsFalse (block.TryReceive (out tuple));
82 Assert.IsTrue (block.Completion.Wait (100));
86 public void MaxNumberOfGroupsTest ()
88 var scheduler = new TestScheduler ();
89 var block = new JoinBlock<int, int, int> (
90 new GroupingDataflowBlockOptions
91 { MaxNumberOfGroups = 1, TaskScheduler = scheduler });
93 Assert.IsTrue (block.Target1.Post (1));
95 Assert.IsFalse (block.Target1.Post (2));
97 Assert.IsTrue (block.Target2.Post (3));
98 Assert.IsTrue (block.Target3.Post (4));
100 Assert.IsFalse (block.Target3.Post (4));
101 Assert.IsFalse (block.Target2.Post (4));
103 Tuple<int, int, int> batch;
104 Assert.IsTrue (block.TryReceive (out batch));
105 Assert.AreEqual (Tuple.Create (1, 3, 4), batch);
107 Assert.IsFalse (block.TryReceive (out batch));
109 scheduler.ExecuteAll ();
111 Assert.IsTrue (block.Completion.Wait (100));
115 public void NonGreedyMaxNumberOfGroupsTest ()
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> ();
125 var header1 = new DataflowMessageHeader (1);
126 source1.AddMessage (header1, 11);
127 source2.AddMessage (header1, 21);
128 source3.AddMessage (header1, 31);
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));
137 scheduler.ExecuteAll ();
139 Assert.IsTrue (source1.WasConsumed (header1));
140 Assert.IsTrue (source2.WasConsumed (header1));
141 Assert.IsTrue (source3.WasConsumed (header1));
143 var header2 = new DataflowMessageHeader (2);
144 Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
145 block.Target1.OfferMessage (header2, 21, source1, false));
147 Tuple<int, int, int> tuple;
148 Assert.IsTrue (block.TryReceive (out tuple));
149 Assert.AreEqual (Tuple.Create (11, 21, 31), tuple);
151 Assert.IsTrue (block.Completion.Wait (100));