mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Add a FileSystemWatcher test for ExecutionContext (dotnet/corefx#33903)
Make sure that a) ExecutionContext properly flows, and b) events from multiple FileSystemWatchers are raised on the right ExecutionContext. Commit migrated from https://github.com/dotnet/corefx/commit/81c6ce11a40df6d93de97ffc11408569befba727
This commit is contained in:
1 parent
f108845249
commit
af60d4a06b
2 files changed
+58
-1
No files matched your search
@@ -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<int>();
|
||||
|
||||
var tcs1 = new TaskCompletionSource<int>();
|
||||
var tcs2 = new TaskCompletionSource<int>();
|
||||
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()
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Invokes the specified test action with retry on failure (other than assertion failure).</summary>
|
||||
/// <param name="action">The test action.</param>
|
||||
/// <param name="maxAttempts">The maximum number of times to attempt to run the test.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does verification that the given watcher will not throw exactly/only the events in "expectedEvents" when
|
||||
/// "action" is executed.
|
||||
|
||||
Reference in new issue
Block a user