2 // YieldAwaitebleTest.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
33 using NUnit.Framework;
34 using System.Runtime.CompilerServices;
35 using System.Threading.Tasks;
36 using System.Threading;
38 namespace MonoTests.System.Runtime.CompilerServices
41 public class YieldAwaitableTest
43 class MyScheduler : TaskScheduler
45 protected override IEnumerable<Task> GetScheduledTasks ()
47 throw new NotImplementedException ();
50 protected override void QueueTask (Task task)
52 TryExecuteTask (task);
55 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
57 throw new NotImplementedException ();
61 class MyContext : SynchronizationContext
65 public int PostCounter;
66 public int SendCounter;
68 public override void OperationStarted ()
71 base.OperationStarted ();
74 public override void OperationCompleted ()
77 base.OperationCompleted ();
80 public override void Post (SendOrPostCallback d, object state)
86 public override void Send (SendOrPostCallback d, object state)
93 YieldAwaitable.YieldAwaiter a;
94 SynchronizationContext sc;
99 sc = SynchronizationContext.Current;
100 a = new YieldAwaitable ().GetAwaiter ();
104 public void TearDown ()
106 SynchronizationContext.SetSynchronizationContext (sc);
110 public void IsCompleted ()
112 Assert.IsFalse (a.IsCompleted, "#1");
114 Assert.IsFalse (a.IsCompleted, "#1");
118 public void OnCompleted_1 ()
121 a.OnCompleted (null);
123 } catch (ArgumentException) {
128 public void OnCompleted_2 ()
130 TaskScheduler scheduler = null;
131 SynchronizationContext.SetSynchronizationContext (null);
133 var mre = new ManualResetEvent (false);
135 a.OnCompleted (() => {
136 scheduler = TaskScheduler.Current;
140 Assert.IsTrue (mre.WaitOne (1000), "#1");
141 Assert.AreEqual (TaskScheduler.Current, scheduler, "#2");
145 public void OnCompleted_3 ()
147 var scheduler = new MyScheduler ();
148 TaskScheduler ran_scheduler = null;
149 SynchronizationContext.SetSynchronizationContext (null);
151 var t = Task.Factory.StartNew (() => {
152 var mre = new ManualResetEvent (false);
154 a.OnCompleted (() => {
155 ran_scheduler = TaskScheduler.Current;
161 }, CancellationToken.None, TaskCreationOptions.None, scheduler);
163 Assert.IsTrue (t.Wait (1000), "#1");
164 Assert.AreEqual (scheduler, ran_scheduler, "#2");
168 public void OnCompleted_4 ()
170 SynchronizationContext context_ran = null;
171 var mre = new ManualResetEvent (false);
173 var context = new MyContext ();
174 SynchronizationContext.SetSynchronizationContext (context);
175 a.OnCompleted (() => {
176 context_ran = SynchronizationContext.Current;
180 Assert.IsTrue (mre.WaitOne (1000), "#1");
181 Assert.IsNull (context_ran, "#2");