Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskCompletionSourceTests.cs
1 // 
2 // TaskCompletionSourceTests.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 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
28 using System;
29 using System.Threading;
30 using System.Threading.Tasks;
31
32 using NUnit.Framework;
33 #if !MOBILE
34 using NUnit.Framework.SyntaxHelpers;
35 #endif
36
37 namespace MonoTests.System.Threading.Tasks
38 {
39         [TestFixture]
40         public class TaskCompletionSourceTests
41         {
42                 TaskCompletionSource<int> completionSource;
43                 object state;
44                 
45                 [SetUp]
46                 public void Setup ()
47                 {
48                         state = new object ();
49                         completionSource = new TaskCompletionSource<int> (state, TaskCreationOptions.None);
50                 }
51                 
52                 [Test]
53                 public void CreationCheckTest ()
54                 {
55                         Assert.IsNotNull (completionSource.Task, "#1");
56                         Assert.AreEqual (TaskCreationOptions.None, completionSource.Task.CreationOptions, "#2");
57                 }
58
59                 [Test]
60                 public void CtorInvalidOptions ()
61                 {
62                         try {
63                                 new TaskCompletionSource<long> (TaskCreationOptions.LongRunning);
64                                 Assert.Fail ("#1");
65                         } catch (ArgumentOutOfRangeException) {
66                         }
67
68                         try {
69                                 new TaskCompletionSource<long> (TaskCreationOptions.PreferFairness);
70                                 Assert.Fail ("#2");
71                         } catch (ArgumentOutOfRangeException) {
72                         }
73                 }
74                 
75                 [Test]
76                 public void SetResultTest ()
77                 {
78                         Assert.IsNotNull (completionSource.Task, "#1");
79                         Assert.IsTrue (completionSource.TrySetResult (42), "#2");
80                         Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
81                         Assert.AreEqual (42, completionSource.Task.Result, "#4");
82                         Assert.IsFalse (completionSource.TrySetResult (43), "#5");
83                         Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#6");
84                         Assert.AreEqual (42, completionSource.Task.Result, "#7");
85                         Assert.IsFalse (completionSource.TrySetCanceled (), "#8");
86                         Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#9");
87                 }
88                 
89                 [Test]
90                 public void SetCanceledTest ()
91                 {
92                         Assert.IsNotNull (completionSource.Task, "#1");
93                         Assert.IsTrue (completionSource.TrySetCanceled (), "#2");
94                         Assert.AreEqual (TaskStatus.Canceled, completionSource.Task.Status, "#3");
95                         Assert.IsFalse (completionSource.TrySetResult (42), "#4");
96                         Assert.AreEqual (TaskStatus.Canceled, completionSource.Task.Status, "#5");
97
98                         try {
99                                 Console.WriteLine (completionSource.Task.Result);
100                                 Assert.Fail ("#6");
101                         } catch (AggregateException e) {
102                                 var details = (TaskCanceledException) e.InnerException;
103                                 Assert.AreEqual (completionSource.Task, details.Task, "#6e");
104                                 Assert.IsNull (details.Task.Exception, "#6e2");
105                         }
106                 }
107
108                 [Test]
109                 public void SetExceptionTest ()
110                 {
111                         Exception e = new Exception ("foo");
112                         
113                         Assert.IsNotNull (completionSource.Task, "#1");
114                         Assert.IsTrue (completionSource.TrySetException (e), "#2");
115                         Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#3");
116                         Assert.That (completionSource.Task.Exception, Is.TypeOf(typeof (AggregateException)), "#4.1");
117                         
118                         AggregateException aggr = (AggregateException)completionSource.Task.Exception;
119                         Assert.AreEqual (1, aggr.InnerExceptions.Count, "#4.2");
120                         Assert.AreEqual (e, aggr.InnerExceptions[0], "#4.3");
121                         
122                         Assert.IsFalse (completionSource.TrySetResult (42), "#5");
123                         Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#6");
124                         Assert.IsFalse (completionSource.TrySetCanceled (), "#8");
125                         Assert.AreEqual (TaskStatus.Faulted, completionSource.Task.Status, "#9");
126                 }
127
128                 [Test]
129                 public void SetExceptionInvalid ()
130                 {
131                         try {
132                                 completionSource.TrySetException (new ApplicationException[0]);
133                                 Assert.Fail ("#1");
134                         } catch (ArgumentException) {
135                         }
136
137                         try {
138                                 completionSource.TrySetException (new [] { new ApplicationException (), null });
139                                 Assert.Fail ("#2");
140                         } catch (ArgumentException) {
141                         }
142
143                         Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "r1");
144                 }
145                 
146                 [Test, ExpectedException (typeof (InvalidOperationException))]
147                 public void SetResultExceptionTest ()
148                 {
149                         Assert.IsNotNull (completionSource.Task, "#1");
150                         Assert.IsTrue (completionSource.TrySetResult (42), "#2");
151                         Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#3");
152                         Assert.AreEqual (42, completionSource.Task.Result, "#4");
153                         
154                         completionSource.SetResult (43);
155                 }
156
157                 [Test]
158                 public void ContinuationTest ()
159                 {
160                         bool result = false;
161                         var t = completionSource.Task.ContinueWith ((p) => { if (p.Result == 2) result = true; });
162                         Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "#A");
163                         completionSource.SetResult (2);
164                         t.Wait ();
165                         Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#1");
166                         Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#2");
167                         Assert.IsTrue (result);
168                 }
169
170                 [Test]
171                 public void FaultedFutureTest ()
172                 {
173                         var thrown = new ApplicationException ();
174                         var source = new TaskCompletionSource<int> ();
175                         source.TrySetException (thrown);
176                         var f = source.Task;
177                         AggregateException ex = null;
178                         try {
179                                 f.Wait ();
180                         } catch (AggregateException e) {
181                                 ex = e;
182                         }
183
184                         Assert.IsNotNull (ex);
185                         Assert.AreEqual (thrown, ex.InnerException);
186                         Assert.AreEqual (thrown, f.Exception.InnerException);
187                         Assert.AreEqual (TaskStatus.Faulted, f.Status);
188
189                         ex = null;
190                         try {
191                                 var result = f.Result;
192                         } catch (AggregateException e) {
193                                 ex = e;
194                         }
195
196                         Assert.IsNotNull (ex);
197                         Assert.AreEqual (TaskStatus.Faulted, f.Status);
198                         Assert.AreEqual (thrown, f.Exception.InnerException);
199                         Assert.AreEqual (thrown, ex.InnerException);
200                 }
201
202                 [Test]
203                 [Ignore ("#4550, Mono GC is lame")]
204                 public void SetExceptionAndUnobservedEvent ()
205                 {
206                         bool notFromMainThread = false;
207                         var mre = new ManualResetEvent (false);
208                         int mainThreadId = Thread.CurrentThread.ManagedThreadId;
209                         TaskScheduler.UnobservedTaskException += (o, args) => {
210                                 notFromMainThread = Thread.CurrentThread.ManagedThreadId != mainThreadId;
211                                 args.SetObserved ();
212                                 mre.Set ();
213                         };
214                         var inner = new ApplicationException ();
215                         CreateFaultedTaskCompletionSource (inner);
216                         GC.Collect ();
217                         GC.WaitForPendingFinalizers ();
218
219                         Assert.IsTrue (mre.WaitOne (5000), "#1");
220                         Assert.IsTrue (notFromMainThread, "#2");
221                 }
222
223                 void CreateFaultedTaskCompletionSource (Exception inner)
224                 {
225                         var tcs = new TaskCompletionSource<int> ();
226                         tcs.SetException (inner);
227                         tcs = null;
228                 }
229
230                 [Test]
231                 public void WaitingTest ()
232                 {
233                         var tcs = new TaskCompletionSource<int> ();
234                         var task = tcs.Task;
235                         bool result = task.Wait (50);
236
237                         Assert.IsFalse (result);
238                 }
239         }
240 }