WebClient progress monitoring uses ContentLength header (dotnet/corefx#26462)
authorDouglas-Cleghorn <douglas@readinghorizons.com>
Thu, 1 Feb 2018 15:41:48 +0000 (08:41 -0700)
committerStephen Toub <stoub@microsoft.com>
Thu, 1 Feb 2018 15:41:48 +0000 (10:41 -0500)
* WebClient progress monitoring uses ContentLength header

* Added test for download progress

* Fixed build issue

* Set contentLength to zero if the header is zero

* Fixing WebClient async tests

Commit migrated from https://github.com/dotnet/corefx/commit/3c4115d601335a61d9d170a71bdfb71eb2177beb

src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs
src/libraries/System.Net.WebClient/tests/System.Net.WebClient.Tests.csproj
src/libraries/System.Net.WebClient/tests/WebClientTest.cs

index 68cf3bd..364046b 100644 (file)
@@ -875,6 +875,11 @@ namespace System.Net
                     }
                     writeStream.SetLength(copyBuffer.Length);
                 }
+                
+                if (contentLength >= 0)
+                {
+                    _progress.TotalBytesToReceive = contentLength;
+                }
 
                 using (writeStream)
                 using (Stream readStream = response.GetResponseStream())
index 7c5b8ce..37e1c73 100644 (file)
@@ -20,6 +20,9 @@
     <Compile Include="$(CommonTestPath)\System\Net\Http\LoopbackServer.cs">
       <Link>Common\System\Net\Http\LoopbackServer.cs</Link>
     </Compile>
+       <Compile Include="$(CommonTestPath)\System\Threading\Tasks\TaskTimeoutExtensions.cs">
+       <Link>Common\System\Threading\Tasks\TaskTimeoutExtensions.cs</Link>
+    </Compile>
   </ItemGroup>
   <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
 </Project>
\ No newline at end of file
index 64efdb0..575f7de 100644 (file)
@@ -476,6 +476,8 @@ namespace System.Net.Tests
 
     public abstract class WebClientTestBase
     {
+        public const int TimeoutMilliseconds = 30 * 1000;
+
         public static readonly object[][] EchoServers = System.Net.Test.Common.Configuration.Http.EchoServers;
         const string ExpectedText =
             "To be, or not to be, that is the question:" +
@@ -548,7 +550,11 @@ namespace System.Net.Tests
                         "\r\n" +
                         $"{ExpectedText}");
                 Assert.Equal(ExpectedText, Encoding.ASCII.GetString(await download));
-                Assert.True(!IsAsync || await downloadProgressInvoked.Task, "Expected download progress callback to be invoked");
+
+                if (IsAsync)
+                {
+                    await downloadProgressInvoked.Task.TimeoutAfter(TimeoutMilliseconds);
+                }
             });
         }
 
@@ -559,7 +565,16 @@ namespace System.Net.Tests
             {
                 string largeText = GetRandomText(1024 * 1024);
 
+                var downloadProgressInvokedWithContentLength = new TaskCompletionSource<bool>();
                 var wc = new WebClient();
+                wc.DownloadProgressChanged += (s, e) =>
+                {
+                    if (e.TotalBytesToReceive == largeText.Length && e.BytesReceived < e.TotalBytesToReceive)
+                    {
+                        downloadProgressInvokedWithContentLength.TrySetResult(true);
+                    }
+                };
+
                 Task<byte[]> download = DownloadDataAsync(wc, url.ToString());
                 await LoopbackServer.ReadRequestAndSendResponseAsync(server,
                         "HTTP/1.1 200 OK\r\n" +
@@ -568,6 +583,11 @@ namespace System.Net.Tests
                         "\r\n" +
                         $"{largeText}");
                 Assert.Equal(largeText, Encoding.ASCII.GetString(await download));
+
+                if (IsAsync)
+                {
+                    await downloadProgressInvokedWithContentLength.Task.TimeoutAfter(TimeoutMilliseconds);
+                }
             });
         }
 
@@ -644,7 +664,10 @@ namespace System.Net.Tests
 
             byte[] result = await UploadDataAsync(wc, echoServer.ToString(), Encoding.UTF8.GetBytes(ExpectedText));
             Assert.Contains(ExpectedText, Encoding.UTF8.GetString(result));
-            Assert.True(!IsAsync || await uploadProgressInvoked.Task, "Expected upload progress callback to be invoked");
+            if(IsAsync)
+            {
+                await uploadProgressInvoked.Task.TimeoutAfter(TimeoutMilliseconds);
+            }
         }
 
         [OuterLoop("Networking test talking to remote server: issue #11345")]