diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs index b14449b3c84..4e01e45d664 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading; +using System.Threading.Tasks; using Xunit; namespace System.IO.Tests @@ -26,6 +27,41 @@ namespace System.IO.Tests } } + [OuterLoop] + [Fact] + public void FileSystemWatcher_File_Create_MultipleWatchers_ExecutionContextFlowed() + { + ExecuteWithRetry(() => + { + using (var watcher1 = new FileSystemWatcher(TestDirectory)) + using (var watcher2 = new FileSystemWatcher(TestDirectory)) + { + string fileName = Path.Combine(TestDirectory, "file"); + watcher1.Filter = Path.GetFileName(fileName); + watcher2.Filter = Path.GetFileName(fileName); + + var local = new AsyncLocal(); + + var tcs1 = new TaskCompletionSource(); + var tcs2 = new TaskCompletionSource(); + watcher1.Created += (s, e) => tcs1.SetResult(local.Value); + watcher2.Created += (s, e) => tcs2.SetResult(local.Value); + + local.Value = 42; + watcher1.EnableRaisingEvents = true; + local.Value = 84; + watcher2.EnableRaisingEvents = true; + local.Value = 0; + + File.Create(fileName).Dispose(); + Task.WaitAll(new[] { tcs1.Task, tcs2.Task }, WaitForExpectedEventTimeout); + + Assert.Equal(42, tcs1.Task.Result); + Assert.Equal(84, tcs2.Task.Result); + } + }); + } + [Fact] public void FileSystemWatcher_File_Create_ForcedRestart() { diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs index 1a23de08098..61991fcb584 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using Xunit; +using Xunit.Sdk; namespace System.IO.Tests { @@ -24,6 +25,7 @@ namespace System.IO.Tests public const int WaitForExpectedEventTimeout_NoRetry = 3000;// ms to wait for an event that isn't surrounded by a retry. public const int DefaultAttemptsForExpectedEvent = 3; // Number of times an expected event should be retried if failing. public const int DefaultAttemptsForUnExpectedEvent = 2; // Number of times an unexpected event should be retried if failing. + public const int RetryDelayMilliseconds = 500; // ms to wait when retrying after failure /// /// Watches the Changed WatcherChangeType and unblocks the returned AutoResetEvent when a @@ -173,7 +175,7 @@ namespace System.IO.Tests // Most intermittent failures in FSW are caused by either a shortage of resources (e.g. inotify instances) // or by insufficient time to execute (e.g. CI gets bogged down). Immediately re-running a failed test // won't resolve the first issue, so we wait a little while hoping that things clear up for the next run. - Thread.Sleep(500); + Thread.Sleep(RetryDelayMilliseconds); } result = ExecuteAndVerifyEvents(newWatcher, expectedEvents, action, attemptsCompleted == attempts, expectedPaths, timeout); @@ -183,6 +185,25 @@ namespace System.IO.Tests } } + /// Invokes the specified test action with retry on failure (other than assertion failure). + /// The test action. + /// The maximum number of times to attempt to run the test. + public static void ExecuteWithRetry(Action action, int maxAttempts = DefaultAttemptsForExpectedEvent) + { + for (int retry = 0; retry < maxAttempts; retry++) + { + try + { + action(); + return; + } + catch (Exception e) when (!(e is XunitException) && retry < maxAttempts - 1) + { + Thread.Sleep(RetryDelayMilliseconds); + } + } + } + /// /// Does verification that the given watcher will not throw exactly/only the events in "expectedEvents" when /// "action" is executed.