Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.InboundAPI.Tests/InboundApiCacheInvalidationHandlerTests.cs
T
Joseph Doherty 0c2e852a62 fix(inbound-api): API-method recompile/invalidate reaches the peer central node; stale doc; locked-override name rule (ScadaBridge#103)
(a) Inbound API compile state (#75) is per central node: a recompile cleared the
known-bad record only on the node that answered, and the peer kept refusing the
unchanged script with SCRIPT_COMPILE_FAILED (the per-request revision check only
notices a changed script). Create / update / delete (ManagementActor post-commit),
RecompileApiMethod, and API methods written by a Transport bundle import now publish
InboundApiMethodInvalidated on the central cache-invalidation topic (#100 item 3).
The new InboundApiCacheInvalidationHandler drops that method's compiled handler,
known-bad record and cached ApiMethod row on the peer, which compiles the current
text on its next request.

(b) docs/components/InboundAPI.md no longer claims a direct DB edit stays stale
until restart: the per-request revision check picks it up once the node's cached
row expires (ApiMethodCacheTtl, 5 s). Component-InboundAPI, Component-Host and the
#85 cache audit describe the peer invalidation.

(c) Decision: the import's locked-override warnings keep EXACT (ordinal) name
matching, because FlatteningService matches override names ordinally. An override
differing from a locked member only by case never reaches that member's lock check
(the flattener drops it as unresolved), so a "targets a LOCKED member" warning for it
would be false. The comparer is now named (LockedOverrideNameComparer) and documented,
and pinned by tests on both sides (flattener + import).

Tests: the peer's known-bad record is cleared by a recompile (and validate leaves it
alone); create/update/delete and the import publish the invalidation; the handler
drops only the named method; composition root registers the handler; case-only
override gets no lock warning; the flattener matches override names exactly.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-09 16:04:28 -05:00

47 lines
2.1 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Communication;
namespace ZB.MOM.WW.ScadaBridge.InboundAPI.Tests;
/// <summary>
/// ScadaBridge#103: a peer central node's <see cref="InboundApiMethodInvalidated"/> drops this
/// node's compiled handler, known-bad record and cached row for that method only.
/// </summary>
public class InboundApiCacheInvalidationHandlerTests
{
private readonly InboundScriptExecutor _executor =
new(NullLogger<InboundScriptExecutor>.Instance, Substitute.For<IServiceProvider>());
private readonly ApiMethodCache _cache = new(TimeSpan.FromHours(1));
private async Task<bool> Refetches(string name)
{
var fetched = false;
await _cache.GetOrFetchAsync(name, _ => { fetched = true; return Task.FromResult<ApiMethod?>(null); }, CancellationToken.None);
return fetched;
}
/// <summary>The named method's known-bad record, handler and row are dropped; others are kept.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Apply_DropsOnlyTheNamedMethod()
{
var bad = new ApiMethod("Bad", "return doesNotExist;");
var good = new ApiMethod("Good", "return 1;");
Assert.False(_executor.CompileAndRegister(bad));
Assert.True(_executor.CompileAndRegister(good));
await _cache.GetOrFetchAsync("Bad", _ => Task.FromResult<ApiMethod?>(bad), CancellationToken.None);
await _cache.GetOrFetchAsync("Good", _ => Task.FromResult<ApiMethod?>(good), CancellationToken.None);
var handler = new InboundApiCacheInvalidationHandler(_executor, _cache);
handler.Apply(new InboundApiMethodInvalidated("Bad"));
handler.Apply(new RoleMappingsInvalidated()); // not ours: ignored
Assert.False(_executor.GetCompileState("Bad", bad.Script).IsKnownBad);
Assert.True(await Refetches("Bad"));
Assert.True(_executor.GetCompileState("Good", good.Script).IsCompiled);
Assert.False(await Refetches("Good"));
}
}