b23bb098217a6290775e0fe948805ca33d8d0999
[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 #if NET_4_5
30 using System;
31 using System.IO;
32 using System.Collections.Generic;
33 using System.Threading;
34 using System.Threading.Tasks;
35 using System.Reflection;
36 using System.Net;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Net
40 {
41         [TestFixture]
42         public class WebClientTestAsync
43         {
44                 [Test]
45                 [Category("Async")]
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                 public void Cancellation ()
98                 {
99                         WebClient wc = new WebClient ();
100                         var progress = new ManualResetEvent (false);
101                         wc.DownloadProgressChanged += delegate {
102                                 progress.Set ();
103                         };
104
105                         // Try downloading some large file, so we don't finish early.
106                         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";
107                         var task = wc.DownloadDataTaskAsync (url);
108                         Assert.IsTrue (progress.WaitOne (15000), "#1");
109                         wc.CancelAsync ();
110
111                         try {
112                                 task.Wait ();
113                                 Assert.Fail ("#2");
114                         } catch (Exception ex) {
115                                 if (ex is AggregateException)
116                                         ex = ((AggregateException)ex).InnerException;
117                                 Assert.That (ex is WebException || ex is OperationCanceledException, "#4");
118                                 Assert.IsTrue (task.IsCanceled || task.IsFaulted, "#5");
119                         }
120                 }
121
122                 [Test]
123                 [Category("InetAccess")]
124                 public void DownloadMultiple ()
125                 {
126                         WebClient wc = new WebClient ();
127                         var t1 = wc.OpenReadTaskAsync ("http://www.google.com/");
128                         Assert.That (t1.Wait (15000));
129                         Assert.IsTrue (t1.IsCompleted, "#1");
130
131                         var t2 = wc.OpenReadTaskAsync ("http://www.mono-project.com/");
132                         Assert.That (t2.Wait (15000));
133                         Assert.IsTrue (t2.IsCompleted, "#2");
134
135                         var t3 = wc.DownloadStringTaskAsync ("http://www.google.com/");
136                         Assert.That (t3.Wait (15000));
137                         Assert.IsTrue (t3.IsCompleted, "#3");
138                 }
139
140                 [Test]
141                 [Category("InetAccess")]
142                 public void DownloadMultiple2 ()
143                 {
144                         WebClient wc = new WebClient ();
145
146                         MessagePumpSyncContext.Run (async () => {
147                                 await wc.DownloadStringTaskAsync ("http://www.google.com/");
148                                 await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
149                         }, null, 15000);
150                 }
151
152                 [Test]
153                 [Category("InetAccess")]
154                 public void DownloadMultiple3 ()
155                 {
156                         WebClient wc = new WebClient ();
157                         int thread_id = Thread.CurrentThread.ManagedThreadId;
158                         bool data_completed = false;
159                         bool string_completed = false;
160                         bool error = false;
161
162                         wc.DownloadDataCompleted += delegate {
163                                 if (data_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
164                                         error = true;
165                                 data_completed = true;
166                         };
167                         wc.DownloadStringCompleted += delegate {
168                                 if (string_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
169                                         error = true;
170                                 string_completed = true;
171                         };
172
173                         MessagePumpSyncContext.Run (async () => {
174                                 await wc.DownloadStringTaskAsync ("http://www.google.com/");
175                                 await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
176                         }, () => data_completed && string_completed, 15000);
177
178                         Assert.IsTrue (data_completed, "#1");
179                         Assert.IsTrue (string_completed, "#2");
180                         Assert.IsFalse (error, "#3");
181                 }
182
183                 public sealed class MessagePumpSyncContext : SynchronizationContext
184                 {
185                         private delegate void MyAction ();
186
187                         private readonly Queue<MyAction> queue = new Queue<MyAction> ();
188                         private readonly object sync = new object ();
189                         private readonly Func<bool> completed;
190                         private readonly int timeout;
191                         private bool running = true;
192
193                         MessagePumpSyncContext (Func<bool> completed, int timeout)
194                         {
195                                 this.completed = completed;
196                                 this.timeout = timeout;
197                         }
198
199                         public override void Send (SendOrPostCallback d, object state)
200                         {
201                                 throw new InvalidOperationException ();
202                         }
203
204                         public override void Post (SendOrPostCallback d, object state)
205                         {
206                                 lock (sync) {
207                                         queue.Enqueue (() => d (state));
208                                         Monitor.Pulse (sync);
209                                 }
210                         }
211
212                         bool IsCompleted {
213                                 get {
214                                         if (running)
215                                                 return false;
216                                         if (completed != null)
217                                                 return completed ();
218                                         return true;
219                                 }
220                         }
221
222                         void RunMessagePump ()
223                         {
224                                 while (running) {
225                                         MyAction action;
226                                         lock (sync) {
227                                                 while (queue.Count == 0) {
228                                                         if (IsCompleted)
229                                                                 return;
230                                                         if (!Monitor.Wait (sync, timeout))
231                                                                 throw new TimeoutException ();
232                                                 }
233                                                 action = queue.Dequeue ();
234                                         }
235                                         action ();
236                                 }
237                         }
238
239                         public void Cancel ()
240                         {
241                                 lock (sync) {
242                                         running = false;
243                                         Monitor.Pulse (sync);
244                                 }
245                         }
246
247                         public static void Run (Func<Task> action, Func<bool> completed, int timeout)
248                         {
249                                 var old_ctx = SynchronizationContext.Current;
250
251                                 var ctx = new MessagePumpSyncContext (completed, timeout);
252                                 try {
253                                         SynchronizationContext.SetSynchronizationContext (ctx);
254
255                                         var thread_id = Thread.CurrentThread.ManagedThreadId;
256
257                                         var task = action ();
258                                         task.ContinueWith ((t) => {
259                                                 ctx.running = false;
260                                         }, TaskScheduler.FromCurrentSynchronizationContext ());
261
262                                         ctx.RunMessagePump ();
263
264                                         if (task.IsFaulted)
265                                                 throw task.Exception;
266                                 } finally {
267                                         SynchronizationContext.SetSynchronizationContext (old_ctx);
268                                 }
269                         }
270
271                 }
272         }
273 }
274 #endif