From c398a6171b6855008c1609a5cb138622ecf2ebdd Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 16 Dec 2024 10:57:44 +0100 Subject: [PATCH] Avoid using `Thread.Sleep` in tests --- .../NotificationTests.cs | 22 +++++++++---------- .../FileSystemWatcherStatisticsTests.cs | 12 +++++----- .../FileSystemWatcher/EventTests.cs | 16 +++++++------- .../FileSystem/FileSystemWatcher/Tests.cs | 8 +++---- .../FileSystemWatcher/WaitForChangedTests.cs | 8 +++---- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Tests/Testably.Abstractions.Testing.Tests/NotificationTests.cs b/Tests/Testably.Abstractions.Testing.Tests/NotificationTests.cs index da6ee7d7..9d6d1cac 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/NotificationTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/NotificationTests.cs @@ -19,13 +19,13 @@ public void AwaitableCallback_Amount_ShouldOnlyReturnAfterNumberOfCallbacks() } }); - _ = Task.Run(() => + _ = Task.Run(async () => { - Thread.Sleep(10); + await Task.Delay(10); for (int i = 1; i <= 10; i++) { timeSystem.Thread.Sleep(i); - Thread.Sleep(1); + await Task.Delay(1); } }); @@ -82,13 +82,13 @@ public void AwaitableCallback_Filter_ShouldOnlyUpdateAfterFilteredValue() receivedCount++; }); - _ = Task.Run(() => + _ = Task.Run(async () => { - Thread.Sleep(10); + await Task.Delay(10); for (int i = 1; i <= 10; i++) { timeSystem.Thread.Sleep(i); - Thread.Sleep(1); + await Task.Delay(1); } }); @@ -107,16 +107,16 @@ public void AwaitableCallback_Predicate_ShouldOnlyUpdateAfterFilteredValue() receivedCount++; }, t => t.TotalMilliseconds > 6); - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try { - Thread.Sleep(10); + await Task.Delay(10); for (int i = 1; i <= 10; i++) { timeSystem.Thread.Sleep(i); - Thread.Sleep(1); + await Task.Delay(1); } ms.Set(); @@ -145,7 +145,7 @@ public void AwaitableCallback_ShouldWaitForCallbackExecution() isCalled = true; }); - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try @@ -153,7 +153,7 @@ public void AwaitableCallback_ShouldWaitForCallbackExecution() while (!ms.IsSet) { timeSystem.Thread.Sleep(1); - Thread.Sleep(1); + await Task.Delay(1); } } catch (ObjectDisposedException) diff --git a/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileSystemWatcherStatisticsTests.cs b/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileSystemWatcherStatisticsTests.cs index 4d3b0136..d87f07f8 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileSystemWatcherStatisticsTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileSystemWatcherStatisticsTests.cs @@ -46,11 +46,11 @@ public void Method_WaitForChanged_WatcherChangeTypes_Int_ShouldRegisterCall() // Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns. using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30)); CancellationToken token = cts.Token; - _ = Task.Run(() => + _ = Task.Run(async () => { while (!token.IsCancellationRequested) { - Thread.Sleep(10); + await Task.Delay(10); sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory")); sut.Directory.Delete(sut.Path.Combine("foo", "some-directory")); } @@ -76,11 +76,11 @@ public void Method_WaitForChanged_WatcherChangeTypes_ShouldRegisterCall() // Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns. using CancellationTokenSource cts = new(); CancellationToken token = cts.Token; - _ = Task.Run(() => + _ = Task.Run(async () => { while (!token.IsCancellationRequested) { - Thread.Sleep(10); + await Task.Delay(10); sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory")); sut.Directory.Delete(sut.Path.Combine("foo", "some-directory")); } @@ -105,11 +105,11 @@ public void Method_WaitForChanged_WatcherChangeTypes_TimeSpan_ShouldRegisterCall // Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns. using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30)); CancellationToken token = cts.Token; - _ = Task.Run(() => + _ = Task.Run(async () => { while (!token.IsCancellationRequested) { - Thread.Sleep(10); + await Task.Delay(10); sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory")); sut.Directory.Delete(sut.Path.Combine("foo", "some-directory")); } diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/EventTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/EventTests.cs index 86c2c41d..06f6bb69 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/EventTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/EventTests.cs @@ -41,7 +41,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e) try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try @@ -51,7 +51,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e) { string content = i++.ToString(CultureInfo.InvariantCulture); FileSystem.File.WriteAllText(path, content); - Thread.Sleep(10); + await Task.Delay(10); ms1.Set(); } } @@ -114,7 +114,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e) try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try @@ -123,7 +123,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e) { FileSystem.Directory.CreateDirectory(path); FileSystem.Directory.Delete(path); - Thread.Sleep(10); + await Task.Delay(10); ms1.Set(); } } @@ -184,7 +184,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e) try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try @@ -193,7 +193,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e) { FileSystem.Directory.CreateDirectory(path); FileSystem.Directory.Delete(path); - Thread.Sleep(10); + await Task.Delay(10); ms1.Set(); } } @@ -255,7 +255,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e) try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try @@ -265,7 +265,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e) while (!token.IsCancellationRequested) { FileSystem.File.Move($"path-{i}", $"path-{++i}"); - Thread.Sleep(10); + await Task.Delay(10); ms1.Set(); } } diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/Tests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/Tests.cs index 1f7dbc56..ec36cdca 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/Tests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/Tests.cs @@ -28,14 +28,14 @@ public void BeginInit_ShouldStopListening(string path) fileSystemWatcher.EnableRaisingEvents.Should().BeTrue(); try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try { while (!ms.IsSet) { - Thread.Sleep(10); + await Task.Delay(10); FileSystem.Directory.CreateDirectory(path); FileSystem.Directory.Delete(path); } @@ -86,14 +86,14 @@ public void EndInit_ShouldRestartListening(string path) fileSystemWatcher.EnableRaisingEvents.Should().BeTrue(); try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try { while (!ms.IsSet) { - Thread.Sleep(10); + await Task.Delay(10); FileSystem.Directory.CreateDirectory(path); FileSystem.Directory.Delete(path); } diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/WaitForChangedTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/WaitForChangedTests.cs index 947f4ce0..6fd74459 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/WaitForChangedTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/WaitForChangedTests.cs @@ -19,14 +19,14 @@ public void WaitForChanged_ShouldBlockUntilEventHappens(string path) FileSystem.FileSystemWatcher.New(BasePath); try { - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try { while (!ms.IsSet) { - Thread.Sleep(10); + await Task.Delay(10); FileSystem.Directory.CreateDirectory(path); FileSystem.Directory.Delete(path); } @@ -68,14 +68,14 @@ public void WaitForChanged_Timeout_ShouldReturnTimedOut(string path, try { fileSystemWatcher.EnableRaisingEvents = true; - _ = Task.Run(() => + _ = Task.Run(async () => { // ReSharper disable once AccessToDisposedClosure try { while (!ms.IsSet) { - Thread.Sleep(10); + await Task.Delay(10); FileSystem.Directory.CreateDirectory(fullPath); FileSystem.Directory.Delete(fullPath); }