Merge pull request #225 from mistoll/master
[mono.git] / mcs / tests / test-async-23.cs
1 // Compiler options: -langversion:future
2
3 using System;
4 using System.Threading;
5 using System.Threading.Tasks;
6
7 class MyContext : SynchronizationContext
8 {
9         public int Started;
10         public int Completed;
11         public int PostCounter;
12         public int SendCounter;
13
14         public override void OperationStarted ()
15         {
16                 ++Started;
17                 base.OperationStarted ();
18         }
19
20         public override void OperationCompleted ()
21         {
22                 ++Completed;
23                 base.OperationCompleted ();
24         }
25
26         public override void Post (SendOrPostCallback d, object state)
27         {
28                 ++PostCounter;
29                 base.Post (d, state);
30         }
31
32         public override void Send (SendOrPostCallback d, object state)
33         {
34                 ++SendCounter;
35                 base.Send (d, state);
36         }
37 }
38
39
40 public class TestPostContext
41 {
42         static async Task<int> Test ()
43         {
44                 return await Task.Factory.StartNew (() => 1);
45         }
46
47         public static int Main ()
48         {
49                 var context = new MyContext ();
50                 try {
51                         SynchronizationContext.SetSynchronizationContext (context);
52                         var t = Test ();
53                         if (!t.Wait (1000))
54                                 return 3;
55                 } finally {
56                         SynchronizationContext.SetSynchronizationContext (null);
57                 }
58
59                 if (context.Started != 0 || context.Completed != 0 || context.SendCounter != 0)
60                         return 1;
61
62                 if (context.PostCounter != 1)
63                         return 2;
64
65                 Console.WriteLine ("ok");
66                 return 0;
67         }
68 }