Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mcs / class / System / Test / System.Net / WebClientTestAsync.cs
1 //
2 // System.Net.WebClientTestAsync
3 //
4 // Authors:
5 //      Martin Baulig (martin.baulig@googlemail.com)
6 //
7 // Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.IO;
31 using System.Collections.Generic;
32 using System.Threading;
33 using System.Threading.Tasks;
34 using System.Reflection;
35 using System.Net;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Net
39 {
40         [TestFixture]
41         public class WebClientTestAsync
42         {
43                 [Test]
44                 [Category("Async")]
45                 [Category("AndroidNotWorking")] // Attempts to access the test dll which won't work on Android
46                 public void DownloadData ()
47                 {
48                         WebClient wc;
49                         bool progress_changed = false;
50                         bool completed = false;
51                         bool progress_changed_error = false;
52                         bool completed_error = false;
53
54                         int thread_id = Thread.CurrentThread.ManagedThreadId;
55
56                         wc = new WebClient ();
57
58                         wc.DownloadProgressChanged += delegate {
59                                 progress_changed = true;
60                                 if (Thread.CurrentThread.ManagedThreadId != thread_id)
61                                         progress_changed_error = true;
62                         };
63                         wc.DownloadDataCompleted += delegate {
64                                 completed = true;
65                                 if (Thread.CurrentThread.ManagedThreadId != thread_id)
66                                         completed_error = true;
67                         };
68
69                         MessagePumpSyncContext.Run (async () => {
70                                 var url = Assembly.GetExecutingAssembly ().CodeBase;
71                                 await wc.DownloadDataTaskAsync (url);
72                                 Assert.AreEqual (Thread.CurrentThread.ManagedThreadId, thread_id);
73                         }, () => progress_changed && completed, 10000);
74
75                         Assert.IsTrue (progress_changed, "#1");
76                         Assert.IsFalse (progress_changed_error, "#2");
77                         Assert.IsTrue (completed, "#3");
78                         Assert.IsFalse (completed_error, "#4");
79                 }
80
81                 [Test]
82                 [Category("InetAccess")]
83                 public void DownloadFileTaskAsync ()
84                 {
85                         WebClient wc = new WebClient ();
86                         string filename = Path.GetTempFileName ();
87
88                         var task = wc.DownloadFileTaskAsync ("http://www.mono-project.com/", filename);
89                         Assert.IsTrue (task.Wait (15000));
90                         Assert.IsTrue (task.IsCompleted);
91                         
92                         File.Delete (filename);
93                 }
94
95                 [Test]
96                 [Category("InetAccess")]
97                 [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
98                 public void Cancellation ()
99                 {
100                         WebClient wc = new WebClient ();
101                         var progress = new ManualResetEvent (false);
102                         wc.DownloadProgressChanged += delegate {
103                                 progress.Set ();
104                         };
105
106                         // Try downloading some large file, so we don't finish early.
107                         var url = "http://download.mono-project.com/archive/2.10.9/macos-10-x86/11/MonoFramework-MDK-2.10.9_11.macos10.xamarin.x86.dmg";
108                         var task = wc.DownloadDataTaskAsync (url);
109                         Assert.IsTrue (progress.WaitOne (15000), "#1");
110                         wc.CancelAsync ();
111
112                         try {
113                                 task.Wait ();
114                                 Assert.Fail ("#2");
115                         } catch (Exception ex) {
116                                 if (ex is AggregateException)
117                                         ex = ((AggregateException)ex).InnerException;
118                                 Assert.That (ex is WebException || ex is OperationCanceledException, "#4");
119                                 Assert.IsTrue (task.IsCanceled || task.IsFaulted, "#5");
120                         }
121                 }
122
123                 [Test]
124                 [Category("InetAccess")]
125                 [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
126                 public void DownloadMultiple ()
127                 {
128                         WebClient wc = new WebClient ();
129                         var t1 = wc.OpenReadTaskAsync ("http://www.google.com/");
130                         Assert.That (t1.Wait (15000));
131                         Assert.IsTrue (t1.IsCompleted, "#1");
132
133                         var t2 = wc.OpenReadTaskAsync ("http://www.mono-project.com/");
134                         Assert.That (t2.Wait (15000));
135                         Assert.IsTrue (t2.IsCompleted, "#2");
136
137                         var t3 = wc.DownloadStringTaskAsync ("http://www.google.com/");
138                         Assert.That (t3.Wait (15000));
139                         Assert.IsTrue (t3.IsCompleted, "#3");
140                 }
141
142                 [Test]
143                 [Category("InetAccess")]
144                 [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
145                 public void DownloadMultiple2 ()
146                 {
147                         WebClient wc = new WebClient ();
148
149                         MessagePumpSyncContext.Run (async () => {
150                                 await wc.DownloadStringTaskAsync ("http://www.google.com/");
151                                 await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
152                         }, null, 15000);
153                 }
154
155                 [Test]
156                 [Category("InetAccess")]
157                 [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
158                 public void DownloadMultiple3 ()
159                 {
160                         WebClient wc = new WebClient ();
161                         int thread_id = Thread.CurrentThread.ManagedThreadId;
162                         bool data_completed = false;
163                         bool string_completed = false;
164                         bool error = false;
165
166                         wc.DownloadDataCompleted += delegate {
167                                 if (data_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
168                                         error = true;
169                                 data_completed = true;
170                         };
171                         wc.DownloadStringCompleted += delegate {
172                                 if (string_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
173                                         error = true;
174                                 string_completed = true;
175                         };
176
177                         MessagePumpSyncContext.Run (async () => {
178                                 await wc.DownloadStringTaskAsync ("http://www.google.com/");
179                                 await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
180                         }, () => data_completed && string_completed, 15000);
181
182                         Assert.IsTrue (data_completed, "#1");
183                         Assert.IsTrue (string_completed, "#2");
184                         Assert.IsFalse (error, "#3");
185                 }
186
187                 public sealed class MessagePumpSyncContext : SynchronizationContext
188                 {
189                         private delegate void MyAction ();
190
191                         private readonly Queue<MyAction> queue = new Queue<MyAction> ();
192                         private readonly object sync = new object ();
193                         private readonly Func<bool> completed;
194                         private readonly int timeout;
195                         private bool running = true;
196
197                         MessagePumpSyncContext (Func<bool> completed, int timeout)
198                         {
199                                 this.completed = completed;
200                                 this.timeout = timeout;
201                         }
202
203                         public override void Send (SendOrPostCallback d, object state)
204                         {
205                                 throw new InvalidOperationException ();
206                         }
207
208                         public override void Post (SendOrPostCallback d, object state)
209                         {
210                                 lock (sync) {
211                                         queue.Enqueue (() => d (state));
212                                         Monitor.Pulse (sync);
213                                 }
214                         }
215
216                         bool IsCompleted {
217                                 get {
218                                         if (running)
219                                                 return false;
220                                         if (completed != null)
221                                                 return completed ();
222                                         return true;
223                                 }
224                         }
225
226                         void RunMessagePump ()
227                         {
228                                 while (running) {
229                                         MyAction action;
230                                         lock (sync) {
231                                                 while (queue.Count == 0) {
232                                                         if (IsCompleted)
233                                                                 return;
234                                                         if (!Monitor.Wait (sync, timeout))
235                                                                 throw new TimeoutException ();
236                                                 }
237                                                 action = queue.Dequeue ();
238                                         }
239                                         action ();
240                                 }
241                         }
242
243                         public void Cancel ()
244                         {
245                                 lock (sync) {
246                                         running = false;
247                                         Monitor.Pulse (sync);
248                                 }
249                         }
250
251                         public static void Run (Func<Task> action, Func<bool> completed, int timeout)
252                         {
253                                 var old_ctx = SynchronizationContext.Current;
254
255                                 var ctx = new MessagePumpSyncContext (completed, timeout);
256                                 try {
257                                         SynchronizationContext.SetSynchronizationContext (ctx);
258
259                                         var thread_id = Thread.CurrentThread.ManagedThreadId;
260
261                                         var task = action ();
262                                         task.ContinueWith ((t) => {
263                                                 ctx.running = false;
264                                         }, TaskScheduler.FromCurrentSynchronizationContext ());
265
266                                         ctx.RunMessagePump ();
267
268                                         if (task.IsFaulted)
269                                                 throw task.Exception;
270                                 } finally {
271                                         SynchronizationContext.SetSynchronizationContext (old_ctx);
272                                 }
273                         }
274
275                 }
276         }
277 }