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; /// /// ScadaBridge#103: a peer central node's drops this /// node's compiled handler, known-bad record and cached row for that method only. /// public class InboundApiCacheInvalidationHandlerTests { private readonly InboundScriptExecutor _executor = new(NullLogger.Instance, Substitute.For()); private readonly ApiMethodCache _cache = new(TimeSpan.FromHours(1)); private async Task Refetches(string name) { var fetched = false; await _cache.GetOrFetchAsync(name, _ => { fetched = true; return Task.FromResult(null); }, CancellationToken.None); return fetched; } /// The named method's known-bad record, handler and row are dropped; others are kept. /// A task that represents the asynchronous operation. [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(bad), CancellationToken.None); await _cache.GetOrFetchAsync("Good", _ => Task.FromResult(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")); } }