Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ScriptAnalysis/MonacoEditorDisposeTests.cs
T
Joseph Doherty b85e004bdb fix(adminui): Monaco dispose on a gone circuit is expected, not an error (#902)
Closing a tab with the script editor open ended the circuit; MonacoEditor.DisposeAsync then
called MonacoBlazor.dispose through SafeInvokeAsync, which caught only JSDisconnectedException,
so the TaskCanceledException from the torn-down circuit escaped and was logged at [ERR].

DisposeAsync now treats JSDisconnectedException, OperationCanceledException (incl.
TaskCanceledException) and ObjectDisposedException as the expected gone-circuit outcome and logs
them at Debug; a real JSException from a live page is still logged (Warning); the
DotNetObjectReference is released in a finally. MonacoEditor is the only component in the repo
that uses JS interop, so there is nothing else to align.

bUnit tests: dispose throwing each of the three gone-circuit exceptions neither escapes nor logs
above Debug (with a positive control that the failing call ran), and a JSException still warns.
They fail against the previous DisposeAsync.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
2026-11-13 01:50:37 -05:00

95 lines
3.8 KiB
C#

using Bunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis;
/// <summary>
/// lmxopcua#902: closing a tab with the script editor open ended the circuit, and the editor's
/// <c>DisposeAsync</c> JS call then failed with <see cref="TaskCanceledException"/>, which escaped and was logged
/// at Error as an unhandled circuit exception. A JS-interop failure caused by the circuit already being gone is
/// the expected outcome of disposal: it must not escape and must not log above Debug. A genuine
/// <see cref="JSException"/> from a live page is still surfaced (Warning).
/// </summary>
[Trait("Category", "Unit")]
public sealed class MonacoEditorDisposeTests : BunitContext
{
private readonly CapturingLoggerProvider _logs = new();
public MonacoEditorDisposeTests()
{
Services.AddLogging(b => b.AddProvider(_logs).SetMinimumLevel(LogLevel.Trace));
JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult();
}
public static TheoryData<string> GoneCircuitFailures => new()
{
nameof(TaskCanceledException),
nameof(JSDisconnectedException),
nameof(ObjectDisposedException),
};
private static Exception Make(string name) => name switch
{
nameof(TaskCanceledException) => new TaskCanceledException(),
nameof(JSDisconnectedException) => new JSDisconnectedException("circuit gone"),
nameof(ObjectDisposedException) => new ObjectDisposedException("JSRuntime"),
_ => throw new ArgumentOutOfRangeException(nameof(name)),
};
[Theory]
[MemberData(nameof(GoneCircuitFailures))]
public async Task Dispose_on_a_gone_circuit_is_quiet(string failure)
{
var dispose = JSInterop.SetupVoid("MonacoBlazor.dispose", _ => true);
dispose.SetException(Make(failure));
Render<MonacoEditor>(p => p.Add(x => x.ShowToolbar, false));
// Disposing the render tree is what the circuit does when the tab closes.
await Should.NotThrowAsync(() => DisposeComponentsAsync());
JSInterop.VerifyInvoke("MonacoBlazor.dispose"); // positive control: the failing call really ran
_logs.Entries.Where(e => e.Level > LogLevel.Debug).ShouldBeEmpty();
_logs.Entries.ShouldContain(e => e.Level == LogLevel.Debug && e.Message.Contains(failure));
}
[Fact]
public async Task A_real_js_fault_on_dispose_is_still_logged()
{
JSInterop.SetupVoid("MonacoBlazor.dispose", _ => true).SetException(new JSException("monaco blew up"));
Render<MonacoEditor>(p => p.Add(x => x.ShowToolbar, false));
await Should.NotThrowAsync(() => DisposeComponentsAsync());
_logs.Entries.ShouldContain(e => e.Level == LogLevel.Warning && e.Exception is JSException);
}
private sealed record LogEntry(LogLevel Level, string Message, Exception? Exception);
private sealed class CapturingLoggerProvider : ILoggerProvider
{
public List<LogEntry> Entries { get; } = new();
public ILogger CreateLogger(string categoryName) => new Logger(this);
public void Dispose() { }
private sealed class Logger(CapturingLoggerProvider owner) : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
lock (owner.Entries) owner.Entries.Add(new LogEntry(logLevel, formatter(state, exception), exception));
}
}
}
}