WebClientTestAsync.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // System.Net.WebClientTestAsync
  3. //
  4. // Authors:
  5. // Martin Baulig ([email protected])
  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. namespace MonoTests.System.Net
  39. {
  40. [TestFixture]
  41. public class WebClientTestAsync
  42. {
  43. [Test]
  44. [Category("Async")]
  45. public void DownloadData ()
  46. {
  47. WebClient wc;
  48. bool progress_changed = false;
  49. bool completed = false;
  50. bool progress_changed_error = false;
  51. bool completed_error = false;
  52. int thread_id = Thread.CurrentThread.ManagedThreadId;
  53. wc = new WebClient ();
  54. wc.DownloadProgressChanged += delegate {
  55. progress_changed = true;
  56. if (Thread.CurrentThread.ManagedThreadId != thread_id)
  57. progress_changed_error = true;
  58. };
  59. wc.DownloadDataCompleted += delegate {
  60. completed = true;
  61. if (Thread.CurrentThread.ManagedThreadId != thread_id)
  62. completed_error = true;
  63. };
  64. MessagePumpSyncContext.Run (async () => {
  65. var url = Assembly.GetExecutingAssembly ().CodeBase;
  66. await wc.DownloadDataTaskAsync (url);
  67. Assert.AreEqual (Thread.CurrentThread.ManagedThreadId, thread_id);
  68. }, () => progress_changed && completed, 10000);
  69. Assert.IsTrue (progress_changed, "#1");
  70. Assert.IsFalse (progress_changed_error, "#2");
  71. Assert.IsTrue (completed, "#3");
  72. Assert.IsFalse (completed_error, "#4");
  73. }
  74. [Test]
  75. [Category("InetAccess")]
  76. public void DownloadFileTaskAsync ()
  77. {
  78. WebClient wc = new WebClient ();
  79. string filename = Path.GetTempFileName ();
  80. var task = wc.DownloadFileTaskAsync ("http://www.mono-project.com/", filename);
  81. Assert.IsTrue (task.Wait (15000));
  82. Assert.IsTrue (task.IsCompleted);
  83. File.Delete (filename);
  84. }
  85. [Test]
  86. [Category("InetAccess")]
  87. public void Cancellation ()
  88. {
  89. WebClient wc = new WebClient ();
  90. var progress = new ManualResetEvent (false);
  91. wc.DownloadProgressChanged += delegate {
  92. progress.Set ();
  93. };
  94. // Try downloading some large file, so we don't finish early.
  95. 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";
  96. var task = wc.DownloadDataTaskAsync (url);
  97. Assert.IsTrue (progress.WaitOne (15000), "#1");
  98. wc.CancelAsync ();
  99. try {
  100. task.Wait ();
  101. Assert.Fail ("#2");
  102. } catch (Exception ex) {
  103. if (ex is AggregateException)
  104. ex = ((AggregateException)ex).InnerException;
  105. Assert.That (ex is WebException || ex is OperationCanceledException, "#4");
  106. Assert.IsTrue (task.IsCanceled || task.IsFaulted, "#5");
  107. }
  108. }
  109. [Test]
  110. [Category("InetAccess")]
  111. public void DownloadMultiple ()
  112. {
  113. WebClient wc = new WebClient ();
  114. var t1 = wc.OpenReadTaskAsync ("http://www.google.com/");
  115. Assert.That (t1.Wait (15000));
  116. Assert.IsTrue (t1.IsCompleted, "#1");
  117. var t2 = wc.OpenReadTaskAsync ("http://www.mono-project.com/");
  118. Assert.That (t2.Wait (15000));
  119. Assert.IsTrue (t2.IsCompleted, "#2");
  120. var t3 = wc.DownloadStringTaskAsync ("http://www.google.com/");
  121. Assert.That (t3.Wait (15000));
  122. Assert.IsTrue (t3.IsCompleted, "#3");
  123. }
  124. [Test]
  125. [Category("InetAccess")]
  126. public void DownloadMultiple2 ()
  127. {
  128. WebClient wc = new WebClient ();
  129. MessagePumpSyncContext.Run (async () => {
  130. await wc.DownloadStringTaskAsync ("http://www.google.com/");
  131. await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
  132. }, null, 15000);
  133. }
  134. [Test]
  135. [Category("InetAccess")]
  136. public void DownloadMultiple3 ()
  137. {
  138. WebClient wc = new WebClient ();
  139. int thread_id = Thread.CurrentThread.ManagedThreadId;
  140. bool data_completed = false;
  141. bool string_completed = false;
  142. bool error = false;
  143. wc.DownloadDataCompleted += delegate {
  144. if (data_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
  145. error = true;
  146. data_completed = true;
  147. };
  148. wc.DownloadStringCompleted += delegate {
  149. if (string_completed || (Thread.CurrentThread.ManagedThreadId != thread_id))
  150. error = true;
  151. string_completed = true;
  152. };
  153. MessagePumpSyncContext.Run (async () => {
  154. await wc.DownloadStringTaskAsync ("http://www.google.com/");
  155. await wc.DownloadDataTaskAsync ("http://www.mono-project.com/");
  156. }, () => data_completed && string_completed, 15000);
  157. Assert.IsTrue (data_completed, "#1");
  158. Assert.IsTrue (string_completed, "#2");
  159. Assert.IsFalse (error, "#3");
  160. }
  161. public sealed class MessagePumpSyncContext : SynchronizationContext
  162. {
  163. private delegate void MyAction ();
  164. private readonly Queue<MyAction> queue = new Queue<MyAction> ();
  165. private readonly object sync = new object ();
  166. private readonly Func<bool> completed;
  167. private readonly int timeout;
  168. private bool running = true;
  169. MessagePumpSyncContext (Func<bool> completed, int timeout)
  170. {
  171. this.completed = completed;
  172. this.timeout = timeout;
  173. }
  174. public override void Send (SendOrPostCallback d, object state)
  175. {
  176. throw new InvalidOperationException ();
  177. }
  178. public override void Post (SendOrPostCallback d, object state)
  179. {
  180. lock (sync) {
  181. queue.Enqueue (() => d (state));
  182. Monitor.Pulse (sync);
  183. }
  184. }
  185. bool IsCompleted {
  186. get {
  187. if (running)
  188. return false;
  189. if (completed != null)
  190. return completed ();
  191. return true;
  192. }
  193. }
  194. void RunMessagePump ()
  195. {
  196. while (running) {
  197. MyAction action;
  198. lock (sync) {
  199. while (queue.Count == 0) {
  200. if (IsCompleted)
  201. return;
  202. if (!Monitor.Wait (sync, timeout))
  203. throw new TimeoutException ();
  204. }
  205. action = queue.Dequeue ();
  206. }
  207. action ();
  208. }
  209. }
  210. public void Cancel ()
  211. {
  212. lock (sync) {
  213. running = false;
  214. Monitor.Pulse (sync);
  215. }
  216. }
  217. public static void Run (Func<Task> action, Func<bool> completed, int timeout)
  218. {
  219. var old_ctx = SynchronizationContext.Current;
  220. var ctx = new MessagePumpSyncContext (completed, timeout);
  221. try {
  222. SynchronizationContext.SetSynchronizationContext (ctx);
  223. var thread_id = Thread.CurrentThread.ManagedThreadId;
  224. var task = action ();
  225. task.ContinueWith ((t) => {
  226. ctx.running = false;
  227. }, TaskScheduler.FromCurrentSynchronizationContext ());
  228. ctx.RunMessagePump ();
  229. if (task.IsFaulted)
  230. throw task.Exception;
  231. } finally {
  232. SynchronizationContext.SetSynchronizationContext (old_ctx);
  233. }
  234. }
  235. }
  236. }
  237. }
  238. #endif