Skip to content

Commit

Permalink
refactor: fix brittle EventTests (#599)
Browse files Browse the repository at this point in the history
The `FileSystemWatcher.EventTests` sometimes fail, due to race-condition. Therefore make the following changes:
- Continually make the triggering changes in a background task, until the `CancellationToken` is cancelled at the end of the test
- Add and listen to separate triggers for when the triggering change was made and when the change was detected in the callback
- Make tests synchronous
- Add class `TestBase` with common timeout settings and use them throughout the Testably.Abstractions.Tests project.
  • Loading branch information
vbreuss authored May 13, 2024
1 parent 223504f commit 3a18916
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Testably.Abstractions.TestHelpers;
/// <remarks>
/// Important: You have to mark your class as ´partial`!
/// </remarks>
public abstract class FileSystemTestBase<TFileSystem>
public abstract class FileSystemTestBase<TFileSystem> : TestBase
where TFileSystem : IFileSystem
{
public abstract string BasePath { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Testably.Abstractions.TestHelpers;
/// <remarks>
/// Important: You have to mark your class as ´partial`!
/// </remarks>
public abstract class RandomSystemTestBase<TRandomSystem>
public abstract class RandomSystemTestBase<TRandomSystem> : TestBase
where TRandomSystem : IRandomSystem
{
public TRandomSystem RandomSystem { get; }
Expand Down
22 changes: 22 additions & 0 deletions Tests/Helpers/Testably.Abstractions.TestHelpers/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Testably.Abstractions.TestHelpers;

/// <summary>
/// Base class for generated tests.
/// </summary>
public abstract class TestBase
{
/// <summary>
/// The delay in milliseconds when wanting to ensure a timeout in the test.
/// </summary>
public const int EnsureTimeout = 500;

/// <summary>
/// The delay in milliseconds when expecting a success in the test.
/// </summary>
public const int ExpectSuccess = 30000;

/// <summary>
/// The delay in milliseconds when expecting a timeout in the test.
/// </summary>
public const int ExpectTimeout = 30;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Testably.Abstractions.TestHelpers;
/// <remarks>
/// Important: You have to mark your class as ´partial`!
/// </remarks>
public abstract class TimeSystemTestBase<TTimeSystem>
public abstract class TimeSystemTestBase<TTimeSystem> : TestBase
where TTimeSystem : ITimeSystem
{
public TTimeSystem TimeSystem { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void Copy_ShouldCopyFileWithContent(

FileSystem.File.WriteAllText(sourceName, contents);

TimeSystem.Thread.Sleep(1000);
TimeSystem.Thread.Sleep(EnsureTimeout);

FileSystem.File.Copy(sourceName, destinationName);
FileSystem.Should().HaveFile(sourceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void CopyTo_ShouldCopyFileWithContent(
FileSystem.File.WriteAllText(sourceName, contents);
IFileInfo sut = FileSystem.FileInfo.New(sourceName);

TimeSystem.Thread.Sleep(1000);
TimeSystem.Thread.Sleep(EnsureTimeout);

IFileInfo result = sut.CopyTo(destinationName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void BeginRead_ShouldCopyContentsToBuffer(
}
}, null);

ms.Wait(30000);
ms.Wait(ExpectSuccess).Should().BeTrue();
buffer.Should().BeEquivalentTo(bytes);
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public void EndRead_ShouldNotAdjustTimes(string path, byte[] bytes)
}
}, null);

ms.Wait(10000);
ms.Wait(ExpectSuccess).Should().BeTrue();
}

DateTime creationTime = FileSystem.File.GetCreationTimeUtc(path);
Expand Down Expand Up @@ -202,7 +202,7 @@ public void Read_ShouldFillBuffer(string path, byte[] bytes)
public async Task ReadAsync_CanReadFalse_ShouldThrowNotSupportedException(
string path, byte[] bytes)
{
using CancellationTokenSource cts = new(30000);
using CancellationTokenSource cts = new(ExpectSuccess);
byte[] buffer = new byte[bytes.Length];
await FileSystem.File.WriteAllBytesAsync(path, bytes, cts.Token);
Exception? exception;
Expand All @@ -228,7 +228,7 @@ public async Task ReadAsync_CanReadFalse_ShouldThrowNotSupportedException(
public async Task ReadAsync_Memory_CanReadFalse_ShouldThrowNotSupportedException(
string path, byte[] bytes)
{
using CancellationTokenSource cts = new(30000);
using CancellationTokenSource cts = new(ExpectSuccess);
byte[] buffer = new byte[bytes.Length];
await FileSystem.File.WriteAllBytesAsync(path, bytes, cts.Token);
Exception? exception;
Expand All @@ -253,7 +253,7 @@ public async Task ReadAsync_Memory_CanReadFalse_ShouldThrowNotSupportedException
[AutoData]
public async Task ReadAsync_ShouldFillBuffer(string path, byte[] bytes)
{
using CancellationTokenSource cts = new(30000);
using CancellationTokenSource cts = new(ExpectSuccess);
byte[] buffer = new byte[bytes.Length];
await FileSystem.File.WriteAllBytesAsync(path, bytes, cts.Token);
await using FileSystemStream stream = FileSystem.File.OpenRead(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void BeginWrite_ShouldCopyContentsToFile(
}
}, null);

ms.Wait(30000);
ms.Wait(ExpectSuccess).Should().BeTrue();
}

FileSystem.Should().HaveFile(path)
Expand Down Expand Up @@ -108,7 +108,7 @@ public void EndWrite_ShouldAdjustTimes(string path, byte[] bytes)
}
}, null);

ms.Wait(10000);
ms.Wait(ExpectSuccess).Should().BeTrue();
}

DateTime creationTime = FileSystem.File.GetCreationTimeUtc(path);
Expand Down Expand Up @@ -210,7 +210,7 @@ public void Write_ShouldFillBuffer(string path, byte[] bytes)
public async Task WriteAsync_CanWriteFalse_ShouldThrowNotSupportedException(
string path, byte[] bytes)
{
using CancellationTokenSource cts = new(30000);
using CancellationTokenSource cts = new(ExpectSuccess);
byte[] buffer = new byte[bytes.Length];
await FileSystem.File.WriteAllBytesAsync(path, bytes, cts.Token);
Exception? exception;
Expand All @@ -236,7 +236,7 @@ public async Task WriteAsync_CanWriteFalse_ShouldThrowNotSupportedException(
[AutoData]
public async Task WriteAsync_ShouldFillBuffer(string path, byte[] bytes)
{
using CancellationTokenSource cts = new(30000);
using CancellationTokenSource cts = new(ExpectSuccess);

await using (FileSystemStream stream = FileSystem.File.Create(path))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public void EnableRaisingEvents_SetToFalse_ShouldStop(string path1, string path2
};
fileSystemWatcher.EnableRaisingEvents = true;
FileSystem.Directory.Delete(path1);
ms.Wait(10000).Should().BeTrue();
ms.Wait(ExpectSuccess).Should().BeTrue();
ms.Reset();

fileSystemWatcher.EnableRaisingEvents = false;

FileSystem.Directory.Delete(path2);
ms.Wait(30).Should().BeFalse();
ms.Wait(ExpectTimeout).Should().BeFalse();
}

[SkippableTheory]
Expand All @@ -62,6 +62,6 @@ public void EnableRaisingEvents_ShouldBeInitializedAsFalse(string path)

FileSystem.Directory.Delete(path);

ms.Wait(30).Should().BeFalse();
ms.Wait(ExpectTimeout).Should().BeFalse();
}
}
Loading

0 comments on commit 3a18916

Please sign in to comment.