fix(historian-gateway): map alarm severity numerically; dispose provisioner's gRPC channel

Driver.Historian.Gateway-001 (High): AlarmEventMapper wrote the alarm's
Severity wire property as the .NET enum name (e.g. "High"), but
EventMapper's read side int.TryParse()s that same property and silently
falls back to the minimum severity on any unparseable value - so every
alarm event's severity round-tripped through this module's own mappers
collapsed to 1 (Low), contradicting AlarmHistorianEvent.Severity's own
documented contract ("mapped to Historian's numeric priority"). Now
mapped through a numeric priority mirroring the existing
NativeAlarmProjector.MapSeverity convention (200/500/700/900).

Driver.Historian.Gateway-002 (Low): GatewayTagProvisioner never disposed
its IHistorianGatewayClient, unlike its GatewayHistorianDataSource /
GatewayAlarmHistorianWriter siblings. Now implements IAsyncDisposable so
the DI-owned singleton's gRPC channel closes gracefully at shutdown.

Both TDD: new/corrected tests confirmed RED against the pre-fix code.
This commit is contained in:
Joseph Doherty
2026-08-10 06:53:41 -04:00
parent b990adae55
commit 55b12dc217
4 changed files with 82 additions and 3 deletions
@@ -149,6 +149,27 @@ public sealed class GatewayTagProvisionerTests
Assert.Equal(1, result.Failed); // non-blocking: no throw
}
/// <summary>
/// Driver.Historian.Gateway-002: unlike its siblings (<c>GatewayHistorianDataSource</c>,
/// <c>GatewayAlarmHistorianWriter</c>), <see cref="GatewayTagProvisioner"/> had no dispose path at
/// all — the gRPC channel <c>GatewayHistorian.CreateProvisioner</c> opens was never closed. The
/// provisioner is registered as a process-lifetime DI singleton
/// (<c>AddHistorianProvisioning</c>), and the built-in container disposes any singleton instance
/// it creates that implements <see cref="IAsyncDisposable"/>/<see cref="IDisposable"/> — regardless
/// of the registered service-interface type — so implementing it here is sufficient for a clean
/// shutdown with no other wiring change.
/// </summary>
[Fact]
public async Task DisposeAsync_disposes_underlying_client()
{
var fake = new FakeHistorianGatewayClient();
var p = Provisioner(fake);
await ((IAsyncDisposable)p).DisposeAsync();
Assert.Equal(1, fake.DisposeCallCount);
}
[Fact]
public async Task Ensured_count_reflects_successful_results()
{
@@ -23,12 +23,41 @@ public sealed class AlarmEventMapperTests
Assert.Equal("A1", e.Properties["AlarmId"]);
Assert.Equal("HiHi", e.Properties["AlarmName"]);
Assert.Equal("Activated", e.Properties["EventKind"]);
Assert.Equal("High", e.Properties["Severity"]);
// Severity is the AlarmHistorianEvent.Severity XML doc contract: "Mapped to Historian's
// numeric priority on the sink side" — NOT the .NET enum name. EventMapper.ParseSeverity on
// the read side int.TryParse()s this same property, so a non-numeric string (the old bug:
// alarm.Severity.ToString() == "High") silently always read back as the minimum severity (1).
Assert.Equal("700", e.Properties["Severity"]);
Assert.Equal("operator1", e.Properties["User"]);
Assert.Equal("ack note", e.Properties["Comment"]);
Assert.Equal("Temp high", e.Properties["Message"]);
}
/// <summary>
/// Driver.Historian.Gateway-001 regression: the write side (<see cref="AlarmEventMapper"/>) and
/// the read side (<see cref="EventMapper"/>) must agree on the wire representation of severity —
/// a numeric OPC UA Part 9 priority string, matching the convention already used elsewhere in the
/// codebase for the same enum (<c>NativeAlarmProjector.MapSeverity</c>: Low=200/Medium=500/
/// High=700/Critical=900). Round-tripping through both mappers must reproduce the numeric severity,
/// not collapse to the minimum every time.
/// </summary>
[Theory]
[InlineData(AlarmSeverity.Low, (ushort)200)]
[InlineData(AlarmSeverity.Medium, (ushort)500)]
[InlineData(AlarmSeverity.High, (ushort)700)]
[InlineData(AlarmSeverity.Critical, (ushort)900)]
public void Severity_round_trips_through_EventMapper_as_numeric_priority(AlarmSeverity severity, ushort expected)
{
var a = new AlarmHistorianEvent("A1", "Area/Line/Pump1", "HiHi", "LimitAlarm",
severity, "Activated", "m", "operator1", null,
new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
var wire = AlarmEventMapper.ToHistorianEvent(a);
var readBack = EventMapper.ToHistoricalEvent(wire);
Assert.Equal(expected, readBack.Severity);
}
[Fact]
public void Null_comment_is_omitted_not_null()
{