Skip to content

Commit

Permalink
Avoid using Thread.Sleep in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Dec 16, 2024
1 parent ac9e237 commit c398a61
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
22 changes: 11 additions & 11 deletions Tests/Testably.Abstractions.Testing.Tests/NotificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand All @@ -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();
Expand Down Expand Up @@ -145,15 +145,15 @@ public void AwaitableCallback_ShouldWaitForCallbackExecution()
isCalled = true;
});

_ = Task.Run(() =>
_ = Task.Run(async () =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
while (!ms.IsSet)
{
timeSystem.Thread.Sleep(1);
Thread.Sleep(1);
await Task.Delay(1);
}
}
catch (ObjectDisposedException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand All @@ -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"));
}
Expand All @@ -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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(() =>
_ = Task.Run(async () =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(() =>
_ = Task.Run(async () =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(() =>
_ = Task.Run(async () =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(() =>
_ = Task.Run(async () =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c398a61

Please sign in to comment.