Merge pull request #1079 from esdrubal/webclient_cancel
authorAlex Rønne Petersen <alex@lycus.org>
Tue, 3 Jun 2014 18:27:58 +0000 (20:27 +0200)
committerAlex Rønne Petersen <alex@lycus.org>
Tue, 3 Jun 2014 18:27:58 +0000 (20:27 +0200)
UploadStringAsync cancel

mcs/class/System/System.Net/WebClient.cs
mcs/class/System/Test/System.Net/WebClientTest.cs

index 7ee338a0a266516d4ed107fcff2920b4d9cd99bd..97f9759dcc32f9675611404754a0cf6fc02eadcf 100644 (file)
@@ -1405,10 +1405,12 @@ namespace System.Net
                                                string data2 = UploadString ((Uri) args [0], (string) args [1], (string) args [2]);
                                                OnUploadStringCompleted (
                                                        new UploadStringCompletedEventArgs (data2, null, false, args [3]));
-                                       } catch (ThreadInterruptedException){
-                                               OnUploadStringCompleted (
-                                                       new UploadStringCompletedEventArgs (null, null, true, args [3]));
                                        } catch (Exception e){
+                                               if (e is ThreadInterruptedException || e.InnerException is ThreadInterruptedException) {
+                                                       OnUploadStringCompleted (
+                                                               new UploadStringCompletedEventArgs (null, null, true, args [3]));
+                                                       return;
+                                               }
                                                OnUploadStringCompleted (
                                                        new UploadStringCompletedEventArgs (null, e, false, args [3]));
                                        }});
index 3505926f41074e31c559b69c2187940f9fd475ef..e39ca906b2fe80c263b62c33bbcf770b007211e4 100644 (file)
@@ -2156,5 +2156,93 @@ namespace MonoTests.System.Net
                        // and return the same instance as WebRequest.DefaultWebProxy
                        Assert.AreSame (wc.Proxy, WebRequest.DefaultWebProxy);
                }
+               
+               [Test]
+               public void UploadStringAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+
+                               webClient.UploadStringCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadStringAsync (uri, "PUT", "text");
+                       });
+               }
+
+               [Test]
+               public void UploadDataAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+                               webClient.UploadDataCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadDataAsync (uri, "PUT", new byte[] { });
+                       });
+               }
+               
+               [Test]
+               public void UploadValuesAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+                               webClient.UploadValuesCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadValuesAsync (uri, "PUT", new NameValueCollection ());
+                       });
+               }
+
+               [Test]
+               public void UploadFileAsyncCancelEvent ()
+               {
+                       UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+                       {
+                               string tempFile = Path.Combine (_tempFolder, "upload.tmp");
+                               File.Create (tempFile).Close ();
+
+                               webClient.UploadFileCompleted += (sender, args) =>
+                               {
+                                       if (args.Cancelled)
+                                               cancelEvent.Set ();
+                               };
+
+                               webClient.UploadFileAsync (uri, "PUT", tempFile);
+                       });
+               }
+
+               public void UploadAsyncCancelEventTest (Action<WebClient, Uri, EventWaitHandle> uploadAction)
+               {
+                       var ep = new IPEndPoint (IPAddress.Loopback, 8000);
+                       string url = "http://" + IPAddress.Loopback + ":8000/test/";
+
+                       using (var responder = new SocketResponder (ep, EchoRequestHandler))
+                       {
+                               responder.Start ();
+
+                               var webClient = new WebClient ();
+
+                               var cancellationTokenSource = new CancellationTokenSource ();
+                               cancellationTokenSource.Token.Register (webClient.CancelAsync);
+
+                               var cancelEvent = new ManualResetEvent (false);
+
+                               uploadAction.Invoke (webClient, new Uri (url), cancelEvent);
+
+                               cancellationTokenSource.Cancel ();
+
+                               Assert.IsTrue (cancelEvent.WaitOne (1000));
+                       }
+               }
        }
 }