fix(comm): fold post-snapshot debug events into the snapshot and fail fast on a snapshot-request failure (COMM-3, COMM-7, WP4 task 4.3)
This commit is contained in:
@@ -224,6 +224,140 @@ public class DebugStreamServiceTests : TestKit
|
||||
ExpectTerminated(bridgeActor, TimeSpan.FromSeconds(3));
|
||||
}
|
||||
|
||||
// --- COMM-3 / COMM-7 ---
|
||||
|
||||
private (DebugStreamService Service, Akka.TestKit.TestProbe CommProbe, ServiceProvider Provider, IDisposable Factory) NewMockedService()
|
||||
{
|
||||
var instance = new Instance("Site1.Pump01") { Id = 7, SiteId = 3 };
|
||||
var site = new Site("Site One", "site-1")
|
||||
{
|
||||
Id = 3,
|
||||
GrpcNodeAAddress = "http://localhost:5100",
|
||||
GrpcNodeBAddress = "http://localhost:5200"
|
||||
};
|
||||
var instanceRepo = Substitute.For<ITemplateEngineRepository>();
|
||||
instanceRepo.GetInstanceByIdAsync(7, Arg.Any<CancellationToken>()).Returns(instance);
|
||||
var siteRepo = Substitute.For<ISiteRepository>();
|
||||
siteRepo.GetSiteByIdAsync(3, Arg.Any<CancellationToken>()).Returns(site);
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped(_ => instanceRepo);
|
||||
services.AddScoped(_ => siteRepo);
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
var commProbe = CreateTestProbe();
|
||||
var commService = new CommunicationService(
|
||||
Options.Create(new CommunicationOptions()),
|
||||
NullLogger<CommunicationService>.Instance);
|
||||
commService.SetCommunicationActor(commProbe.Ref);
|
||||
|
||||
var grpcFactory = new Grpc.MockSiteStreamGrpcClientFactory(new Grpc.MockSiteStreamGrpcClient());
|
||||
var service = new DebugStreamService(
|
||||
commService, provider, grpcFactory, NullLogger<DebugStreamService>.Instance);
|
||||
service.SetActorSystem(Sys);
|
||||
return (service, commProbe, provider, grpcFactory);
|
||||
}
|
||||
|
||||
private static ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AttributeValueChanged Attr(
|
||||
string name, object? value, DateTimeOffset ts) =>
|
||||
new("Site1.Pump01", "", name, value, "Good", ts);
|
||||
|
||||
/// <summary>
|
||||
/// COMM-3 regression: a gap-window event (captured while the snapshot was being built, newer
|
||||
/// than the snapshot row) must not reach the consumer ahead of the snapshot. It is folded into
|
||||
/// the snapshot StartStreamAsync returns, and only later events are delivered to onEvent.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Comm3_GapWindowEvent_IsFoldedIntoTheSnapshot_NotDeliveredAheadOfIt()
|
||||
{
|
||||
var (service, commProbe, provider, factory) = NewMockedService();
|
||||
using var _p = provider;
|
||||
using var _f = factory;
|
||||
using var _s = service;
|
||||
|
||||
var delivered = new System.Collections.Concurrent.ConcurrentQueue<object>();
|
||||
var t0 = DateTimeOffset.UtcNow;
|
||||
var startTask = service.StartStreamAsync(7, onEvent: e => delivered.Enqueue(e), onTerminated: () => { });
|
||||
|
||||
commProbe.ExpectMsg<SiteEnvelope>(TimeSpan.FromSeconds(5));
|
||||
var bridge = commProbe.LastSender;
|
||||
|
||||
// Arrives on the stream BEFORE the snapshot (stream-first), but is newer than the row.
|
||||
bridge.Tell(Attr("Speed", 42.0, t0.AddSeconds(2)));
|
||||
bridge.Tell(new ZB.MOM.WW.ScadaBridge.Commons.Messages.DebugView.DebugViewSnapshot(
|
||||
"Site1.Pump01",
|
||||
new List<ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AttributeValueChanged> { Attr("Speed", 10.0, t0.AddSeconds(1)) },
|
||||
new List<ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AlarmStateChanged>(),
|
||||
t0.AddSeconds(1)));
|
||||
|
||||
var session = await startTask;
|
||||
|
||||
var speed = Assert.Single(session.InitialSnapshot.AttributeValues);
|
||||
Assert.Equal(42.0, speed.Value);
|
||||
Assert.Empty(delivered);
|
||||
|
||||
// After the hand-over, events pass straight through.
|
||||
bridge.Tell(Attr("Speed", 43.0, t0.AddSeconds(3)));
|
||||
AwaitCondition(() => !delivered.IsEmpty, TimeSpan.FromSeconds(3));
|
||||
var live = Assert.IsType<ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AttributeValueChanged>(Assert.Single(delivered));
|
||||
Assert.Equal(43.0, live.Value);
|
||||
|
||||
service.StopStream(session.SessionId);
|
||||
}
|
||||
|
||||
/// <summary>COMM-3: the fold keeps a newer snapshot row over an older event and appends unseen entities.</summary>
|
||||
[Fact]
|
||||
public void Comm3_FoldIntoSnapshot_RespectsTimestamps_AndAppendsNewEntities()
|
||||
{
|
||||
var t0 = DateTimeOffset.UtcNow;
|
||||
var snapshot = new ZB.MOM.WW.ScadaBridge.Commons.Messages.DebugView.DebugViewSnapshot(
|
||||
"Site1.Pump01",
|
||||
new List<ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AttributeValueChanged> { Attr("A", 1, t0.AddSeconds(5)) },
|
||||
new List<ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AlarmStateChanged>(),
|
||||
t0);
|
||||
|
||||
var folded = DebugStreamService.FoldIntoSnapshot(snapshot, new object[]
|
||||
{
|
||||
Attr("A", 0, t0.AddSeconds(1)), // older than the row: ignored
|
||||
Attr("B", 2, t0.AddSeconds(2)), // not in the snapshot: appended
|
||||
new ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming.AlarmStateChanged(
|
||||
"Site1.Pump01", "Hi", ZB.MOM.WW.ScadaBridge.Commons.Types.Enums.AlarmState.Active, 500, t0.AddSeconds(3)),
|
||||
});
|
||||
|
||||
Assert.Equal(1, folded.AttributeValues.Single(a => a.AttributeName == "A").Value);
|
||||
Assert.Equal(2, folded.AttributeValues.Single(a => a.AttributeName == "B").Value);
|
||||
Assert.Single(folded.AlarmStates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// COMM-7 regression: the command transport's fail-fast <c>Status.Failure</c> reply to the
|
||||
/// snapshot request ends the start immediately with the real cause, instead of the 30 s
|
||||
/// timeout, and tears the bridge actor down.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Comm7_SnapshotRequestFailure_FailsStartFastWithTheCause()
|
||||
{
|
||||
var (service, commProbe, provider, factory) = NewMockedService();
|
||||
using var _p = provider;
|
||||
using var _f = factory;
|
||||
using var _s = service;
|
||||
|
||||
var terminated = false;
|
||||
var startTask = service.StartStreamAsync(7, onEvent: _ => { }, onTerminated: () => terminated = true);
|
||||
commProbe.ExpectMsg<SiteEnvelope>(TimeSpan.FromSeconds(5));
|
||||
var bridge = commProbe.LastSender;
|
||||
Watch(bridge);
|
||||
|
||||
bridge.Tell(new Status.Failure(new InvalidOperationException("no gRPC channel for site site-1")));
|
||||
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => startTask.WaitAsync(TimeSpan.FromSeconds(5)));
|
||||
Assert.Contains("no gRPC channel for site site-1", ex.Message);
|
||||
Assert.True(terminated);
|
||||
ExpectTerminated(bridge, TimeSpan.FromSeconds(3));
|
||||
Assert.Equal(0, service.SessionCount);
|
||||
}
|
||||
|
||||
// --- single-endpoint sites ---
|
||||
|
||||
/// <summary>Verifies that a single-endpoint site uses its sole address for both node slots.</summary>
|
||||
|
||||
Reference in New Issue
Block a user