fix(worker): review 2026-10-21 stream E — cache eviction, phased ReadBulk, poll/STA fault handling
- RemoveItem/Unregister evict value + write-completion caches (leak under churn) - ReadBulk snapshot path: add/advise all, one shared deadline, then tear down - alarm poll faults only after 3 consecutive failures; transient ones logged - AlarmCommandHandler disposes primary when standby build throws - StaRuntime pumps between queued work items - WRK-02: post-start STA loop death surfaces as StaHung WorkerFault - WRK-09: AppDomain unhandled-exception hook, exit code 7 - RETRIGGER documented as reserved, never synthesized - TST-05: live facts for Ping/GetSessionState/GetWorkerInfo/DrainEvents/ShutdownWorker
This commit is contained in:
@@ -155,6 +155,124 @@ public sealed class StaRuntimeTests
|
||||
Assert.Contains("shutting down", exception.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A backlog of queued work items must not starve inbound COM
|
||||
/// callbacks: the loop pumps Windows messages between items rather
|
||||
/// than only after draining the whole queue. A gate item holds the STA
|
||||
/// while two more items queue behind it; once released, the second
|
||||
/// queued item must observe a pump that happened after the first.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ProcessQueuedCommands_PumpsMessagesBetweenQueuedWorkItems()
|
||||
{
|
||||
ControllableMessagePump pump = new();
|
||||
using StaRuntime runtime = new(new RecordingComApartmentInitializer(), pump, TimeSpan.FromSeconds(30));
|
||||
runtime.Start();
|
||||
|
||||
using ManualResetEventSlim gateEntered = new(false);
|
||||
using ManualResetEventSlim releaseGate = new(false);
|
||||
Task gate = runtime.InvokeAsync(() =>
|
||||
{
|
||||
gateEntered.Set();
|
||||
releaseGate.Wait(TimeSpan.FromSeconds(10));
|
||||
});
|
||||
Assert.True(gateEntered.Wait(TimeSpan.FromSeconds(5)));
|
||||
|
||||
Task<int> first = runtime.InvokeAsync(() => pump.PumpCount);
|
||||
Task<int> second = runtime.InvokeAsync(() => pump.PumpCount);
|
||||
releaseGate.Set();
|
||||
|
||||
await gate;
|
||||
int pumpsSeenByFirst = await first;
|
||||
int pumpsSeenBySecond = await second;
|
||||
|
||||
Assert.True(
|
||||
pumpsSeenBySecond > pumpsSeenByFirst,
|
||||
$"Expected a pump between queued work items (first saw {pumpsSeenByFirst}, second saw {pumpsSeenBySecond}).");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WRK-02: an exception that kills the STA loop after a successful
|
||||
/// <see cref="StaRuntime.Start"/> must be surfaced — recorded on
|
||||
/// <see cref="StaRuntime.LoopFault"/>, handed to the registered fault
|
||||
/// handler, and turned into fast failures for every later
|
||||
/// <see cref="StaRuntime.InvokeAsync{T}(Func{T}, CancellationToken)"/> —
|
||||
/// rather than parked in a startup field nothing reads.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task LoopException_AfterStart_IsReportedToHandlerAndFailsLaterCommands()
|
||||
{
|
||||
ControllableMessagePump pump = new();
|
||||
RecordingComApartmentInitializer initializer = new();
|
||||
using StaRuntime runtime = new(initializer, pump, TimeSpan.FromMilliseconds(25));
|
||||
runtime.Start();
|
||||
|
||||
TaskCompletionSource<Exception> reported = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
runtime.RegisterLoopFaultHandler(exception => reported.TrySetResult(exception));
|
||||
Assert.Null(runtime.LoopFault);
|
||||
|
||||
InvalidOperationException loopException = new("pump failed");
|
||||
pump.ThrowOnNextPump(loopException);
|
||||
|
||||
Task completed = await Task.WhenAny(reported.Task, Task.Delay(TimeSpan.FromSeconds(5)));
|
||||
Assert.Same(reported.Task, completed);
|
||||
Assert.Same(loopException, await reported.Task);
|
||||
Assert.Same(loopException, runtime.LoopFault);
|
||||
|
||||
InvalidOperationException rejected = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => runtime.InvokeAsync(() => 1));
|
||||
Assert.IsNotType<StaRuntimeShutdownException>(rejected);
|
||||
Assert.Same(loopException, rejected.InnerException);
|
||||
|
||||
// The dying thread still runs its apartment cleanup.
|
||||
Assert.True(runtime.Shutdown(TimeSpan.FromSeconds(5)));
|
||||
Assert.False(runtime.IsRunning);
|
||||
Assert.Equal(1, initializer.UninitializeCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A handler registered after the loop already died still receives the
|
||||
/// fault, so the window between Start and registration loses nothing.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task RegisterLoopFaultHandler_AfterLoopDied_InvokesHandlerImmediately()
|
||||
{
|
||||
ControllableMessagePump pump = new();
|
||||
using StaRuntime runtime = new(new RecordingComApartmentInitializer(), pump, TimeSpan.FromMilliseconds(25));
|
||||
runtime.Start();
|
||||
InvalidOperationException loopException = new("pump failed");
|
||||
pump.ThrowOnNextPump(loopException);
|
||||
|
||||
DateTime deadline = DateTime.UtcNow.AddSeconds(5);
|
||||
while (runtime.LoopFault is null && DateTime.UtcNow < deadline)
|
||||
{
|
||||
await Task.Delay(10);
|
||||
}
|
||||
|
||||
Exception? seen = null;
|
||||
runtime.RegisterLoopFaultHandler(exception => seen = exception);
|
||||
|
||||
Assert.Same(loopException, seen);
|
||||
}
|
||||
|
||||
/// <summary>A requested shutdown is not a loop fault.</summary>
|
||||
[Fact]
|
||||
public void Shutdown_DoesNotRecordLoopFault()
|
||||
{
|
||||
using StaRuntime runtime = new(new RecordingComApartmentInitializer(), new ControllableMessagePump(), TimeSpan.FromMilliseconds(25));
|
||||
runtime.Start();
|
||||
bool invoked = false;
|
||||
runtime.RegisterLoopFaultHandler(_ => invoked = true);
|
||||
|
||||
Assert.True(runtime.Shutdown(TimeSpan.FromSeconds(5)));
|
||||
|
||||
Assert.Null(runtime.LoopFault);
|
||||
Assert.False(invoked);
|
||||
}
|
||||
|
||||
private static StaRuntime CreateRuntime(RecordingComApartmentInitializer initializer)
|
||||
{
|
||||
return new StaRuntime(
|
||||
|
||||
Reference in New Issue
Block a user