From 49da7d950070dd77b48ef7b7b022732bdf94cb8d Mon Sep 17 00:00:00 2001 From: bezzad Date: Wed, 18 Sep 2024 15:54:04 +0330 Subject: [PATCH] refactor and add [ExcludeFromCodeCoverage] attribute to up test code coverage --- .../DummyApiException.cs | 4 +- .../DummyApiExceptionFilterAttribute.cs | 2 + src/Downloader.DummyHttpServer/DummyData.cs | 2 + .../DummyFileHelper.cs | 4 +- .../DummyLazyStream.cs | 2 + src/Downloader.DummyHttpServer/HttpServer.cs | 3 +- .../MockMemoryStream.cs | 2 + src/Downloader.DummyHttpServer/Startup.cs | 2 + src/Downloader.Test/Helper/FileLogger.cs | 2 + .../DownloadIntegrationTest.cs | 78 +++++++++--------- .../IntegrationTests/DownloadServiceTest.cs | 82 +++++++++---------- .../IntegrationTests/ThrottledStreamTest.cs | 6 +- .../UnitTests/ChunkDownloaderOnFileTest.cs | 3 +- .../UnitTests/ChunkDownloaderOnMemoryTest.cs | 2 +- .../UnitTests/DownloadPackageTest.cs | 3 +- src/Downloader.Test/UnitTests/PacketTest.cs | 1 - src/Downloader/DownloadPackage.cs | 1 - src/Samples/Downloader.Sample/DownloadItem.cs | 4 +- src/Samples/Downloader.Sample/Helper.cs | 2 + src/Samples/Downloader.Sample/Program.cs | 2 + 20 files changed, 115 insertions(+), 92 deletions(-) diff --git a/src/Downloader.DummyHttpServer/DummyApiException.cs b/src/Downloader.DummyHttpServer/DummyApiException.cs index e881cf7..2904925 100644 --- a/src/Downloader.DummyHttpServer/DummyApiException.cs +++ b/src/Downloader.DummyHttpServer/DummyApiException.cs @@ -1,7 +1,9 @@ -using System.Net; +using System.Diagnostics.CodeAnalysis; +using System.Net; namespace Downloader.DummyHttpServer; +[ExcludeFromCodeCoverage] public class DummyApiException : WebException { public DummyApiException(string message) diff --git a/src/Downloader.DummyHttpServer/DummyApiExceptionFilterAttribute.cs b/src/Downloader.DummyHttpServer/DummyApiExceptionFilterAttribute.cs index ce35ade..388e897 100644 --- a/src/Downloader.DummyHttpServer/DummyApiExceptionFilterAttribute.cs +++ b/src/Downloader.DummyHttpServer/DummyApiExceptionFilterAttribute.cs @@ -1,10 +1,12 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; +using System.Diagnostics.CodeAnalysis; using System.Net; using static System.Console; namespace Downloader.DummyHttpServer; +[ExcludeFromCodeCoverage] public class DummyApiExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) diff --git a/src/Downloader.DummyHttpServer/DummyData.cs b/src/Downloader.DummyHttpServer/DummyData.cs index a248391..998cf50 100644 --- a/src/Downloader.DummyHttpServer/DummyData.cs +++ b/src/Downloader.DummyHttpServer/DummyData.cs @@ -1,10 +1,12 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace Downloader.DummyHttpServer; /// /// Class with helper methods to create random data /// +[ExcludeFromCodeCoverage] public static class DummyData { private static Random _rand = new Random(DateTime.Now.GetHashCode()); diff --git a/src/Downloader.DummyHttpServer/DummyFileHelper.cs b/src/Downloader.DummyHttpServer/DummyFileHelper.cs index ae1ab0f..3123b9e 100644 --- a/src/Downloader.DummyHttpServer/DummyFileHelper.cs +++ b/src/Downloader.DummyHttpServer/DummyFileHelper.cs @@ -1,8 +1,10 @@ -using System.IO; +using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; namespace Downloader.DummyHttpServer; +[ExcludeFromCodeCoverage] public static class DummyFileHelper { public const string TempFilesExtension = ".temp"; diff --git a/src/Downloader.DummyHttpServer/DummyLazyStream.cs b/src/Downloader.DummyHttpServer/DummyLazyStream.cs index 9b64d84..dbafa2a 100644 --- a/src/Downloader.DummyHttpServer/DummyLazyStream.cs +++ b/src/Downloader.DummyHttpServer/DummyLazyStream.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; namespace Downloader.DummyHttpServer; @@ -10,6 +11,7 @@ public enum DummyDataType Single } +[ExcludeFromCodeCoverage] public class DummyLazyStream : Stream { private readonly Random _random; diff --git a/src/Downloader.DummyHttpServer/HttpServer.cs b/src/Downloader.DummyHttpServer/HttpServer.cs index 91d86b0..7482a4a 100644 --- a/src/Downloader.DummyHttpServer/HttpServer.cs +++ b/src/Downloader.DummyHttpServer/HttpServer.cs @@ -2,14 +2,15 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.Caching.Memory; -using Microsoft.Extensions.Hosting; using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Downloader.DummyHttpServer; +[ExcludeFromCodeCoverage] public class HttpServer { private static IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions()); diff --git a/src/Downloader.DummyHttpServer/MockMemoryStream.cs b/src/Downloader.DummyHttpServer/MockMemoryStream.cs index c76148e..362f9e4 100644 --- a/src/Downloader.DummyHttpServer/MockMemoryStream.cs +++ b/src/Downloader.DummyHttpServer/MockMemoryStream.cs @@ -1,10 +1,12 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading; using System.Threading.Tasks; namespace Downloader.DummyHttpServer; +[ExcludeFromCodeCoverage] public class MockMemoryStream : MemoryStream { private readonly long _failureOffset = 0; diff --git a/src/Downloader.DummyHttpServer/Startup.cs b/src/Downloader.DummyHttpServer/Startup.cs index bd1f4ce..0ce661d 100644 --- a/src/Downloader.DummyHttpServer/Startup.cs +++ b/src/Downloader.DummyHttpServer/Startup.cs @@ -1,8 +1,10 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using System.Diagnostics.CodeAnalysis; namespace Downloader.DummyHttpServer; +[ExcludeFromCodeCoverage] internal class Startup { /// diff --git a/src/Downloader.Test/Helper/FileLogger.cs b/src/Downloader.Test/Helper/FileLogger.cs index b16c58f..11989aa 100644 --- a/src/Downloader.Test/Helper/FileLogger.cs +++ b/src/Downloader.Test/Helper/FileLogger.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.CompilerServices; using System.Threading; @@ -7,6 +8,7 @@ namespace Downloader.Extensions.Logging; +[ExcludeFromCodeCoverage] public class FileLogger : ILogger, IDisposable { private volatile bool _disposed; diff --git a/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs b/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs index d1928ec..26f282b 100644 --- a/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs +++ b/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; -using FileLogger = Downloader.Extensions.Logging.FileLogger; namespace Downloader.Test.IntegrationTests; @@ -43,7 +42,7 @@ protected void DownloadFileCompleted(object sender, System.ComponentModel.AsyncC { if (e.Error is not null) { - Output.WriteLine("Error when completed: " + e.Error.Message.ToString()); + Output.WriteLine("Error when completed: " + e.Error.Message); } } @@ -53,7 +52,7 @@ public async Task DownloadUrlWithFilenameOnMemoryTest() // arrange var downloadCompletedSuccessfully = false; var resultMessage = ""; - Downloader.DownloadFileCompleted += (s, e) => { + Downloader.DownloadFileCompleted += (_, e) => { if (e.Cancelled == false && e.Error == null) { downloadCompletedSuccessfully = true; @@ -65,7 +64,7 @@ public async Task DownloadUrlWithFilenameOnMemoryTest() }; // act - using var memoryStream = await Downloader.DownloadFileTaskAsync(URL); + await using var memoryStream = await Downloader.DownloadFileTaskAsync(URL); // assert Assert.True(downloadCompletedSuccessfully, resultMessage); @@ -85,7 +84,7 @@ public async Task DownloadAndReadFileOnDownloadFileCompletedEventTest() var destFilename = FilePath; byte[] downloadedBytes = null; var downloadCompletedSuccessfully = false; - Downloader.DownloadFileCompleted += (s, e) => { + Downloader.DownloadFileCompleted += (_, e) => { if (e.Cancelled == false && e.Error == null) { // Execute the downloaded file within completed event @@ -193,7 +192,7 @@ public async Task DownloadProgressChangedTest() // arrange var progressChangedCount = (int)Math.Ceiling((double)FileSize / Config.BufferBlockSize); var progressCounter = 0; - Downloader.DownloadProgressChanged += (s, e) => Interlocked.Increment(ref progressCounter); + Downloader.DownloadProgressChanged += (_, _) => Interlocked.Increment(ref progressCounter); // act await Downloader.DownloadFileTaskAsync(URL); @@ -215,7 +214,7 @@ public async Task StopResumeDownloadTest() var cancellationsOccurrenceCount = 0; var downloadFileExecutionCounter = 0; var downloadCompletedSuccessfully = false; - Downloader.DownloadFileCompleted += (s, e) => { + Downloader.DownloadFileCompleted += (_, e) => { if (e.Cancelled && e.Error != null) { cancellationsOccurrenceCount++; @@ -241,7 +240,7 @@ public async Task StopResumeDownloadTest() // resume download from stopped point. await Downloader.DownloadFileTaskAsync(Downloader.Package); } - var stream = File.ReadAllBytes(Downloader.Package.FileName); + var stream = await File.ReadAllBytesAsync(Downloader.Package.FileName); // assert Assert.True(File.Exists(Downloader.Package.FileName)); @@ -261,7 +260,7 @@ public async Task PauseResumeDownloadTest() var expectedPauseCount = 2; var pauseCount = 0; var downloadCompletedSuccessfully = false; - Downloader.DownloadFileCompleted += (s, e) => { + Downloader.DownloadFileCompleted += (_, e) => { if (e.Cancelled == false && e.Error is null) downloadCompletedSuccessfully = true; }; @@ -300,7 +299,9 @@ public async Task StopResumeDownloadFromLastPositionTest() var totalProgressedByteSize = 0L; var totalReceivedBytes = 0L; Config.BufferBlockSize = 1024; - Downloader.DownloadProgressChanged += (s, e) => { + Config.EnableLiveStreaming = true; + + Downloader.DownloadProgressChanged += (_, e) => { totalProgressedByteSize += e.ProgressedByteSize; totalReceivedBytes += e.ReceivedBytes.Length; if (expectedStopCount > stopCount) @@ -332,8 +333,9 @@ public async Task StopResumeDownloadOverFirstPackagePositionTest() var cancellationCount = 4; var isSavingStateOnCancel = false; var isSavingStateBeforCancel = false; - - Downloader.DownloadProgressChanged += async (s, e) => { + Config.EnableLiveStreaming = true; + + Downloader.DownloadProgressChanged += async (_, _) => { isSavingStateBeforCancel |= Downloader.Package.IsSaving; if (--cancellationCount > 0) { @@ -374,7 +376,8 @@ public async Task TestTotalReceivedBytesWhenResumeDownload() var lastProgressPercentage = 0.0; Config.BufferBlockSize = 1024; Config.ChunkCount = 1; - Downloader.DownloadProgressChanged += async (s, e) => { + Config.EnableLiveStreaming = true; + Downloader.DownloadProgressChanged += async (_, e) => { totalDownloadSize += e.ReceivedBytes.Length; lastProgressPercentage = e.ProgressPercentage; if (canStopDownload && totalDownloadSize > FileSize / 2) @@ -406,7 +409,7 @@ public async Task TestTotalReceivedBytesOnResumeDownloadWhenLostDownloadedData() var lastProgressPercentage = 0.0; Config.BufferBlockSize = 1024; Config.ChunkCount = 1; - Downloader.DownloadProgressChanged += (s, e) => { + Downloader.DownloadProgressChanged += (_, e) => { totalDownloadSize = e.ReceivedBytesSize; lastProgressPercentage = e.ProgressPercentage; if (canStopDownload && totalDownloadSize > FileSize / 2) @@ -439,7 +442,7 @@ public async Task SpeedLimitTest() Config.BufferBlockSize = 1024; Config.MaximumBytesPerSecond = 2048; // Byte/s - Downloader.DownloadProgressChanged += (s, e) => { + Downloader.DownloadProgressChanged += (_, e) => { averageSpeed = ((averageSpeed * progressCounter) + e.BytesPerSecondSpeed) / (progressCounter + 1); progressCounter++; }; @@ -457,7 +460,7 @@ public async Task DynamicSpeedLimitTest() { // arrange double upperTolerance = 1.5; // 50% upper than expected avg speed - double expectedAverageSpeed = FileSize / 32; // == (256*16 + 512*8 + 1024*4 + 2048*2) / 32 + long expectedAverageSpeed = FileSize / 32; // == (256*16 + 512*8 + 1024*4 + 2048*2) / 32 double averageSpeed = 0; var progressCounter = 0; const int oneSpeedStepSize = 4096; // FileSize / 4 @@ -465,7 +468,8 @@ public async Task DynamicSpeedLimitTest() Config.MaximumBytesPerSecond = 256; // Byte/s - Downloader.DownloadProgressChanged += (s, e) => { + Downloader.DownloadProgressChanged += (_, e) => { + // ReSharper disable once AccessToModifiedClosure averageSpeed += e.BytesPerSecondSpeed; progressCounter++; @@ -491,7 +495,7 @@ public async Task TestSizeWhenDownloadOnMemoryStream() // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); // assert Assert.Equal(FileSize, Downloader.Package.TotalFileSize); @@ -505,7 +509,7 @@ public async Task TestTypeWhenDownloadOnMemoryStream() // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); // assert Assert.True(stream is MemoryStream); @@ -515,11 +519,11 @@ public async Task TestTypeWhenDownloadOnMemoryStream() public async Task TestContentWhenDownloadOnMemoryStream() { // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); - var data = (stream as MemoryStream).ToArray(); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); + var data = (stream as MemoryStream)?.ToArray(); // assert - Assert.True(FileData.SequenceEqual(data)); + Assert.True(data != null && FileData.SequenceEqual(data)); } [Fact(Timeout = 60_000)] @@ -533,7 +537,7 @@ public async Task Download256BytesRangeOfFileTest() // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); // assert Assert.NotNull(stream); @@ -558,7 +562,7 @@ public async Task DownloadNegetiveRangeOfFileTest() // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); var bytes = ((MemoryStream)stream).ToArray(); // assert @@ -578,12 +582,12 @@ public async Task TestDownloadParallelVsHalfOfChunks() Config.ParallelCount = maxParallelCountTasks; var actualMaxParallelCountTasks = 0; - Downloader.ChunkDownloadProgressChanged += (s, e) => { + Downloader.ChunkDownloadProgressChanged += (_, e) => { actualMaxParallelCountTasks = Math.Max(actualMaxParallelCountTasks, e.ActiveChunks); }; // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); var bytes = ((MemoryStream)stream).ToArray(); // assert @@ -604,8 +608,8 @@ public async Task TestResumeImmediatelyAfterCanceling() var lastProgressPercentage = 0d; bool? stopped = null; - Downloader.DownloadFileCompleted += (s, e) => stopped ??= e.Cancelled; - Downloader.DownloadProgressChanged += (s, e) => { + Downloader.DownloadFileCompleted += (_, e) => stopped ??= e.Cancelled; + Downloader.DownloadProgressChanged += (_, e) => { if (canStopDownload && e.ProgressPercentage > 50) { canStopDownload = false; @@ -619,7 +623,7 @@ public async Task TestResumeImmediatelyAfterCanceling() // act await Downloader.DownloadFileTaskAsync(URL); - using var stream = await Downloader.DownloadFileTaskAsync(Downloader.Package); // resume + await using var stream = await Downloader.DownloadFileTaskAsync(Downloader.Package); // resume // assert Assert.True(stopped); @@ -653,7 +657,7 @@ public async Task KeepOrRemoveFileWhenDownloadFailedTest(bool clearFileAfterFail [Theory] [InlineData(true)] // Test Retry Download After Timeout [InlineData(false)] // Test Retry Download After Failure - public async Task testRetryDownloadAfterFailure(bool timeout) + public async Task TestRetryDownloadAfterFailure(bool timeout) { // arrange Exception error = null; @@ -668,7 +672,7 @@ public async Task testRetryDownloadAfterFailure(bool timeout) var url = timeout ? DummyFileHelper.GetFileWithTimeoutAfterOffset(fileSize, failureOffset) : DummyFileHelper.GetFileWithFailureAfterOffset(fileSize, failureOffset); - downloadService.DownloadFileCompleted += (s, e) => error = e.Error; + downloadService.DownloadFileCompleted += (_, e) => error = e.Error; // act var stream = await downloadService.DownloadFileTaskAsync(url); @@ -717,8 +721,8 @@ public async Task TestStopDownloadWithCancellationToken() var downloadCancelled = false; var cts = new CancellationTokenSource(); - Downloader.DownloadFileCompleted += (s, e) => downloadCancelled = e.Cancelled; - Downloader.DownloadProgressChanged += (s, e) => { + Downloader.DownloadFileCompleted += (_, e) => downloadCancelled = e.Cancelled; + Downloader.DownloadProgressChanged += (_, e) => { downloadProgress = e.ProgressPercentage; if (e.ProgressPercentage > 10) { @@ -747,7 +751,7 @@ public async Task TestResumeDownloadWithAnotherUrl() var totalDownloadSize = 0L; Config.BufferBlockSize = 1024; Config.ChunkCount = 4; - Downloader.DownloadProgressChanged += (s, e) => { + Downloader.DownloadProgressChanged += (_, e) => { totalDownloadSize = e.ReceivedBytesSize; if (canStopDownload && totalDownloadSize > FileSize / 2) { @@ -785,7 +789,7 @@ public async Task DownloadAFileFromMultipleUrlsWithMultipleChunksTest(int urlsCo .ToArray(); // act - using var stream = await Downloader.DownloadFileTaskAsync(urls); + await using var stream = await Downloader.DownloadFileTaskAsync(urls); var bytes = ((MemoryStream)stream).ToArray(); // assert @@ -838,7 +842,7 @@ public async Task DownloadBigFileOnMemory() var actualFile = DummyData.GenerateOrderedBytes(totalSize); // act - using var stream = await Downloader.DownloadFileTaskAsync(URL); + await using var stream = await Downloader.DownloadFileTaskAsync(URL); // assert Assert.Equal(totalSize, Downloader.Package.TotalFileSize); @@ -862,7 +866,7 @@ public async Task DownloadBigFileWithMemoryLimitationOnDisk() // act await Downloader.DownloadFileTaskAsync(URL, FilePath); - using var fileStream = File.Open(FilePath, FileMode.Open, FileAccess.Read); + await using var fileStream = File.Open(FilePath, FileMode.Open, FileAccess.Read); // assert Assert.Equal(totalSize, Downloader.Package.TotalFileSize); diff --git a/src/Downloader.Test/IntegrationTests/DownloadServiceTest.cs b/src/Downloader.Test/IntegrationTests/DownloadServiceTest.cs index 58ab570..b51f726 100644 --- a/src/Downloader.Test/IntegrationTests/DownloadServiceTest.cs +++ b/src/Downloader.Test/IntegrationTests/DownloadServiceTest.cs @@ -58,8 +58,8 @@ public async Task CancelAsyncTest() AsyncCompletedEventArgs eventArgs = null; string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadStarted += (s, e) => CancelAsync(); - DownloadFileCompleted += (s, e) => eventArgs = e; + DownloadStarted += (_, _) => CancelAsync(); + DownloadFileCompleted += (_, e) => eventArgs = e; // act await DownloadFileTaskAsync(address); @@ -68,7 +68,7 @@ public async Task CancelAsyncTest() Assert.True(IsCancelled); Assert.NotNull(eventArgs); Assert.True(eventArgs.Cancelled); - Assert.Equal(typeof(TaskCanceledException), eventArgs.Error.GetType()); + Assert.Equal(typeof(TaskCanceledException), eventArgs.Error?.GetType()); } [Fact] @@ -78,8 +78,8 @@ public async Task CancelTaskAsyncTest() AsyncCompletedEventArgs eventArgs = null; string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadStarted += async (s, e) => await CancelTaskAsync(); - DownloadFileCompleted += (s, e) => eventArgs = e; + DownloadStarted += async (_, _) => await CancelTaskAsync(); + DownloadFileCompleted += (_, e) => eventArgs = e; // act await DownloadFileTaskAsync(address); @@ -88,7 +88,7 @@ public async Task CancelTaskAsyncTest() Assert.True(IsCancelled); Assert.NotNull(eventArgs); Assert.True(eventArgs.Cancelled); - Assert.Equal(typeof(TaskCanceledException), eventArgs.Error.GetType()); + Assert.Equal(typeof(TaskCanceledException), eventArgs.Error?.GetType()); } [Fact(Timeout = 30_000)] @@ -100,7 +100,7 @@ public async Task CompletesWithErrorWhenBadUrlTest() Filename = Path.GetTempFileName(); Options = GetDefaultConfig(); Options.MaxTryAgainOnFailover = 0; - DownloadFileCompleted += (s, e) => { + DownloadFileCompleted += (_, e) => { onCompletionException = e.Error; }; @@ -174,7 +174,7 @@ public async Task TestPackageChunksDataAfterDispose() for (int i = 0; i < Package.Chunks.Length; i++) { var buffer = new byte[chunkSize]; - await stream.ReadAsync(buffer, 0, chunkSize); + _ = await stream.ReadAsync(buffer, 0, chunkSize); Assert.True(dummyData.SequenceEqual(buffer)); } } @@ -187,11 +187,11 @@ public async Task CancelPerformanceTest() var watch = new Stopwatch(); string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadProgressChanged += async (s, e) => { + DownloadProgressChanged += async (_, _) => { watch.Start(); await CancelTaskAsync(); }; - DownloadFileCompleted += (s, e) => eventArgs = e; + DownloadFileCompleted += (_, e) => eventArgs = e; // act await DownloadFileTaskAsync(address); @@ -213,8 +213,8 @@ public async Task ResumePerformanceTest() var isCancelled = false; string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadFileCompleted += (s, e) => eventArgs = e; - DownloadProgressChanged += async (s, e) => { + DownloadFileCompleted += (_, e) => eventArgs = e; + DownloadProgressChanged += async (_, _) => { if (isCancelled == false) { await CancelTaskAsync(); @@ -248,10 +248,10 @@ public async Task PauseResumeTest() var cancelled = false; string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadFileCompleted += (s, e) => eventArgs = e; + DownloadFileCompleted += (_, e) => eventArgs = e; // act - DownloadProgressChanged += (s, e) => { + DownloadProgressChanged += (_, _) => { Pause(); cancelled = IsCancelled; paused = IsPaused; @@ -277,10 +277,10 @@ public async Task CancelAfterPauseTest() var cancelStateAfterCancel = false; string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadFileCompleted += (s, e) => eventArgs = e; + DownloadFileCompleted += (_, e) => eventArgs = e; // act - DownloadProgressChanged += async (s, e) => { + DownloadProgressChanged += async (_, _) => { Pause(); cancelStateBeforeCancel = IsCancelled; pauseStateBeforeCancel = IsPaused; @@ -310,8 +310,8 @@ public async Task DownloadParallelNotSupportedUrlTest() AsyncCompletedEventArgs eventArgs = null; string address = DummyFileHelper.GetFileWithNoAcceptRangeUrl("test.dat", DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadFileCompleted += (s, e) => eventArgs = e; - DownloadStarted += (s, e) => { + DownloadFileCompleted += (_, e) => eventArgs = e; + DownloadStarted += (_, _) => { actualChunksCount = Package.Chunks.Length; }; @@ -340,8 +340,8 @@ public async Task ResumeNotSupportedUrlTest() var maxProgressPercentage = 0d; var address = DummyFileHelper.GetFileWithNoAcceptRangeUrl("test.dat", DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadFileCompleted += (s, e) => eventArgs = e; - DownloadProgressChanged += async (s, e) => { + DownloadFileCompleted += (_, e) => eventArgs = e; + DownloadProgressChanged += async (_, e) => { if (cancelOnProgressNo == progressCount++) { await CancelTaskAsync(); @@ -379,7 +379,7 @@ public async Task ActiveChunksTest() Options = GetDefaultConfig(); // act - DownloadProgressChanged += (s, e) => { + DownloadProgressChanged += (_, e) => { allActiveChunksCount.Add(e.ActiveChunks); }; await DownloadFileTaskAsync(address); @@ -402,7 +402,7 @@ public async Task ActiveChunksWithRangeNotSupportedUrlTest() Options = GetDefaultConfig(); // act - DownloadProgressChanged += (s, e) => { + DownloadProgressChanged += (_, e) => { allActiveChunksCount.Add(e.ActiveChunks); }; await DownloadFileTaskAsync(address); @@ -427,7 +427,7 @@ public async Task ActiveChunksAfterCancelResumeWithNotSupportedUrlTest() var cancelOnProgressNo = 6; var address = DummyFileHelper.GetFileWithNoAcceptRangeUrl("test.dat", DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - DownloadProgressChanged += async (s, e) => { + DownloadProgressChanged += async (_, e) => { allActiveChunksCount.Add(e.ActiveChunks); if (cancelOnProgressNo == progressCount++) { @@ -488,8 +488,8 @@ public async Task TestPackageStatusAfterCompletionWithSuccess() var resumeStatus = DownloadStatus.None; var completedStatus = DownloadStatus.None; - DownloadStarted += (s, e) => createdStatus = Package.Status; - DownloadProgressChanged += (s, e) => { + DownloadStarted += (_, _) => createdStatus = Package.Status; + DownloadProgressChanged += (_, e) => { runningStatus = Package.Status; if (e.ProgressPercentage > 50 && e.ProgressPercentage < 70) { @@ -499,7 +499,7 @@ public async Task TestPackageStatusAfterCompletionWithSuccess() resumeStatus = Package.Status; } }; - DownloadFileCompleted += (s, e) => completedStatus = Package.Status; + DownloadFileCompleted += (_, _) => completedStatus = Package.Status; // act await DownloadFileTaskAsync(url); @@ -526,8 +526,8 @@ public async Task TestSerializePackageAfterCancel(bool onMemory) var packageText = string.Empty; var url = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - ChunkDownloadProgressChanged += (s, e) => CancelAsync(); - DownloadFileCompleted += (s, e) => { + ChunkDownloadProgressChanged += (_, _) => CancelAsync(); + DownloadFileCompleted += (_, e) => { package = e.UserState as DownloadPackage; if (package!.Status != DownloadStatus.Completed) packageText = System.Text.Json.JsonSerializer.Serialize(package!); @@ -561,14 +561,14 @@ public async Task TestResumeFromSerializedPackage(bool onMemory) var packageText = string.Empty; var url = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb); Options = GetDefaultConfig(); - ChunkDownloadProgressChanged += async (s, e) => { + ChunkDownloadProgressChanged += async (_, _) => { if (isCancelOccurred == false) { isCancelOccurred = true; await CancelTaskAsync(); } }; - DownloadFileCompleted += (s, e) => { + DownloadFileCompleted += (_, e) => { package = e.UserState as DownloadPackage; if (package!.Status != DownloadStatus.Completed) packageText = System.Text.Json.JsonSerializer.Serialize(package!); @@ -607,8 +607,8 @@ public async Task TestPackageStatusAfterCancellation() var cancelledStatus = DownloadStatus.None; var completedStatus = DownloadStatus.None; - DownloadStarted += (s, e) => createdStatus = Package.Status; - DownloadProgressChanged += async (s, e) => { + DownloadStarted += (_, _) => createdStatus = Package.Status; + DownloadProgressChanged += async (_, e) => { runningStatus = Package.Status; if (e.ProgressPercentage > 50 && e.ProgressPercentage < 70) { @@ -616,7 +616,7 @@ public async Task TestPackageStatusAfterCancellation() cancelledStatus = Package.Status; } }; - DownloadFileCompleted += (s, e) => completedStatus = Package.Status; + DownloadFileCompleted += (_, _) => completedStatus = Package.Status; // act await DownloadFileTaskAsync(url); @@ -640,10 +640,10 @@ public async Task TestResumeDownloadImmediatelyAfterCancellationAsync() var secondStartProgressPercent = -1d; var url = DummyFileHelper.GetFileWithNameUrl(DummyFileHelper.SampleFile16KbName, DummyFileHelper.FileSize16Kb); var tcs = new TaskCompletionSource(); - DownloadFileCompleted += (s, e) => completedState = Package.Status; + DownloadFileCompleted += (_, _) => completedState = Package.Status; // act - DownloadProgressChanged += async (s, e) => { + DownloadProgressChanged += async (_, e) => { if (secondStartProgressPercent < 0) { if (checkProgress) @@ -676,10 +676,10 @@ public async Task TestStopDownloadOnClearWhenRunning() // arrange var completedState = DownloadStatus.None; var url = DummyFileHelper.GetFileWithNameUrl(DummyFileHelper.SampleFile16KbName, DummyFileHelper.FileSize16Kb); - DownloadFileCompleted += (s, e) => completedState = Package.Status; + DownloadFileCompleted += (_, _) => completedState = Package.Status; // act - DownloadProgressChanged += async (s, e) => { + DownloadProgressChanged += async (_, e) => { if (e.ProgressPercentage > 50 && e.ProgressPercentage < 60) await Clear(); }; @@ -698,11 +698,11 @@ public async Task TestStopDownloadOnClearWhenPaused() // arrange var completedState = DownloadStatus.None; var url = DummyFileHelper.GetFileWithNameUrl(DummyFileHelper.SampleFile16KbName, DummyFileHelper.FileSize16Kb); - DownloadFileCompleted += (s, e) => completedState = Package.Status; + DownloadFileCompleted += (_, _) => completedState = Package.Status; // act - DownloadProgressChanged += async (s, e) => { - if (e.ProgressPercentage > 50 && e.ProgressPercentage < 60) + DownloadProgressChanged += async (_, e) => { + if (e.ProgressPercentage is > 50 and < 60) { Pause(); await Clear(); @@ -728,7 +728,7 @@ public async Task TestMinimumSizeOfChunking() var activeChunks = 0; int? chunkCounts = null; var progressIds = new Dictionary(); - ChunkDownloadProgressChanged += (s, e) => { + ChunkDownloadProgressChanged += (_, e) => { activeChunks = Math.Max(activeChunks, e.ActiveChunks); progressIds[e.ProgressId] = true; chunkCounts ??= Package.Chunks.Length; diff --git a/src/Downloader.Test/IntegrationTests/ThrottledStreamTest.cs b/src/Downloader.Test/IntegrationTests/ThrottledStreamTest.cs index 7b0c247..21dc89e 100644 --- a/src/Downloader.Test/IntegrationTests/ThrottledStreamTest.cs +++ b/src/Downloader.Test/IntegrationTests/ThrottledStreamTest.cs @@ -31,7 +31,7 @@ public async Task TestReadStreamSpeed(int speedX, bool asAsync) var buffer = new byte[maxBytesPerSecond / 8]; var readSize = 1; var totalReadSize = 0L; - using ThrottledStream stream = new ThrottledStream(new MemoryStream(bytes), maxBytesPerSecond); + await using ThrottledStream stream = new ThrottledStream(new MemoryStream(bytes), maxBytesPerSecond); var stopWatcher = Stopwatch.StartNew(); // act @@ -86,7 +86,7 @@ public async Task TestStreamWriteSpeedAsync() var tolerance = 50; // 50 ms var expectedTime = size / bytesPerSecond * 1000; // 4000 Milliseconds var randomBytes = DummyData.GenerateRandomBytes(size); - using Stream stream = new ThrottledStream(new MemoryStream(), bytesPerSecond); + await using Stream stream = new ThrottledStream(new MemoryStream(), bytesPerSecond); var stopWatcher = Stopwatch.StartNew(); // act @@ -141,7 +141,7 @@ public void TestStreamIntegrity(int streamSize, long maximumBytesPerSecond) // act stream.Write(data, 0, data.Length); stream.Seek(0, SeekOrigin.Begin); - stream.Read(copiedData, 0, copiedData.Length); + _ = stream.Read(copiedData, 0, copiedData.Length); // assert Assert.Equal(streamSize, data.Length); diff --git a/src/Downloader.Test/UnitTests/ChunkDownloaderOnFileTest.cs b/src/Downloader.Test/UnitTests/ChunkDownloaderOnFileTest.cs index af45829..36557bb 100644 --- a/src/Downloader.Test/UnitTests/ChunkDownloaderOnFileTest.cs +++ b/src/Downloader.Test/UnitTests/ChunkDownloaderOnFileTest.cs @@ -1,5 +1,4 @@ -using Downloader.DummyHttpServer; -using System.IO; +using System.IO; namespace Downloader.Test.UnitTests; diff --git a/src/Downloader.Test/UnitTests/ChunkDownloaderOnMemoryTest.cs b/src/Downloader.Test/UnitTests/ChunkDownloaderOnMemoryTest.cs index 07be0a8..c7d0d24 100644 --- a/src/Downloader.Test/UnitTests/ChunkDownloaderOnMemoryTest.cs +++ b/src/Downloader.Test/UnitTests/ChunkDownloaderOnMemoryTest.cs @@ -10,7 +10,7 @@ public ChunkDownloaderOnMemoryTest() ParallelDownload = true, MaxTryAgainOnFailover = 100, MinimumSizeOfChunking = 16, - Timeout = 100, + Timeout = 100 }; Storage = new ConcurrentStream(null); } diff --git a/src/Downloader.Test/UnitTests/DownloadPackageTest.cs b/src/Downloader.Test/UnitTests/DownloadPackageTest.cs index b814010..7317620 100644 --- a/src/Downloader.Test/UnitTests/DownloadPackageTest.cs +++ b/src/Downloader.Test/UnitTests/DownloadPackageTest.cs @@ -1,6 +1,5 @@ using Downloader.DummyHttpServer; using Downloader.Test.Helper; -using System; using System.Linq; using System.Threading.Tasks; using Xunit; @@ -37,7 +36,7 @@ public void PackageSerializationTest() Package.Storage.Dispose(); var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject(serialized); var destData = new byte[deserialized.TotalFileSize]; - deserialized.Storage.OpenRead().Read(destData, 0, destData.Length); + _ = deserialized.Storage.OpenRead().Read(destData, 0, destData.Length); // assert AssertHelper.AreEquals(Package, deserialized); diff --git a/src/Downloader.Test/UnitTests/PacketTest.cs b/src/Downloader.Test/UnitTests/PacketTest.cs index f02c0b9..80d6c02 100644 --- a/src/Downloader.Test/UnitTests/PacketTest.cs +++ b/src/Downloader.Test/UnitTests/PacketTest.cs @@ -1,5 +1,4 @@ using Downloader.DummyHttpServer; -using System; using System.Linq; using Xunit; diff --git a/src/Downloader/DownloadPackage.cs b/src/Downloader/DownloadPackage.cs index f1e5c15..50c5c08 100644 --- a/src/Downloader/DownloadPackage.cs +++ b/src/Downloader/DownloadPackage.cs @@ -1,7 +1,6 @@ using Downloader.Extensions.Logging; using System; using System.Linq; -using System.Threading; using System.Threading.Tasks; namespace Downloader; diff --git a/src/Samples/Downloader.Sample/DownloadItem.cs b/src/Samples/Downloader.Sample/DownloadItem.cs index 99fbb11..bf0cf99 100644 --- a/src/Samples/Downloader.Sample/DownloadItem.cs +++ b/src/Samples/Downloader.Sample/DownloadItem.cs @@ -1,7 +1,9 @@ -using System.IO; +using System.Diagnostics.CodeAnalysis; +using System.IO; namespace Downloader.Sample; +[ExcludeFromCodeCoverage] public class DownloadItem { private string _folderPath; diff --git a/src/Samples/Downloader.Sample/Helper.cs b/src/Samples/Downloader.Sample/Helper.cs index 35070e6..9ee022a 100644 --- a/src/Samples/Downloader.Sample/Helper.cs +++ b/src/Samples/Downloader.Sample/Helper.cs @@ -1,7 +1,9 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace Downloader.Sample; +[ExcludeFromCodeCoverage] public static class Helper { public static string CalcMemoryMensurableUnit(this long bytes) diff --git a/src/Samples/Downloader.Sample/Program.cs b/src/Samples/Downloader.Sample/Program.cs index 15475ea..cf4be9e 100644 --- a/src/Samples/Downloader.Sample/Program.cs +++ b/src/Samples/Downloader.Sample/Program.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -13,6 +14,7 @@ namespace Downloader.Sample; +[ExcludeFromCodeCoverage] public partial class Program { private const string DownloadListFile = "download.json";