From ba0b09a3ff01d84686f77d0f55688a3ac4ed088c Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Mon, 17 Jan 2022 15:05:57 +0000 Subject: [PATCH] Fix bug in HttpClient progress reporting --- src/Squirrel/FileDownloader.cs | 12 +++++++++++- test/UtilityTests.cs | 25 ++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Squirrel/FileDownloader.cs b/src/Squirrel/FileDownloader.cs index e0a708ac..68d87a97 100644 --- a/src/Squirrel/FileDownloader.cs +++ b/src/Squirrel/FileDownloader.cs @@ -111,12 +111,22 @@ namespace Squirrel var buffer = new byte[81920]; long totalBytesRead = 0; int bytesRead; + int lastProgress = 0; while ((bytesRead = await download.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) { await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); totalBytesRead += bytesRead; + // Convert absolute progress (bytes downloaded) into relative progress (0% - 100%) - progress((int) (totalBytesRead / contentLength.Value)); + // and don't report progress < 3% difference, kind of like a shitty debounce. + var curProgress = (int) ((double) totalBytesRead / contentLength.Value * 100); + if (curProgress - lastProgress >= 3) { + lastProgress = curProgress; + progress(curProgress); + } } + + if (lastProgress != 100) + progress(100); } /// diff --git a/test/UtilityTests.cs b/test/UtilityTests.cs index c2185eab..fa83eb86 100644 --- a/test/UtilityTests.cs +++ b/test/UtilityTests.cs @@ -9,6 +9,7 @@ using Squirrel; using Squirrel.Tests.TestHelpers; using Xunit; using Squirrel.Shell; +using System.Collections.Generic; namespace Squirrel.Tests { @@ -92,7 +93,7 @@ namespace Squirrel.Tests var emptyString = string.Empty; string nullString = null; - byte[] nullByteArray = {}; + byte[] nullByteArray = { }; Assert.Equal(string.Empty, Utility.RemoveByteOrderMarkerIfPresent(emptyString)); Assert.Equal(string.Empty, Utility.RemoveByteOrderMarkerIfPresent(nullString)); Assert.Equal(string.Empty, Utility.RemoveByteOrderMarkerIfPresent(nullByteArray)); @@ -182,8 +183,6 @@ namespace Squirrel.Tests Assert.Equal(result, Utility.IsFileTopLevelInPackage(input, packagePath)); } - - [Fact] public void WeCanFetchAllProcesses() { @@ -192,6 +191,23 @@ namespace Squirrel.Tests Assert.True(result.Count != 2048); } + [Fact(Skip = "Only really need to run this test after changes to FileDownloader")] + public void DownloaderReportsProgress() + { + // this probably should use a local http server instead. + const string testUrl = "http://speedtest.tele2.net/1MB.zip"; + + var dl = Utility.CreateDefaultDownloader(); + + List prog = new List(); + using (Utility.WithTempFile(out var tempPath)) + dl.DownloadFile(testUrl, tempPath, prog.Add).Wait(); + + Assert.True(prog.Count > 10); + Assert.Equal(100, prog.Last()); + Assert.True(prog[1] != 0); + } + static void CreateSampleDirectory(string directory) { Random prng = new Random(); @@ -237,8 +253,7 @@ namespace Squirrel.Tests { var rv = new byte[arrays.Sum(a => a.Length)]; var offset = 0; - foreach (var array in arrays) - { + foreach (var array in arrays) { Buffer.BlockCopy(array, 0, rv, offset, array.Length); offset += array.Length; }