2 // System.Net.WebClientTestAsync
5 // Martin Baulig (martin.baulig@googlemail.com)
7 // Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
33 using System.Threading;
34 using System.Threading.Tasks;
35 using System.Reflection;
37 using NUnit.Framework;
39 namespace MonoTests.System.Net
42 public class WebClientTestAsync
46 [Category("AndroidNotWorking")] // Attempts to access the test dll which won't work on Android
47 public void DownloadData ()
50 bool progress_changed = false;
51 bool completed = false;
52 bool progress_changed_error = false;
53 bool completed_error = false;
55 int thread_id = Thread.CurrentThread.ManagedThreadId;
57 wc = new WebClient ();
59 wc.DownloadProgressChanged += delegate {
60 progress_changed = true;
61 if (Thread.CurrentThread.ManagedThreadId != thread_id)
62 progress_changed_error = true;
64 wc.DownloadDataCompleted += delegate {
66 if (Thread.CurrentThread.ManagedThreadId != thread_id)
67 completed_error = true;
70 MessagePumpSyncContext.Run (async () => {
71 var url = Assembly.GetExecutingAssembly ().CodeBase;
72 await wc.DownloadDataTaskAsync (url);
73 Assert.AreEqual (Thread.CurrentThread.ManagedThreadId, thread_id);
74 }, () => progress_changed && completed, 10000);
76 Assert.IsTrue (progress_changed, "#1");
77 Assert.IsFalse (progress_changed_error, "#2");
78 Assert.IsTrue (completed, "#3");
79 Assert.IsFalse (completed_error, "#4");
83 [Category("InetAccess")]
84 public void DownloadFileTaskAsync ()
86 WebClient wc = new WebClient ();
87 string filename = Path.GetTempFileName ();
89 var task = wc.DownloadFileTaskAsync ("http://www.mono-project.com/", filename);
90 Assert.IsTrue (task.Wait (15000));
91 Assert.IsTrue (task.IsCompleted);
93 File.Delete (filename);
97 [Category("InetAccess")]
98 public void Cancellation ()
100 WebClient wc = new WebClient ();
101 var progress = new ManualResetEvent (false);
102 wc.DownloadProgressChanged += delegate {
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");
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");
124 [Category("InetAccess")]
125 public void DownloadMultiple ()
127 WebClient wc = new WebClient ();
128 var t1 = wc.OpenReadTaskAsync ("http://www.google.com/");
129 Assert.That (t1.Wait (15000));
130 Assert.IsTrue (t1.IsCompleted, "#1");
132 var t2 = wc.OpenReadTaskAsync ("http://www.mono-project.com/");
133 Assert.That (t2.Wait (15000));
134 Assert.IsTrue (t2.IsCompleted, "#2");
136 var t3 = wc.DownloadStringTaskAsync ("http://www.google.com/");
137 Assert.That (t3.Wait (15000));
138 Assert.IsTrue (t3.IsCompleted, "#3");
142 [Category("InetAccess")]
143 public void DownloadMultiple2 ()
145 WebClient wc = new WebClient ();
147 MessagePumpSyncContext.Run (async () => {
148 await wc.DownloadStringTaskAsync ("http://www.google.com/");
149 await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
154 [Category("InetAccess")]
155 public void DownloadMultiple3 ()
157 WebClient wc = new WebClient ();
158 int thread_id = Thread.CurrentThread.ManagedThreadId;
159 bool data_completed = false;
160 bool string_completed = false;
163 wc.DownloadDataCompleted += delegate {
164 if (data_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
166 data_completed = true;
168 wc.DownloadStringCompleted += delegate {
169 if (string_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
171 string_completed = true;
174 MessagePumpSyncContext.Run (async () => {
175 await wc.DownloadStringTaskAsync ("http://www.google.com/");
176 await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
177 }, () => data_completed && string_completed, 15000);
179 Assert.IsTrue (data_completed, "#1");
180 Assert.IsTrue (string_completed, "#2");
181 Assert.IsFalse (error, "#3");
184 public sealed class MessagePumpSyncContext : SynchronizationContext
186 private delegate void MyAction ();
188 private readonly Queue<MyAction> queue = new Queue<MyAction> ();
189 private readonly object sync = new object ();
190 private readonly Func<bool> completed;
191 private readonly int timeout;
192 private bool running = true;
194 MessagePumpSyncContext (Func<bool> completed, int timeout)
196 this.completed = completed;
197 this.timeout = timeout;
200 public override void Send (SendOrPostCallback d, object state)
202 throw new InvalidOperationException ();
205 public override void Post (SendOrPostCallback d, object state)
208 queue.Enqueue (() => d (state));
209 Monitor.Pulse (sync);
217 if (completed != null)
223 void RunMessagePump ()
228 while (queue.Count == 0) {
231 if (!Monitor.Wait (sync, timeout))
232 throw new TimeoutException ();
234 action = queue.Dequeue ();
240 public void Cancel ()
244 Monitor.Pulse (sync);
248 public static void Run (Func<Task> action, Func<bool> completed, int timeout)
250 var old_ctx = SynchronizationContext.Current;
252 var ctx = new MessagePumpSyncContext (completed, timeout);
254 SynchronizationContext.SetSynchronizationContext (ctx);
256 var thread_id = Thread.CurrentThread.ManagedThreadId;
258 var task = action ();
259 task.ContinueWith ((t) => {
261 }, TaskScheduler.FromCurrentSynchronizationContext ());
263 ctx.RunMessagePump ();
266 throw task.Exception;
268 SynchronizationContext.SetSynchronizationContext (old_ctx);