2 // DataflowBlockTest.cs
5 // Jérémie "garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2011 Jérémie "garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Threading.Tasks.Dataflow;
33 using NUnit.Framework;
35 namespace MonoTests.System.Threading.Tasks.Dataflow
38 public class DataflowBlockTest
41 public void ChooseTest ()
43 var source1 = new BufferBlock<int> ();
44 var source2 = new BufferBlock<long> ();
48 var completion = DataflowBlock.Choose (source1, (_) => action1 = true, source2, (_) => action2 = true);
53 Assert.IsTrue (action1);
54 Assert.IsFalse (action2);
55 Assert.IsTrue (completion.IsCompleted);
56 Assert.AreEqual (TaskStatus.RanToCompletion, completion.Status);
57 Assert.AreEqual (0, completion.Result);
61 public void ChooseTest_3 ()
63 var source1 = new BufferBlock<int> ();
64 var source2 = new BufferBlock<long> ();
65 var source3 = new BufferBlock<object> ();
70 var completion = DataflowBlock.Choose (source1, (_) => action1 = true, source2, (_) => action2 = true, source3, (_) => action3 = true);
72 source3.Post (new object ());
75 Assert.IsFalse (action1);
76 Assert.IsFalse (action2);
77 Assert.IsTrue (action3);
78 Assert.IsTrue (completion.IsCompleted);
79 Assert.AreEqual (TaskStatus.RanToCompletion, completion.Status);
80 Assert.AreEqual (2, completion.Result);
84 public void TryReceiveTest ()
86 var block = new BufferBlock<int> ();
91 Assert.IsTrue (block.TryReceive (out value));
92 Assert.AreEqual (42, value);
96 public void ReceiveTest ()
98 var block = new BufferBlock<int> ();
99 Task.Factory.StartNew (() => { Thread.Sleep (1400); block.Post (42); });
100 Assert.AreEqual (42, block.Receive ());
104 public void AsyncReceiveTest ()
107 var mre = new ManualResetEventSlim (false);
109 var block = new WriteOnceBlock<int> (null);
110 block.ReceiveAsync ().ContinueWith (i => { result = i.Result; mre.Set (); });
111 Task.Factory.StartNew (() => { Thread.Sleep (600); block.Post (42); });
114 Assert.AreEqual (42, result);
118 public void AsyncReceiveTestCanceled ()
120 var src = new CancellationTokenSource ();
122 var block = new WriteOnceBlock<int> (null);
123 var task = block.ReceiveAsync (src.Token);
124 Task.Factory.StartNew (() => { Thread.Sleep (800); block.Post (42); });
128 AggregateException ex = null;
132 } catch (AggregateException e) {
136 Assert.IsNotNull (ex);
137 Assert.IsNotNull (ex.InnerException);
138 Assert.IsInstanceOfType (typeof (OperationCanceledException), ex.InnerException);
139 Assert.IsTrue (task.IsCompleted);
140 Assert.AreEqual (TaskStatus.Canceled, task.Status);