Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / OutputAvailableTest.cs
1 // OutputAvailableTest.cs
2 //  
3 // Copyright (c) 2012 Petr Onderka
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22
23 using System;
24 using System.Threading;
25 using System.Threading.Tasks.Dataflow;
26 using NUnit.Framework;
27
28 namespace MonoTests.System.Threading.Tasks.Dataflow {
29         [TestFixture]
30         public class OutputAvailableTest {
31                 [Test]
32                 public void ImmediateTest()
33                 {
34                         var scheduler = new TestScheduler();
35                         var source = new BufferBlock<int>(new DataflowBlockOptions{TaskScheduler = scheduler});
36
37                         Assert.IsTrue(source.Post(1));
38
39                         var task = source.OutputAvailableAsync();
40
41                         // not necessary in MS.NET
42                         scheduler.ExecuteAll();
43
44                         Assert.IsTrue(task.IsCompleted);
45                         Assert.IsTrue(task.Result);
46                 }
47
48                 [Test]
49                 public void PostponedTest()
50                 {
51                         var source = new BufferBlock<int>(new DataflowBlockOptions());
52
53                         var task = source.OutputAvailableAsync();
54
55                         Assert.IsFalse(task.Wait(100));
56
57                         Assert.IsTrue(source.Post(1));
58
59                         Assert.IsTrue(task.Wait(100));
60                         Assert.IsTrue(task.Result);
61                 }
62
63                 [Test]
64                 public void CompletedImmediateTest()
65                 {
66                         var source = new BufferBlock<int>();
67                         source.Complete();
68
69                         var task = source.OutputAvailableAsync();
70
71                         Assert.IsTrue(task.Wait(100));
72                         Assert.IsFalse(task.Result);
73                 }
74
75                 [Test]
76                 public void CompletedPostponedTest()
77                 {
78                         var source = new BufferBlock<int>(new DataflowBlockOptions());
79
80                         var task = source.OutputAvailableAsync();
81
82                         Assert.IsFalse(task.Wait(100));
83
84                         source.Complete();
85
86                         Assert.IsTrue(task.Wait(100));
87                         Assert.IsFalse(task.Result);
88                 }
89
90                 [Test]
91                 public void FaultedImmediateTest()
92                 {
93                         var source = new BufferBlock<int>();
94                         ((IDataflowBlock)source).Fault(new Exception());
95
96                         var task = source.OutputAvailableAsync();
97
98                         Assert.IsTrue(task.Wait(100));
99                         Assert.IsFalse(task.Result);
100                 }
101
102                 [Test]
103                 public void FaultedPostponedTest()
104                 {
105                         var source = new BufferBlock<int>();
106
107                         var task = source.OutputAvailableAsync();
108
109                         Assert.IsFalse(task.Wait(100));
110
111                         ((IDataflowBlock)source).Fault(new Exception());
112
113                         Assert.IsTrue(task.Wait(100));
114                         Assert.IsFalse(task.Result);
115                 }
116
117                 [Test]
118                 public void WithTargetTest()
119                 {
120                         var source = new BufferBlock<int>();
121                         var target = new BufferBlock<int>();
122                         source.LinkTo(target);
123
124                         var task = source.OutputAvailableAsync();
125
126                         Assert.IsTrue(source.Post(1));
127
128                         Assert.IsFalse(task.Wait(100));
129                 }
130
131                 [Test]
132                 public void WithDecliningTargetTest()
133                 {
134                         var source = new BufferBlock<int>();
135                         var target = new BufferBlock<int>();
136                         source.LinkTo(target, i => false);
137
138                         var task = source.OutputAvailableAsync();
139
140                         Assert.IsTrue(source.Post(1));
141
142                         Assert.IsTrue(task.Wait(100));
143                         Assert.IsTrue(task.Result);
144                 }
145
146                 [Test]
147                 public void CancellationTest()
148                 {
149                         var source = new BufferBlock<int>();
150
151                         var tokenSource = new CancellationTokenSource();
152                         var task = source.OutputAvailableAsync(tokenSource.Token);
153
154                         tokenSource.Cancel();
155
156                         AssertEx.Throws<AggregateException>(() => task.Wait(100));
157                         Assert.IsTrue(task.IsCanceled);
158                 }
159         }
160 }