Merge pull request #225 from mistoll/master
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / DataflowBlockTest.cs
1 // 
2 // DataflowBlockTest.cs
3 //  
4 // Author:
5 //       Jérémie "garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2011 Jérémie "garuma" Laval
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27 using System;
28 using System.Linq;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Threading.Tasks.Dataflow;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Threading.Tasks.Dataflow
36 {
37         [TestFixture]
38         public class DataflowBlockTest
39         {
40                 [Test]
41                 public void ChooseTest ()
42                 {
43                         var source1 = new BufferBlock<int> ();
44                         var source2 = new BufferBlock<long> ();
45
46                         bool action1 = false;
47                         bool action2 = false;
48                         var completion = DataflowBlock.Choose (source1, (_) => action1 = true, source2, (_) => action2 = true);
49
50                         source1.Post (42);
51
52                         Thread.Sleep (1600);
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);
58                 }
59
60                 [Test]
61                 public void ChooseTest_3 ()
62                 {
63                         var source1 = new BufferBlock<int> ();
64                         var source2 = new BufferBlock<long> ();
65                         var source3 = new BufferBlock<object> ();
66
67                         bool action1 = false;
68                         bool action2 = false;
69                         bool action3 = false;
70                         var completion = DataflowBlock.Choose (source1, (_) => action1 = true, source2, (_) => action2 = true, source3, (_) => action3 = true);
71
72                         source3.Post (new object ());
73
74                         Thread.Sleep (1600);
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);                 
81                 }
82
83                 [Test]
84                 public void TryReceiveTest ()
85                 {
86                         var block = new BufferBlock<int> ();
87                         int value = -1;
88
89                         block.Post (42);
90                         Thread.Sleep (500);
91                         Assert.IsTrue (block.TryReceive (out value));
92                         Assert.AreEqual (42, value);
93                 }
94
95                 [Test]
96                 public void ReceiveTest ()
97                 {
98                         var block = new BufferBlock<int> ();
99                         Task.Factory.StartNew (() => { Thread.Sleep (1400); block.Post (42); });
100                         Assert.AreEqual (42, block.Receive ());
101                 }
102
103                 [Test]
104                 public void AsyncReceiveTest ()
105                 {
106                         int result = -1;
107                         var mre = new ManualResetEventSlim (false);
108
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); });
112                         mre.Wait ();
113
114                         Assert.AreEqual (42, result);
115                 }
116
117                 [Test]
118                 public void AsyncReceiveTestCanceled ()
119                 {
120                         var src = new CancellationTokenSource ();
121
122                         var block = new WriteOnceBlock<int> (null);
123                         var task = block.ReceiveAsync (src.Token);
124                         Task.Factory.StartNew (() => { Thread.Sleep (800); block.Post (42); });
125                         Thread.Sleep (50);
126                         src.Cancel ();
127
128                         AggregateException ex = null;
129
130                         try {
131                                 task.Wait ();
132                         } catch (AggregateException e) {
133                                 ex = e;
134                         }
135
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);
141                 }
142         }
143 }