2 // TaskCompletionSourceTests.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 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;
32 using NUnit.Framework;
34 using NUnit.Framework.SyntaxHelpers;
37 namespace MonoTests.System.Threading.Tasks
40 public class TaskCompletionSourceTests
42 TaskCompletionSource<int> completionSource;
48 state = new object ();
49 completionSource = new TaskCompletionSource<int> (state, TaskCreationOptions.None);
53 public void CreationCheckTest ()
55 Assert.IsNotNull (completionSource.Task, "#1");
56 Assert.AreEqual (TaskCreationOptions.None, completionSource.Task.CreationOptions, "#2");
60 public void CtorInvalidOptions ()
63 new TaskCompletionSource<long> (TaskCreationOptions.LongRunning);
65 } catch (ArgumentOutOfRangeException) {
69 new TaskCompletionSource<long> (TaskCreationOptions.PreferFairness);
71 } catch (ArgumentOutOfRangeException) {
76 public void SetResultTest ()
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");
90 public void SetCanceledTest ()
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");
99 Console.WriteLine (completionSource.Task.Result);
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");
109 public void SetExceptionTest ()
111 Exception e = new Exception ("foo");
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");
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");
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");
129 public void SetExceptionInvalid ()
132 completionSource.TrySetException (new ApplicationException[0]);
134 } catch (ArgumentException) {
138 completionSource.TrySetException (new [] { new ApplicationException (), null });
140 } catch (ArgumentException) {
143 Assert.AreEqual (TaskStatus.WaitingForActivation, completionSource.Task.Status, "r1");
146 [Test, ExpectedException (typeof (InvalidOperationException))]
147 public void SetResultExceptionTest ()
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");
154 completionSource.SetResult (43);
158 public void ContinuationTest ()
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);
165 Assert.AreEqual (TaskStatus.RanToCompletion, completionSource.Task.Status, "#1");
166 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#2");
167 Assert.IsTrue (result);
171 public void FaultedFutureTest ()
173 var thrown = new ApplicationException ();
174 var source = new TaskCompletionSource<int> ();
175 source.TrySetException (thrown);
177 AggregateException ex = null;
180 } catch (AggregateException e) {
184 Assert.IsNotNull (ex);
185 Assert.AreEqual (thrown, ex.InnerException);
186 Assert.AreEqual (thrown, f.Exception.InnerException);
187 Assert.AreEqual (TaskStatus.Faulted, f.Status);
191 var result = f.Result;
192 } catch (AggregateException e) {
196 Assert.IsNotNull (ex);
197 Assert.AreEqual (TaskStatus.Faulted, f.Status);
198 Assert.AreEqual (thrown, f.Exception.InnerException);
199 Assert.AreEqual (thrown, ex.InnerException);
203 [Ignore ("#4550, Mono GC is lame")]
204 public void SetExceptionAndUnobservedEvent ()
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;
214 var inner = new ApplicationException ();
215 CreateFaultedTaskCompletionSource (inner);
217 GC.WaitForPendingFinalizers ();
219 Assert.IsTrue (mre.WaitOne (5000), "#1");
220 Assert.IsTrue (notFromMainThread, "#2");
223 void CreateFaultedTaskCompletionSource (Exception inner)
225 var tcs = new TaskCompletionSource<int> ();
226 tcs.SetException (inner);
231 public void WaitingTest ()
233 var tcs = new TaskCompletionSource<int> ();
235 bool result = task.Wait (50);
237 Assert.IsFalse (result);