Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ public IChangeToken CreateFileChangeToken(string filter)
}

IChangeToken changeToken = GetOrAddChangeToken(filter);
TryEnableFileSystemWatcher();
if (!PollForChanges)
{
TryEnableFileSystemWatcher();
}

return changeToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders.Internal;
using Microsoft.Extensions.FileProviders.Physical;
Expand Down Expand Up @@ -87,6 +89,48 @@ public void GetFileInfoReturnsNotFoundFileInfoForIllegalPathWithLeadingSlashes_U
GetFileInfoReturnsNotFoundFileInfoForIllegalPathWithLeadingSlashes(path);
}

[Fact]
[PlatformSpecific(TestPlatforms.Linux)]
public async void PollingFileProviderShouldntConsumeINotifyInstances()
{
List<IDisposable> disposables = new List<IDisposable>();
using var root = new DisposableFileSystem();

int callbacks = 0;
int maxInstances = int.Parse(File.ReadAllText("/proc/sys/fs/inotify/max_user_instances"));
int instances = maxInstances + 16;
try
{
for (int i = 0; i < instances; i++)
{
PhysicalFileProvider pfp = new PhysicalFileProvider(root.RootPath)
{
UsePollingFileWatcher = true,
UseActivePolling = true
};
disposables.Add(pfp);
disposables.Add(pfp.Watch("*").RegisterChangeCallback(
o => Interlocked.Increment(ref callbacks),
i));
}

// trigger an event
root.CreateFile("test.txt");

// let some events fire
await Task.Delay(WaitTimeForTokenToFire * 16);

Assert.NotEqual(0, callbacks);
}
finally
{
foreach (var disposable in disposables)
{
disposable.Dispose();
}
}
}

private void GetFileInfoReturnsNotFoundFileInfoForIllegalPathWithLeadingSlashes(string path)
{
using (var provider = new PhysicalFileProvider(Path.GetTempPath()))
Expand Down