Skip to content

Commit

Permalink
refactor: avoid blocking in tests (#598)
Browse files Browse the repository at this point in the history
* Avoid async methods in `Task.Run` in tests

* Make `FileStream.ReadTests` and `FileStream.WriteTests` part of the sequentially executed tests

* Set `EnableRaisingEvents` in `FileSystemWatcherStatisticsTests`
  • Loading branch information
vbreuss committed May 13, 2024
1 parent 17578f7 commit 64be166
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 36 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(async () =>
_ = Task.Run(() =>
{
await Task.Delay(10);
Thread.Sleep(10);
for (int i = 1; i <= 10; i++)
{
timeSystem.Thread.Sleep(i);
await Task.Delay(1);
Thread.Sleep(1);
}
});

Expand Down Expand Up @@ -82,13 +82,13 @@ public void AwaitableCallback_Filter_ShouldOnlyUpdateAfterFilteredValue()
receivedCount++;
});

_ = Task.Run(async () =>
_ = Task.Run(() =>
{
await Task.Delay(10);
Thread.Sleep(10);
for (int i = 1; i <= 10; i++)
{
timeSystem.Thread.Sleep(i);
await Task.Delay(1);
Thread.Sleep(1);
}
});

Expand All @@ -107,16 +107,16 @@ public void AwaitableCallback_Predicate_ShouldOnlyUpdateAfterFilteredValue()
receivedCount++;
}, t => t.TotalMilliseconds > 6);

_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
await Task.Delay(10);
Thread.Sleep(10);
for (int i = 1; i <= 10; i++)
{
timeSystem.Thread.Sleep(i);
await Task.Delay(1);
Thread.Sleep(1);
}
ms.Set();
Expand Down Expand Up @@ -145,15 +145,15 @@ public void AwaitableCallback_ShouldWaitForCallbackExecution()
isCalled = true;
});

_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
while (!ms.IsSet)
{
timeSystem.Thread.Sleep(1);
await Task.Delay(1);
Thread.Sleep(1);
}
}
catch (ObjectDisposedException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ public void Method_WaitForChanged_WatcherChangeTypes_Int_ShouldRegisterCall()
sut.Initialize().WithSubdirectory("foo");
using IFileSystemWatcher fileSystemWatcher = sut.FileSystemWatcher.New("foo");
// Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns.
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(1));
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30));
CancellationToken token = cts.Token;
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
await Task.Delay(10, token);
Thread.Sleep(10);
sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory"));
sut.Directory.Delete(sut.Path.Combine("foo", "some-directory"));
}
}, token);
WatcherChangeTypes changeType = WatcherChangeTypes.Created;
fileSystemWatcher.EnableRaisingEvents = true;
int timeout = 42;

fileSystemWatcher.WaitForChanged(changeType, timeout);
Expand All @@ -73,18 +74,19 @@ public void Method_WaitForChanged_WatcherChangeTypes_ShouldRegisterCall()
sut.Initialize().WithSubdirectory("foo");
using IFileSystemWatcher fileSystemWatcher = sut.FileSystemWatcher.New("foo");
// Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns.
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(1));
using CancellationTokenSource cts = new();
CancellationToken token = cts.Token;
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
await Task.Delay(10, token);
Thread.Sleep(10);
sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory"));
sut.Directory.Delete(sut.Path.Combine("foo", "some-directory"));
}
}, token);
WatcherChangeTypes changeType = WatcherChangeTypes.Created;
fileSystemWatcher.EnableRaisingEvents = true;

fileSystemWatcher.WaitForChanged(changeType);
cts.Cancel();
Expand All @@ -101,18 +103,19 @@ public void Method_WaitForChanged_WatcherChangeTypes_TimeSpan_ShouldRegisterCall
sut.Initialize().WithSubdirectory("foo");
using IFileSystemWatcher fileSystemWatcher = sut.FileSystemWatcher.New("foo");
// Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns.
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(1));
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30));
CancellationToken token = cts.Token;
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
await Task.Delay(10, token);
Thread.Sleep(10);
sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory"));
sut.Directory.Delete(sut.Path.Combine("foo", "some-directory"));
}
}, token);
WatcherChangeTypes changeType = WatcherChangeTypes.Created;
fileSystemWatcher.EnableRaisingEvents = true;
TimeSpan timeout = TimeSpan.FromSeconds(2);

fileSystemWatcher.WaitForChanged(changeType, timeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Testably.Abstractions.Tests.FileSystem.FileStream;

// ReSharper disable once PartialTypeWithSinglePart
[Collection("RealFileSystemTests")]
public abstract partial class ReadTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Testably.Abstractions.Tests.FileSystem.FileStream;

// ReSharper disable once PartialTypeWithSinglePart
[Collection("RealFileSystemTests")]
public abstract partial class WriteTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -48,7 +48,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e)
{
FileSystem.File.WriteAllText(path,
i++.ToString(CultureInfo.InvariantCulture));
await Task.Delay(10);
Thread.Sleep(10);
}
}
catch (IOException)
Expand Down Expand Up @@ -104,7 +104,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -113,7 +113,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e)
{
FileSystem.Directory.CreateDirectory(path);
FileSystem.Directory.Delete(path);
await Task.Delay(10);
Thread.Sleep(10);
}
}
catch (ObjectDisposedException)
Expand Down Expand Up @@ -166,7 +166,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -175,7 +175,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e)
{
FileSystem.Directory.CreateDirectory(path);
FileSystem.Directory.Delete(path);
await Task.Delay(10);
Thread.Sleep(10);
}
}
catch (ObjectDisposedException)
Expand Down Expand Up @@ -229,7 +229,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e)

try
{
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
Expand All @@ -239,7 +239,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e)
while (!ms.IsSet)
{
FileSystem.File.Move($"path-{i}", $"path-{++i}");
await Task.Delay(10);
Thread.Sleep(10);
}
}
catch (ObjectDisposedException)
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(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
while (!ms.IsSet)
{
await Task.Delay(10);
Thread.Sleep(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(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
while (!ms.IsSet)
{
await Task.Delay(10);
Thread.Sleep(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(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
while (!ms.IsSet)
{
await Task.Delay(10);
Thread.Sleep(10);
FileSystem.Directory.CreateDirectory(path);
FileSystem.Directory.Delete(path);
}
Expand Down Expand Up @@ -68,14 +68,14 @@ public void WaitForChanged_ShouldBlockUntilEventHappens(string path)
try
{
fileSystemWatcher.EnableRaisingEvents = true;
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
while (!ms.IsSet)
{
await Task.Delay(10);
Thread.Sleep(10);
FileSystem.Directory.CreateDirectory(fullPath);
FileSystem.Directory.Delete(fullPath);
}
Expand Down

0 comments on commit 64be166

Please sign in to comment.