From a47e8e9fe591242b0d8aeada9c8b2e9e2a9694a6 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sat, 12 Oct 2024 22:30:34 +0300 Subject: [PATCH] Fixes file system browser flaky unit tests --- .../FileManagement/DrivesControllerTests.cs | 17 ++- .../FileSystemControllerTests.cs | 17 ++- .../FileManagement/PathControllerTests.cs | 119 +++++++++++++----- .../ThumbnailsControllerTests.cs | 17 ++- 4 files changed, 130 insertions(+), 40 deletions(-) diff --git a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/DrivesControllerTests.cs b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/DrivesControllerTests.cs index f2e919f8..d7dc628a 100644 --- a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/DrivesControllerTests.cs +++ b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/DrivesControllerTests.cs @@ -125,16 +125,25 @@ public class DrivesControllerTests { // Arrange CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask>>(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return ErrorOrFactory.From(_fileSystemTreeNodeResponseFixture.CreateMany(3, 1, 1).AsEnumerable()); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.GetDrives(cts.Token)); + // Act + Task operationTask = _sut.GetDrives(cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } } diff --git a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/FileSystemControllerTests.cs b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/FileSystemControllerTests.cs index 0e593b14..ddbef04a 100644 --- a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/FileSystemControllerTests.cs +++ b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/FileSystemControllerTests.cs @@ -91,16 +91,25 @@ public class FileSystemControllerTests { // Arrange CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return new FileSystemTypeResponse(PlatformType.Windows); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.GetType(cts.Token)); + // Act + Task operationTask = _sut.GetType(cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } } diff --git a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs index 470a529b..a751ce21 100644 --- a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs +++ b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs @@ -148,17 +148,26 @@ public class PathControllerTests // Arrange string path = @"C:\Users\TestUser"; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask>(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return ErrorOrFactory.From(_pathSegmentResponseFixture.Create()); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.GetPathRoot(path, cts.Token)); + // Act + Task operationTask = _sut.GetPathRoot(path, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } [Fact] @@ -199,17 +208,26 @@ public class PathControllerTests { // Arrange CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return _pathSeparatorResponseFixture.Create(); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.GetPathSeparator(cts.Token)); + // Act + Task operationTask = _sut.GetPathSeparator(cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } [Fact] @@ -314,20 +332,29 @@ public class PathControllerTests // Arrange string path = @"C:\Users\TestUser\Documents"; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask>>(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return ErrorOrFactory.From(new List { _pathSegmentResponseFixture.Create() }.AsEnumerable()); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.GetPathParent(path, cts.Token)); + // Act + Task operationTask = _sut.GetPathParent(path, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } [Fact] @@ -432,17 +459,26 @@ public class PathControllerTests string originalPath = @"C:\Users"; string newPath = "TestUser"; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask>(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return ErrorOrFactory.From(_pathSegmentResponseFixture.Create()); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.CombinePath(originalPath, newPath, cts.Token)); + // Act + Task operationTask = _sut.CombinePath(originalPath, newPath, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } [Fact] @@ -548,20 +584,29 @@ public class PathControllerTests // Arrange string path = @"C:\Users\TestUser\Documents"; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask>>(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return ErrorOrFactory.From(new List { _pathSegmentResponseFixture.Create() }.AsEnumerable()); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.SplitPath(path, cts.Token)); + // Act + Task operationTask = _sut.SplitPath(path, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } [Fact] @@ -605,17 +650,26 @@ public class PathControllerTests // Arrange string path = @"C:\Users\TestUser\Documents"; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return new PathValidResponse(true); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.ValidatePath(path, cts.Token)); + // Act + Task operationTask = _sut.ValidatePath(path, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } [Fact] @@ -659,16 +713,25 @@ public class PathControllerTests // Arrange string path = @"C:\Users\TestUser\Documents"; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return new PathExistsResponse(true); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.CheckPathExists(path, true, cts.Token)); + // Act + Task operationTask = _sut.CheckPathExists(path, true, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } } diff --git a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/ThumbnailsControllerTests.cs b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/ThumbnailsControllerTests.cs index a1a25ae7..07e9e129 100644 --- a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/ThumbnailsControllerTests.cs +++ b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/ThumbnailsControllerTests.cs @@ -133,16 +133,25 @@ public class ThumbnailsControllerTests string path = "/path/to/file.jpg"; int quality = 80; CancellationTokenSource cts = new(); + TaskCompletionSource operationStarted = new(); + TaskCompletionSource cancellationRequested = new(); _mockMediator.Send(Arg.Any(), Arg.Any()) .Returns(callInfo => new ValueTask>(Task.Run(async () => { - await Task.Delay(100, callInfo.Arg()); + operationStarted.SetResult(true); + await cancellationRequested.Task; + callInfo.Arg().ThrowIfCancellationRequested(); return ErrorOrFactory.From(ThumbnailResponseFixture.CreateThumbnailResponse()); }, callInfo.Arg()))); - // Act & Assert - cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.GetThumbnail(path, quality, cts.Token)); + // Act + Task operationTask = _sut.GetThumbnail(path, quality, cts.Token); + await operationStarted.Task; + cts.Cancel(); + cancellationRequested.SetResult(true); + + // Assert + await Assert.ThrowsAnyAsync(() => operationTask); } }