test: replace runtime root source freezes
This commit is contained in:
parent
80c7b44457
commit
3c492aedc2
4 changed files with 248 additions and 220 deletions
|
|
@ -1,139 +1,109 @@
|
|||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using AcDream.App.Rendering;
|
||||
using AcDream.App.Tests.Architecture;
|
||||
using AcDream.Runtime.Session;
|
||||
using Silk.NET.Windowing;
|
||||
|
||||
namespace AcDream.App.Tests.Rendering;
|
||||
|
||||
/// <summary>
|
||||
/// Fix #406: before this change, <c>GameWindow.CompleteShutdown</c> wrote
|
||||
/// a hardcoded <c>exited{code:0,reason:"graceful"}</c> status event
|
||||
/// whenever the resource-shutdown transaction converged — even when
|
||||
/// <c>Dispose()</c> (and therefore <c>CompleteShutdown</c>) ran mid-unwind
|
||||
/// of an exception that escaped <c>Run()</c>'s Silk.NET frame loop and was
|
||||
/// about to crash the process via the CLR's unhandled-exception path.
|
||||
/// <c>GameWindow</c> cannot be constructed without a live GPU/window (see
|
||||
/// the established pattern in <c>GameWindowHostBoundaryTests</c>), so
|
||||
/// this pins the fix as a source-shape test exactly like that file does
|
||||
/// for the surrounding shutdown machinery.
|
||||
/// Fix #406: shutdown must not report a graceful exit while an exception from
|
||||
/// the native frame loop is still unwinding. These checks inspect compiled
|
||||
/// latch and status edges because constructing <see cref="GameWindow"/> would
|
||||
/// require a real GPU/window.
|
||||
/// </summary>
|
||||
public sealed class GameWindowCrashStatusTests
|
||||
{
|
||||
[Fact]
|
||||
public void Run_LatchesRunFailureBeforeRethrowingFromTheFrameLoopCatch()
|
||||
{
|
||||
string body = MethodBody(
|
||||
"public void Run()",
|
||||
"void IGameWindowPlatformPublication<GameWindowGraphics, IInputContext>.PublishGraphics(");
|
||||
string tryBlock = Slice(body, "try\n {\n _window.Run();", "}\n }");
|
||||
MethodInfo run = RequiredMethod(nameof(GameWindow.Run));
|
||||
IReadOnlyList<CompiledCall> calls = CompiledCallGraph.Read(run);
|
||||
CompiledCall frameLoop = Assert.Single(
|
||||
calls,
|
||||
call => call.Target.Name == nameof(IWindow.Run)
|
||||
&& call.Target.DeclaringType?.Namespace == "Silk.NET.Windowing");
|
||||
CompiledCall retain = Assert.Single(
|
||||
calls,
|
||||
call => call.Target.DeclaringType
|
||||
== typeof(ResourceConstructionCleanupLedger)
|
||||
&& call.Target.Name
|
||||
== nameof(ResourceConstructionCleanupLedger.RetainFrom));
|
||||
CompiledFieldReference latch = Assert.Single(
|
||||
CompiledCallGraph.ReadFieldReferences(run),
|
||||
field => field.Field.Name == "_runFailure"
|
||||
&& field.OpCode == OpCodes.Stfld);
|
||||
CompiledInstruction rethrow = Assert.Single(
|
||||
CompiledCallGraph.ReadInstructions(run),
|
||||
instruction => instruction.OpCode == OpCodes.Rethrow);
|
||||
|
||||
AssertAppearsInOrder(
|
||||
tryBlock,
|
||||
"_window.Run();",
|
||||
"catch (Exception failure)",
|
||||
"_constructionCleanup.RetainFrom(failure);",
|
||||
// The latch MUST happen before the rethrow: Dispose() (and
|
||||
// therefore CompleteShutdown/ReportExited) can run mid-unwind
|
||||
// of this exact exception, via Program.cs's
|
||||
// `using var window = ...`.
|
||||
"_runFailure = failure;",
|
||||
"throw;");
|
||||
Assert.True(frameLoop.Offset < retain.Offset);
|
||||
Assert.True(retain.Offset < latch.Offset);
|
||||
Assert.True(latch.Offset < rethrow.Offset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReportExited_ChecksRunFailureBeforeEitherGracefulOrShutdownIncompletePaths()
|
||||
{
|
||||
string source = GameWindowSource();
|
||||
string reportExited = Slice(
|
||||
source,
|
||||
"private void ReportExited(GameWindowLifetimeReport report)",
|
||||
"\n }\n");
|
||||
MethodInfo reportExited = RequiredMethod("ReportExited");
|
||||
IReadOnlyList<CompiledCall> calls = CompiledCallGraph.Read(reportExited);
|
||||
CompiledFieldReference failureRead = Assert.Single(
|
||||
CompiledCallGraph.ReadFieldReferences(reportExited),
|
||||
field => field.Field.Name == "_runFailure"
|
||||
&& field.OpCode == OpCodes.Ldfld);
|
||||
CompiledCall[] exited = calls
|
||||
.Where(call => call.Target.DeclaringType == typeof(SessionStatusWriter)
|
||||
&& call.Target.Name == nameof(SessionStatusWriter.Exited))
|
||||
.ToArray();
|
||||
Assert.Equal(3, exited.Length);
|
||||
CompiledCall status = Assert.Single(
|
||||
calls,
|
||||
call => call.Target.DeclaringType == typeof(GameWindowLifetimeReport)
|
||||
&& call.Target.Name == "get_Status");
|
||||
|
||||
Assert.Contains(
|
||||
"string sessionId = _options.SessionId ?? \"app\";",
|
||||
reportExited,
|
||||
StringComparison.Ordinal);
|
||||
AssertAppearsInOrder(
|
||||
reportExited,
|
||||
"if (_runFailure is not null)",
|
||||
"_statusWriter.Exited(sessionId, 1, \"crashed\");",
|
||||
"return;",
|
||||
"if (report.Status == GameWindowLifetimeStatus.Complete)",
|
||||
"_statusWriter.Exited(sessionId, 0, \"graceful\");",
|
||||
"_statusWriter.Exited(sessionId, 1, \"shutdown-incomplete\");");
|
||||
Assert.True(failureRead.Offset < exited[0].Offset);
|
||||
Assert.True(exited[0].Offset < status.Offset);
|
||||
Assert.True(status.Offset < exited[1].Offset);
|
||||
Assert.True(exited[1].Offset < exited[2].Offset);
|
||||
Assert.Equal(
|
||||
["app", "crashed", "graceful", "shutdown-incomplete"],
|
||||
CompiledCallGraph.ReadStringLiterals(reportExited));
|
||||
|
||||
// Every terminal-status write in CompleteShutdown funnels through
|
||||
// this ONE method — a second, uncoordinated call site would be
|
||||
// exactly how the pre-fix bug reappears.
|
||||
MethodInfo completeShutdown = RequiredMethod("CompleteShutdown");
|
||||
Assert.Equal(
|
||||
2,
|
||||
CountOccurrences(source, "ReportExited(report)"));
|
||||
Assert.DoesNotContain(
|
||||
"_statusWriter.Exited(_options.SessionId ?? \"app\", 0, \"graceful\")",
|
||||
source,
|
||||
StringComparison.Ordinal);
|
||||
CompiledCallGraph.Read(completeShutdown).Count(call =>
|
||||
call.Target.DeclaringType == typeof(GameWindow)
|
||||
&& call.Target.Name == "ReportExited"));
|
||||
Assert.Equal(
|
||||
exited.Length,
|
||||
CompiledCallGraph.ReadDeclared(typeof(GameWindow)).Count(call =>
|
||||
call.Target.DeclaringType == typeof(SessionStatusWriter)
|
||||
&& call.Target.Name == nameof(SessionStatusWriter.Exited)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RunFailureFieldExistsAndDefaultsToNull()
|
||||
{
|
||||
string source = GameWindowSource();
|
||||
FieldInfo field = typeof(GameWindow).GetField(
|
||||
"_runFailure",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic)
|
||||
?? throw new MissingFieldException(typeof(GameWindow).FullName, "_runFailure");
|
||||
Assert.Equal(typeof(Exception), field.FieldType);
|
||||
|
||||
Assert.Contains(
|
||||
"private Exception? _runFailure;",
|
||||
source,
|
||||
StringComparison.Ordinal);
|
||||
ConstructorInfo[] constructors = typeof(GameWindow).GetConstructors(
|
||||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
Assert.NotEmpty(constructors);
|
||||
Assert.DoesNotContain(
|
||||
constructors.SelectMany(CompiledCallGraph.ReadFieldReferences),
|
||||
reference => reference.Field == field
|
||||
&& reference.OpCode == OpCodes.Stfld);
|
||||
}
|
||||
|
||||
private static string MethodBody(string start, string end) =>
|
||||
Slice(GameWindowSource(), start, end);
|
||||
|
||||
private static string Slice(string source, string start, string end)
|
||||
{
|
||||
int first = source.IndexOf(start, StringComparison.Ordinal);
|
||||
int last = source.IndexOf(end, first + 1, StringComparison.Ordinal);
|
||||
Assert.True(first >= 0, $"Missing source boundary: {start}");
|
||||
Assert.True(last > first, $"Missing source boundary: {end}");
|
||||
return source[first..last];
|
||||
}
|
||||
|
||||
private static int CountOccurrences(string source, string value)
|
||||
{
|
||||
int count = 0;
|
||||
int cursor = 0;
|
||||
while ((cursor = source.IndexOf(value, cursor, StringComparison.Ordinal)) >= 0)
|
||||
{
|
||||
count++;
|
||||
cursor += value.Length;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
private static void AssertAppearsInOrder(string source, params string[] fragments)
|
||||
{
|
||||
int cursor = -1;
|
||||
foreach (string fragment in fragments)
|
||||
{
|
||||
int next = source.IndexOf(fragment, cursor + 1, StringComparison.Ordinal);
|
||||
Assert.True(next >= 0, $"Missing expected source fragment: {fragment}");
|
||||
Assert.True(next > cursor, $"Out-of-order source fragment: {fragment}");
|
||||
cursor = next;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GameWindowSource() => File.ReadAllText(Path.Combine(
|
||||
FindRepoRoot(),
|
||||
"src",
|
||||
"AcDream.App",
|
||||
"Rendering",
|
||||
"GameWindow.cs")).Replace("\r\n", "\n", StringComparison.Ordinal);
|
||||
|
||||
private static string FindRepoRoot()
|
||||
{
|
||||
DirectoryInfo? directory = new(AppContext.BaseDirectory);
|
||||
while (directory is not null)
|
||||
{
|
||||
if (File.Exists(Path.Combine(directory.FullName, "AcDream.slnx")))
|
||||
return directory.FullName;
|
||||
directory = directory.Parent;
|
||||
}
|
||||
|
||||
throw new DirectoryNotFoundException("Could not find AcDream.slnx.");
|
||||
}
|
||||
private static MethodInfo RequiredMethod(string name) =>
|
||||
typeof(GameWindow).GetMethod(
|
||||
name,
|
||||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
?? throw new MissingMethodException(typeof(GameWindow).FullName, name);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue