3773509888
A-3: UnsTreeService pre-checks sibling (cluster,area,line) names on create/update/import, catches DbUpdateException, validates column lengths. A-4: blank ack password keeps the stored one; first-time blank refused. A-6: BrowseSessionReaper drains sessions in a finally on shutdown. A-7: node flushes response headers; central treats them as established; deadline warning throttled. A-9: key delete always reloads and names leftover ack secrets when cleanup fails. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
111 lines
4.6 KiB
C#
111 lines
4.6 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Browsing;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Browsing;
|
|
|
|
/// <summary>Unit tests for <see cref="BrowseSessionReaper"/>. We drive the reaper
|
|
/// directly through the internal <c>ReapOnceAsync</c> entry point and skew
|
|
/// <see cref="FakeBrowseSession.LastUsedUtc"/> to simulate the passage of time —
|
|
/// no real wall-clock waits.</summary>
|
|
public sealed class BrowseSessionReaperTests
|
|
{
|
|
private static BrowseSessionReaper NewReaper(BrowseSessionRegistry registry) =>
|
|
new(registry, NullLogger<BrowseSessionReaper>.Instance);
|
|
|
|
[Fact]
|
|
public async Task ReapOnceAsync_evicts_idle_session()
|
|
{
|
|
var registry = new BrowseSessionRegistry();
|
|
var session = new FakeBrowseSession
|
|
{
|
|
LastUsedUtc = DateTime.UtcNow - TimeSpan.FromMinutes(3),
|
|
};
|
|
registry.Register(session);
|
|
var reaper = NewReaper(registry);
|
|
|
|
await reaper.ReapOnceAsync(CancellationToken.None);
|
|
|
|
registry.TryGet(session.Token, out _).ShouldBeFalse();
|
|
session.Disposed.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReapOnceAsync_preserves_recent_session()
|
|
{
|
|
var registry = new BrowseSessionRegistry();
|
|
var session = new FakeBrowseSession { LastUsedUtc = DateTime.UtcNow };
|
|
registry.Register(session);
|
|
var reaper = NewReaper(registry);
|
|
|
|
await reaper.ReapOnceAsync(CancellationToken.None);
|
|
|
|
registry.TryGet(session.Token, out _).ShouldBeTrue();
|
|
session.Disposed.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReapOnceAsync_handles_already_removed_session()
|
|
{
|
|
var registry = new BrowseSessionRegistry();
|
|
var session = new FakeBrowseSession
|
|
{
|
|
LastUsedUtc = DateTime.UtcNow - TimeSpan.FromMinutes(3),
|
|
};
|
|
registry.Register(session);
|
|
// Race the reaper: pull the entry out before the loop calls TryRemove.
|
|
registry.TryRemove(session.Token, out _).ShouldBeTrue();
|
|
var reaper = NewReaper(registry);
|
|
|
|
// No exception, no double dispose. Because we removed the session ourselves
|
|
// without disposing it, Disposed should still be false.
|
|
await reaper.ReapOnceAsync(CancellationToken.None);
|
|
|
|
session.Disposed.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReapOnceAsync_continues_when_one_session_dispose_throws()
|
|
{
|
|
var registry = new BrowseSessionRegistry();
|
|
var staleAt = DateTime.UtcNow - TimeSpan.FromMinutes(3);
|
|
var bad = new FakeBrowseSession { LastUsedUtc = staleAt, ThrowOnDispose = true };
|
|
var good = new FakeBrowseSession { LastUsedUtc = staleAt };
|
|
registry.Register(bad);
|
|
registry.Register(good);
|
|
var reaper = NewReaper(registry);
|
|
|
|
// The dispose exception is swallowed; the second session must still be reaped + disposed.
|
|
await Should.NotThrowAsync(() => reaper.ReapOnceAsync(CancellationToken.None));
|
|
|
|
registry.TryGet(bad.Token, out _).ShouldBeFalse();
|
|
registry.TryGet(good.Token, out _).ShouldBeFalse();
|
|
good.Disposed.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>#997 A-6: stopping the host used to leave <c>ExecuteAsync</c> by an
|
|
/// <see cref="OperationCanceledException"/> from the timer wait, so the post-loop drain never ran and every
|
|
/// open browse session (OPC UA / MQTT / gateway) was abandoned instead of closed.</summary>
|
|
[Fact]
|
|
public async Task Stopping_the_service_drains_every_open_session_including_recent_ones()
|
|
{
|
|
var registry = new BrowseSessionRegistry();
|
|
var recent = new FakeBrowseSession { LastUsedUtc = DateTime.UtcNow }; // NOT idle: only the shutdown drain can close it
|
|
var throwing = new FakeBrowseSession { LastUsedUtc = DateTime.UtcNow, ThrowOnDispose = true };
|
|
registry.Register(recent);
|
|
registry.Register(throwing);
|
|
var reaper = NewReaper(registry);
|
|
|
|
await reaper.StartAsync(CancellationToken.None);
|
|
// Stopping before ExecuteAsync has actually begun cancels it unrun (the host may schedule it on the pool).
|
|
await reaper.LoopStarted.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
|
|
await reaper.StopAsync(CancellationToken.None);
|
|
await (reaper.ExecuteTask ?? Task.CompletedTask); // a faulted/cancelled ExecuteAsync would surface here
|
|
|
|
recent.Disposed.ShouldBeTrue();
|
|
registry.TryGet(recent.Token, out _).ShouldBeFalse();
|
|
registry.TryGet(throwing.Token, out _).ShouldBeFalse(); // a dispose failure does not strand the rest
|
|
}
|
|
}
|