1 // DataflowBlockTest.cs
4 // Petr Onderka <gsvick@gmail.com>
6 // Copyright (c) 2012 Petr Onderka
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Threading;
28 using System.Threading.Tasks;
29 using System.Threading.Tasks.Dataflow;
30 using NUnit.Framework;
32 namespace MonoTests.System.Threading.Tasks.Dataflow {
34 public class ChooseTest {
36 public void BasicTest ()
38 var source1 = new BufferBlock<int> ();
39 var source2 = new BufferBlock<long> ();
43 var completion = DataflowBlock.Choose (
44 source1, _ => action1 = true,
45 source2, _ => action2 = true);
49 Assert.IsTrue (completion.Wait (500));
50 Assert.AreEqual (0, completion.Result);
51 Assert.IsTrue (action1);
52 Assert.IsFalse (action2);
56 public void OnlyOneConsumedTest ()
58 var source1 = new BufferBlock<int> ();
59 var source2 = new BufferBlock<long> ();
63 var completion = DataflowBlock.Choose (
64 source1, _ => action1++,
65 source2, _ => action2++);
70 Assert.IsTrue (completion.Wait (500));
71 Assert.AreEqual (0, completion.Result);
72 Assert.AreEqual (1, action1);
73 Assert.AreEqual (0, action2);
76 Assert.IsTrue (source1.TryReceive (out item));
77 Assert.AreEqual (43, item);
81 public void RaceTest ()
83 var source1 = new BufferBlock<int> ();
84 var source2 = new BufferBlock<int> ();
88 var completion = DataflowBlock.Choose (
89 source1, _ => action1++,
90 source2, _ => action2++);
92 var barrier = new Barrier (2);
94 var t1 = Task.Run (() =>
96 barrier.SignalAndWait ();
99 var t2 = Task.Run (() =>
101 barrier.SignalAndWait ();
105 Task.WaitAll (t1, t2);
107 Assert.IsTrue (completion.Wait (500));
108 Assert.AreEqual (1, action1 + action2);
111 Assert.IsTrue (source1.TryReceive (out item) || source2.TryReceive (out item));
115 public void BlockCompletionTest ()
117 var source1 = new BufferBlock<int> ();
118 var source2 = new BufferBlock<long> ();
120 var completion = DataflowBlock.Choose (
121 source1, _ => { }, source2, _ => { });
123 Assert.IsFalse (completion.IsCanceled);
125 ((IDataflowBlock)source1).Fault (new Exception ());
130 Assert.IsTrue (completion.IsCanceled);
134 public void CancellationTest ()
136 var source1 = new BufferBlock<int> ();
137 var source2 = new BufferBlock<long> ();
139 var tokenSource = new CancellationTokenSource ();
140 var options = new DataflowBlockOptions
141 { CancellationToken = tokenSource.Token };
143 var completion = DataflowBlock.Choose (
144 source1, _ => { }, source2, _ => { }, options);
146 Assert.IsFalse (completion.IsCanceled);
148 tokenSource.Cancel ();
152 Assert.IsTrue (completion.IsCanceled);
156 public void ConsumeToAcceptTest ()
158 var source1 = new BroadcastBlock<int> (_ => 42);
159 var source2 = new BufferBlock<int> ();
163 var completion = DataflowBlock.Choose (
164 source1, i => action1 = i,
165 source2, i => action2 = i);
169 Assert.IsTrue (completion.Wait (500));
170 Assert.AreEqual (0, completion.Result);
171 Assert.AreEqual (42, action1);
172 Assert.AreEqual (0, action2);
176 public void ExceptionTest ()
178 var source1 = new BufferBlock<int> ();
179 var source2 = new BufferBlock<long> ();
181 var exception = new Exception ();
182 var completion = DataflowBlock.Choose (
183 source1, _ => { throw exception; },
188 var ae = AssertEx.Throws<AggregateException> (() => completion.Wait (500));
189 Assert.AreEqual (1, ae.InnerExceptions.Count);
190 Assert.AreSame (exception, ae.InnerException);
194 public void BasicTest_3 ()
196 var source1 = new BufferBlock<int> ();
197 var source2 = new BufferBlock<long> ();
198 var source3 = new BufferBlock<object> ();
200 bool action1 = false;
201 bool action2 = false;
202 bool action3 = false;
203 var completion = DataflowBlock.Choose (
204 source1, _ => action1 = true,
205 source2, _ => action2 = true,
206 source3, _ => action3 = true);
208 source3.Post (new object ());
210 Assert.IsTrue (completion.Wait (500));
211 Assert.AreEqual (2, completion.Result);
212 Assert.IsFalse (action1);
213 Assert.IsFalse (action2);
214 Assert.IsTrue (action3);