Skip to content

Commit

Permalink
Address feedback on tests and improve them to cover more scenarios.
Browse files Browse the repository at this point in the history
  • Loading branch information
jozkee committed Jul 19, 2021
1 parent 1164e33 commit 98b737a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,67 @@ public void UsePollingFileWatcher_FileWatcherNotNull_ReturnsFalse()
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void UsePollingFileWatcher_UseActivePolling_HasChanged(bool useWildcard)
{
// Arrange
using var root = new DisposableFileSystem();
string fileName = Path.GetRandomFileName();
string filePath = Path.Combine(root.RootPath, fileName);
File.WriteAllText(filePath, "v1.1");

using var provider = new PhysicalFileProvider(root.RootPath) { UsePollingFileWatcher = true, UseActivePolling = true };
IChangeToken token = provider.Watch(useWildcard ? "*" : fileName);
Assert.False(token.HasChanged);

// Act
Thread.Sleep(100); // Wait a bit before writing again, see https://github.com/dotnet/runtime/issues/55951.
File.WriteAllText(filePath, "v1.2");
int timeout = GetTokenPollingInterval(token);
Thread.Sleep(timeout);

// Assert
Assert.True(token.HasChanged);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void UsePollingFileWatcher_UseActivePolling_HasChanged_FileDeleted(bool useWildcard)
{
// Arrange
using var root = new DisposableFileSystem();
string fileName = Path.GetRandomFileName();
string filePath = Path.Combine(root.RootPath, fileName);
File.WriteAllText(filePath, "v1.1");

string filter = useWildcard ? "*" : fileName;
using var provider = new PhysicalFileProvider(root.RootPath) { UsePollingFileWatcher = true, UseActivePolling = true };
IChangeToken token = provider.Watch(filter);
Assert.False(token.HasChanged);

// Act
File.Delete(filePath);
Thread.Sleep(GetTokenPollingInterval(token));

// Assert
Assert.True(token.HasChanged);
}

private int GetTokenPollingInterval(IChangeToken changeToken)
{
TimeSpan pollingInterval = (changeToken as CompositeChangeToken).ChangeTokens[1] switch
{
PollingWildCardChangeToken wildcardChangeToken => wildcardChangeToken.PollingInterval,
PollingFileChangeToken => PollingFileChangeToken.PollingInterval,
_ => throw new InvalidOperationException()
};

return (int)pollingInterval.TotalMilliseconds;
}

[Fact]
public void CreateFileWatcher_CreatesWatcherWithPollingAndActiveFlags()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,6 @@ namespace Microsoft.Extensions.FileProviders
{
public partial class PhysicalFileProviderTests
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void UsePollingFileWatcher_UseActivePolling_HasChanged(bool useWildcard)
{
// Arrange
using var root = new DisposableFileSystem();
string fileName = Path.GetRandomFileName();
string filePath = Path.Combine(root.RootPath, fileName);
File.WriteAllText(filePath, "v1.1");

using var provider = new PhysicalFileProvider(root.RootPath) { UsePollingFileWatcher = true, UseActivePolling = true };
IChangeToken token = provider.Watch(useWildcard ? "*" : fileName);
Assert.False(token.HasChanged);

// Act
File.WriteAllText(filePath, "v1.2");
Thread.Sleep(GetTokenPollingInterval(token));

// Assert
Assert.True(token.HasChanged);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand All @@ -55,6 +32,7 @@ public void UsePollingFileWatcher_UseActivePolling_HasChanged_SymbolicLink(bool
Assert.False(token.HasChanged);

// Act
Thread.Sleep(100); // Wait a bit before writing again, see https://github.com/dotnet/runtime/issues/55951.
File.WriteAllText(filePath, "v1.2");
Thread.Sleep(GetTokenPollingInterval(token));

Expand Down Expand Up @@ -82,18 +60,25 @@ public void UsePollingFileWatcher_UseActivePolling_HasChanged_SymbolicLink_Targe
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void UsePollingFileWatcher_UseActivePolling_HasChanged_SymbolicLink_TargetChanged(bool useWildcard)
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void UsePollingFileWatcher_UseActivePolling_HasChanged_SymbolicLink_TargetChanged(bool useWildcard, bool fromTargetNonExistent)
{
// Arrange
using var rootOfFile = new DisposableFileSystem();
string file1Path = Path.Combine(rootOfFile.RootPath, Path.GetRandomFileName());
File.WriteAllText(file1Path, "v1.1");

// Create file 2 first as we want to verify that the change is reported regardless of the timestamp being older.
string file2Path = Path.Combine(rootOfFile.RootPath, Path.GetRandomFileName());
File.WriteAllText(file2Path, "v2.1");

string file1Path = Path.Combine(rootOfFile.RootPath, Path.GetRandomFileName());
if (!fromTargetNonExistent)
{
Thread.Sleep(100); // Wait a bit before writing again, see https://github.com/dotnet/runtime/issues/55951.
File.WriteAllText(file1Path, "v1.1");
}

using var rootOfLink = new DisposableFileSystem();
string linkName = Path.GetRandomFileName();
string linkPath = Path.Combine(rootOfLink.RootPath, linkName);
Expand All @@ -104,43 +89,42 @@ public void UsePollingFileWatcher_UseActivePolling_HasChanged_SymbolicLink_Targe
IChangeToken token = provider.Watch(filter);
Assert.False(token.HasChanged);

// Act 1 - Change file 1's content.
File.WriteAllText(file1Path, "v1.2");
Thread.Sleep(GetTokenPollingInterval(token));

// Assert 1
Assert.True(token.HasChanged);

// Act 2 - Change link target to file 2.
token = provider.Watch(filter); // Once HasChanged is true, the value will always be true. Get a new change token.
Assert.False(token.HasChanged);
// Act - Change link target to file 2.
File.Delete(linkPath);
File.CreateSymbolicLink(linkPath, file2Path);
Thread.Sleep(GetTokenPollingInterval(token));

// Assert 2
// Assert
Assert.True(token.HasChanged); // It should report the change regardless of the timestamp being older.
}

// Act 3 - Change file 2's content.
token = provider.Watch(filter);
[Theory]
[InlineData(false)]
[InlineData(true)]
public void UsePollingFileWatcher_UseActivePolling_HasChanged_SymbolicLink_TargetDeleted(bool useWildcard)
{
// Arrange
using var rootOfFile = new DisposableFileSystem();

string filePath = Path.Combine(rootOfFile.RootPath, Path.GetRandomFileName());
File.WriteAllText(filePath, "v1.1");

using var rootOfLink = new DisposableFileSystem();
string linkName = Path.GetRandomFileName();
string linkPath = Path.Combine(rootOfLink.RootPath, linkName);
File.CreateSymbolicLink(linkPath, filePath);

string filter = useWildcard ? "*" : linkName;
using var provider = new PhysicalFileProvider(rootOfLink.RootPath) { UsePollingFileWatcher = true, UseActivePolling = true };
IChangeToken token = provider.Watch(filter);
Assert.False(token.HasChanged);
File.WriteAllText(file2Path, "v2.2");

// Act
File.Delete(linkPath);
Thread.Sleep(GetTokenPollingInterval(token));

// Assert 3
// Assert
Assert.True(token.HasChanged);
}

private int GetTokenPollingInterval(IChangeToken changeToken)
{
TimeSpan pollingInterval = (changeToken as CompositeChangeToken).ChangeTokens[1] switch
{
PollingWildCardChangeToken wildcardChangeToken => wildcardChangeToken.PollingInterval,
PollingFileChangeToken => PollingFileChangeToken.PollingInterval,
_ => throw new InvalidOperationException()
};

return (int)pollingInterval.TotalMilliseconds;
}
}
}

0 comments on commit 98b737a

Please sign in to comment.