using System.Diagnostics; using System.Text.Json; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Wire; namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.IntegrationTests; /// /// grpc-data A1 exit gate L4 — FOCAS leg: against the live focas-mock, the latency from InitializeAsync returning /// (the host's quality-Good edge) to the first /// (first successful cnc_rdalmmsg2 tick, default 1 s alarm poll cadence). N samples, each a fresh driver. /// Read-only: the mock's alarm list is read, never scheduled. Results: OTOPCUA_L4_RESULTS when set. /// [Collection(FocasSimCollection.Name)] [Trait("Category", "LiveIntegration")] [Trait("Feature", "GrpcData")] [Trait("Phase", "L4")] public sealed class FocasAlarmSignalLatencyTests(FocasSimFixture fx, ITestOutputHelper output) { private const int Samples = 20; [Fact] public async Task First_alarm_read_signal_latency_from_connected() { if (fx.SkipReason is not null) Assert.Skip(fx.SkipReason); var ct = TestContext.Current.CancellationToken; const string rawPath = "Plant/Cnc/l4/alarms"; var latencies = new List(); var withConnect = new List(); for (var i = 0; i < Samples; i++) { var driver = new FocasDriver(new FocasDriverOptions { Devices = [new FocasDeviceOptions(fx.DeviceUri, DeviceName: "l4")], RawTags = [ new RawTagEntry(rawPath, JsonSerializer.Serialize(new Dictionary { ["address"] = "ALARM:*", ["dataType"] = "Int32", ["alarm"] = new Dictionary { ["alarmType"] = "AlarmCondition" }, }), WriteIdempotent: false, DeviceName: "l4"), ], Probe = new FocasProbeOptions { Enabled = false }, }, driverInstanceId: $"focas-l4-{i}", clientFactory: new WireFocasClientFactory()); await using (driver) { var signalled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); driver.InitialAlarmStateDelivered += (_, e) => signalled.TrySetResult(e); driver.OnAlarmEvent += (_, _) => { }; var total = Stopwatch.StartNew(); await driver.InitializeAsync("{}", ct); var sw = Stopwatch.StartNew(); await driver.SubscribeAlarmsAsync([rawPath], ct); var args = await signalled.Task.WaitAsync(TimeSpan.FromSeconds(30), ct); sw.Stop(); args.ConditionIds.ShouldBe([rawPath]); latencies.Add(sw.Elapsed.TotalMilliseconds); withConnect.Add(total.Elapsed.TotalMilliseconds); await driver.ShutdownAsync(ct); } } var sorted = latencies.OrderBy(x => x).ToArray(); var sc = withConnect.OrderBy(x => x).ToArray(); double Pc(double q) => sc[Math.Clamp((int)Math.Ceiling(q * sc.Length) - 1, 0, sc.Length - 1)]; double P(double q) => sorted[Math.Clamp((int)Math.Ceiling(q * sorted.Length) - 1, 0, sorted.Length - 1)]; var line = JsonSerializer.Serialize(new { driverType = "FOCAS", fixture = fx.DeviceUri, n = sorted.Length, p50 = P(0.50), p99 = P(0.99), max = sorted[^1], samplesMs = latencies.Select(x => Math.Round(x, 1)), withConnectP50 = Pc(0.50), withConnectP99 = Pc(0.99), }); output.WriteLine(line); if (Environment.GetEnvironmentVariable("OTOPCUA_L4_RESULTS") is { Length: > 0 } path) File.AppendAllText(path, line + Environment.NewLine); } }