[System.Net.Http] HttpClient timeout range checks. Fixes #25755
[mono.git] / mcs / tests / test-async-23.cs
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
4
5 class MyContext : SynchronizationContext
6 {
7         public int Started;
8         public int Completed;
9         public int PostCounter;
10         public int SendCounter;
11         ManualResetEvent mre;
12         
13         public MyContext (ManualResetEvent mre)
14         {
15                 this.mre = mre;
16         }
17
18         public override void OperationStarted ()
19         {
20                 ++Started;
21                 base.OperationStarted ();
22         }
23
24         public override void OperationCompleted ()
25         {
26                 ++Completed;
27                 base.OperationCompleted ();
28         }
29
30         public override void Post (SendOrPostCallback d, object state)
31         {
32                 ++PostCounter;
33                 mre.Set ();
34                 base.Post (d, state);
35         }
36
37         public override void Send (SendOrPostCallback d, object state)
38         {
39                 ++SendCounter;
40                 base.Send (d, state);
41         }
42 }
43
44
45 public class TestPostContext
46 {
47         static ManualResetEvent await_mre;
48         
49         static async Task<int> Test ()
50         {
51                 return await Task.Factory.StartNew (() => { await_mre.WaitOne(); return 1; });
52         }
53
54         public static int Main ()
55         {
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;
65                                 
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);
71                 }
72
73                 if (context.Started != 0 || context.Completed != 0 || context.SendCounter != 0)
74                         return 1;
75
76                 Console.WriteLine ("ok");
77                 return 0;
78         }
79 }