diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/AlarmCommandHandlerTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/AlarmCommandHandlerTests.cs index 597179b5..1a642c81 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/AlarmCommandHandlerTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/AlarmCommandHandlerTests.cs @@ -644,6 +644,52 @@ public sealed class AlarmCommandHandlerTests Assert.Equal("33.AckMsg", AckItemOf(handler)); } + /// + /// MXA-010: adding one alarm to a fallback watch list advises only that alarm's subtags on + /// the existing subtag source. Rebuilding the standby instead created a second source (a + /// second MXAccess server registration), re-advised every watched subtag on it, and + /// unregistered the first — work proportional to the whole watch list for a one-item change. + /// + [Fact] + public void UpdateWatchList_OneTargetAdded_AdvisesOnlyTheAddedSubtagsOnTheExistingSource() + { + List sources = new List(); + AlarmCommandHandler handler = new AlarmCommandHandler( + new MxAccessEventQueue(), + () => new FakeConsumer(), + threadAffinityCheck: null, + comFactory: null, + standbyFactory: watchList => + { + CountingSubtagSource source = new CountingSubtagSource(); + sources.Add(source); + return new SubtagAlarmConsumer(source, watchList); + }, + ackWriterFactory: () => new RecordingAckWriter()); + SubscribeAlarmsCommand subscribe = new SubscribeAlarmsCommand + { + SubscriptionExpression = @"\\HOST\Galaxy!Area", + FallbackEnabled = true, + }; + List watchList = new List(); + for (int i = 0; i < 50; i++) + { + watchList.Add(NewTarget($"Galaxy!Area.Tank{i:D2}.Alarm")); + } + + subscribe.WatchList.AddRange(watchList); + handler.Subscribe(subscribe, "s1"); + Assert.Single(sources); + Assert.Equal(100, sources[0].AdvisedCount); + + watchList.Add(NewTarget("Galaxy!Area.Tank50.Alarm")); + handler.UpdateWatchList(watchList); + + Assert.Single(sources); + Assert.False(sources[0].Disposed); + Assert.Equal(102, sources[0].AdvisedCount); + } + /// /// A non-failover subscribe (alarmmgr-only) never enqueues a /// provider-mode-changed event, and a subsequent Unsubscribe detaches @@ -793,6 +839,39 @@ public sealed class AlarmCommandHandlerTests } } + /// Subtag source that counts the addresses it is asked to advise. + private sealed class CountingSubtagSource : ISubtagAlarmSource + { +#pragma warning disable CS0067 // Event never invoked — these tests count advise work, not transitions. + /// + public event EventHandler? ValueChanged; +#pragma warning restore CS0067 + + /// Gets the addresses currently advised. + public HashSet Advised { get; } = new HashSet(StringComparer.Ordinal); + + /// Gets the total number of advise operations performed (AddItem + Advise pairs). + public int AdvisedCount { get; private set; } + + /// Gets a value indicating whether the source was disposed. + public bool Disposed { get; private set; } + + /// + public void Advise(IReadOnlyCollection itemAddresses) + { + foreach (string address in itemAddresses) + { + if (Advised.Add(address)) + { + AdvisedCount++; + } + } + } + + /// + public void Dispose() => Disposed = true; + } + private sealed class RecordingAckWriter : IAlarmAckWriter { public string? LastItemAddress { get; private set; }