[corlib] Fix a few NSLogWriter issues. (#13799)
[mono-project.git] / mcs / tests / test-async-23.cs
blobc702827bba35e8ec1a81ec6425e1efb859a8a766
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
5 class MyContext : SynchronizationContext
7 public int Started;
8 public int Completed;
9 public int PostCounter;
10 public int SendCounter;
11 ManualResetEvent mre;
13 public MyContext (ManualResetEvent mre)
15 this.mre = mre;
18 public override void OperationStarted ()
20 ++Started;
21 base.OperationStarted ();
24 public override void OperationCompleted ()
26 ++Completed;
27 base.OperationCompleted ();
30 public override void Post (SendOrPostCallback d, object state)
32 ++PostCounter;
33 mre.Set ();
34 base.Post (d, state);
37 public override void Send (SendOrPostCallback d, object state)
39 ++SendCounter;
40 base.Send (d, state);
45 public class TestPostContext
47 static ManualResetEvent await_mre;
49 static async Task<int> Test ()
51 return await Task.Factory.StartNew (() => { await_mre.WaitOne(); return 1; });
54 public static int Main ()
56 var mre = new ManualResetEvent (false);
57 await_mre = new ManualResetEvent (false);
58 var context = new MyContext (mre);
59 try {
60 SynchronizationContext.SetSynchronizationContext (context);
61 var t = Test ();
62 await_mre.Set ();
63 if (!t.Wait (3000))
64 return 3;
66 // Wait is needed because synchronization is executed as continuation (once task finished)
67 if (!mre.WaitOne (3000))
68 return 2;
69 } finally {
70 SynchronizationContext.SetSynchronizationContext (null);
73 if (context.Started != 0 || context.Completed != 0 || context.SendCounter != 0)
74 return 1;
76 Console.WriteLine ("ok");
77 return 0;