Phase 3B: Site I/O & Observability — Communication, DCL, Script/Alarm actors, Health, Event Logging

Communication Layer (WP-1–5):
- 8 message patterns with correlation IDs, per-pattern timeouts
- Central/Site communication actors, transport heartbeat config
- Connection failure handling (no central buffering, debug streams killed)

Data Connection Layer (WP-6–14, WP-34):
- Connection actor with Become/Stash lifecycle (Connecting/Connected/Reconnecting)
- OPC UA + LmxProxy adapters behind IDataConnection
- Auto-reconnect, bad quality propagation, transparent re-subscribe
- Write-back, tag path resolution with retry, health reporting
- Protocol extensibility via DataConnectionFactory

Site Runtime (WP-15–25, WP-32–33):
- ScriptActor/ScriptExecutionActor (triggers, concurrent execution, blocking I/O dispatcher)
- AlarmActor/AlarmExecutionActor (ValueMatch/RangeViolation/RateOfChange, in-memory state)
- SharedScriptLibrary (inline execution), ScriptRuntimeContext (API)
- ScriptCompilationService (Roslyn, forbidden API enforcement, execution timeout)
- Recursion limit (default 10), call direction enforcement
- SiteStreamManager (per-subscriber bounded buffers, fire-and-forget)
- Debug view backend (snapshot + stream), concurrency serialization
- Local artifact storage (4 SQLite tables)

Health Monitoring (WP-26–28):
- SiteHealthCollector (thread-safe counters, connection state)
- HealthReportSender (30s interval, monotonic sequence numbers)
- CentralHealthAggregator (offline detection 60s, online recovery)

Site Event Logging (WP-29–31):
- SiteEventLogger (SQLite, 6 event categories, ISO 8601 UTC)
- EventLogPurgeService (30-day retention, 1GB cap)
- EventLogQueryService (filters, keyword search, keyset pagination)

541 tests pass, zero warnings.
This commit is contained in:
Joseph Doherty
2026-03-16 20:57:25 -04:00
parent a3bf0c43f3
commit 389f5a0378
97 changed files with 8308 additions and 127 deletions

View File

@@ -0,0 +1,105 @@
using Akka.Actor;
using Akka.TestKit.Xunit2;
using ScadaLink.Commons.Messages.Communication;
using ScadaLink.Commons.Messages.Deployment;
using ScadaLink.Commons.Messages.DebugView;
using ScadaLink.Commons.Messages.Health;
using ScadaLink.Communication.Actors;
namespace ScadaLink.Communication.Tests;
/// <summary>
/// WP-4: Tests for CentralCommunicationActor message routing.
/// WP-5: Tests for connection failure and failover handling.
/// </summary>
public class CentralCommunicationActorTests : TestKit
{
public CentralCommunicationActorTests()
: base(@"akka.loglevel = DEBUG")
{
}
[Fact]
public void RegisterSite_AllowsMessageRouting()
{
var centralActor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor()));
// Register a site pointing to the test probe
var probe = CreateTestProbe();
centralActor.Tell(new RegisterSite("site1", probe.Ref.Path.ToString()));
// Send a message to the site
var command = new DeployInstanceCommand(
"dep1", "inst1", "hash1", "{}", "admin", DateTimeOffset.UtcNow);
centralActor.Tell(new SiteEnvelope("site1", command));
// The probe should receive the inner message (not the envelope)
probe.ExpectMsg<DeployInstanceCommand>(msg => msg.DeploymentId == "dep1");
}
[Fact]
public void UnregisteredSite_MessageIsDropped()
{
var centralActor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor()));
var command = new DeployInstanceCommand(
"dep1", "inst1", "hash1", "{}", "admin", DateTimeOffset.UtcNow);
centralActor.Tell(new SiteEnvelope("unknown-site", command));
// No crash, no response — the ask will timeout on the caller side
ExpectNoMsg(TimeSpan.FromMilliseconds(200));
}
[Fact]
public void ConnectionLost_DebugStreamsKilled()
{
var centralActor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor()));
var siteProbe = CreateTestProbe();
// Register site
centralActor.Tell(new RegisterSite("site1", siteProbe.Ref.Path.ToString()));
// Subscribe to debug view (this tracks the subscription)
var subscriberProbe = CreateTestProbe();
var subRequest = new SubscribeDebugViewRequest("inst1", "corr-123");
centralActor.Tell(new SiteEnvelope("site1", subRequest), subscriberProbe.Ref);
// Simulate site disconnection
centralActor.Tell(new ConnectionStateChanged("site1", false, DateTimeOffset.UtcNow));
// The subscriber should receive a DebugStreamTerminated notification
subscriberProbe.ExpectMsg<DebugStreamTerminated>(
msg => msg.SiteId == "site1" && msg.CorrelationId == "corr-123");
}
[Fact]
public void ConnectionLost_SiteSelectionRemoved()
{
var centralActor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor()));
var siteProbe = CreateTestProbe();
centralActor.Tell(new RegisterSite("site1", siteProbe.Ref.Path.ToString()));
// Disconnect
centralActor.Tell(new ConnectionStateChanged("site1", false, DateTimeOffset.UtcNow));
// Sending a message to the disconnected site should be dropped
centralActor.Tell(new SiteEnvelope("site1",
new DeployInstanceCommand("dep2", "inst2", "hash2", "{}", "admin", DateTimeOffset.UtcNow)));
siteProbe.ExpectNoMsg(TimeSpan.FromMilliseconds(200));
}
[Fact]
public void Heartbeat_ForwardedToParent()
{
var parentProbe = CreateTestProbe();
var centralActor = parentProbe.ChildActorOf(
Props.Create(() => new CentralCommunicationActor()));
var heartbeat = new HeartbeatMessage("site1", "host1", true, DateTimeOffset.UtcNow);
centralActor.Tell(heartbeat);
parentProbe.ExpectMsg<HeartbeatMessage>(msg => msg.SiteId == "site1");
}
}

View File

@@ -0,0 +1,61 @@
namespace ScadaLink.Communication.Tests;
/// <summary>
/// WP-2: Tests for per-pattern timeout configuration.
/// </summary>
public class CommunicationOptionsTests
{
[Fact]
public void DefaultTimeouts_AreReasonable()
{
var options = new CommunicationOptions();
Assert.Equal(TimeSpan.FromMinutes(2), options.DeploymentTimeout);
Assert.Equal(TimeSpan.FromSeconds(30), options.LifecycleTimeout);
Assert.Equal(TimeSpan.FromMinutes(1), options.ArtifactDeploymentTimeout);
Assert.Equal(TimeSpan.FromSeconds(30), options.QueryTimeout);
Assert.Equal(TimeSpan.FromSeconds(30), options.IntegrationTimeout);
Assert.Equal(TimeSpan.FromSeconds(10), options.DebugViewTimeout);
Assert.Equal(TimeSpan.FromSeconds(10), options.HealthReportTimeout);
}
[Fact]
public void TransportHeartbeat_HasExplicitDefaults()
{
var options = new CommunicationOptions();
// WP-3: Transport heartbeat is explicitly configured, not framework defaults
Assert.Equal(TimeSpan.FromSeconds(5), options.TransportHeartbeatInterval);
Assert.Equal(TimeSpan.FromSeconds(15), options.TransportFailureThreshold);
}
[Fact]
public void DeploymentTimeout_IsLongestPattern()
{
var options = new CommunicationOptions();
Assert.True(options.DeploymentTimeout > options.LifecycleTimeout);
Assert.True(options.DeploymentTimeout > options.QueryTimeout);
Assert.True(options.DeploymentTimeout > options.IntegrationTimeout);
}
[Fact]
public void AllTimeouts_AreConfigurable()
{
var options = new CommunicationOptions
{
DeploymentTimeout = TimeSpan.FromMinutes(5),
LifecycleTimeout = TimeSpan.FromMinutes(1),
ArtifactDeploymentTimeout = TimeSpan.FromMinutes(3),
QueryTimeout = TimeSpan.FromMinutes(1),
IntegrationTimeout = TimeSpan.FromMinutes(1),
DebugViewTimeout = TimeSpan.FromSeconds(30),
HealthReportTimeout = TimeSpan.FromSeconds(30),
TransportHeartbeatInterval = TimeSpan.FromSeconds(2),
TransportFailureThreshold = TimeSpan.FromSeconds(10)
};
Assert.Equal(TimeSpan.FromMinutes(5), options.DeploymentTimeout);
Assert.Equal(TimeSpan.FromSeconds(2), options.TransportHeartbeatInterval);
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace ScadaLink.Communication.Tests;
/// <summary>
/// WP-2: Tests for CommunicationService initialization and state.
/// </summary>
public class CommunicationServiceTests
{
[Fact]
public async Task BeforeInitialization_ThrowsOnUsage()
{
var options = Options.Create(new CommunicationOptions());
var logger = NullLogger<CommunicationService>.Instance;
var service = new CommunicationService(options, logger);
// CommunicationService requires SetCommunicationActor before use
await Assert.ThrowsAsync<InvalidOperationException>(() =>
service.DeployInstanceAsync("site1",
new Commons.Messages.Deployment.DeployInstanceCommand(
"dep1", "inst1", "hash1", "{}", "admin", DateTimeOffset.UtcNow)));
}
[Fact]
public void UnsubscribeDebugView_IsTellNotAsk()
{
// Verify the method signature is void (fire-and-forget Tell pattern)
var method = typeof(CommunicationService).GetMethod("UnsubscribeDebugView");
Assert.NotNull(method);
Assert.Equal(typeof(void), method!.ReturnType);
}
}

View File

@@ -0,0 +1,102 @@
using ScadaLink.Commons.Messages.Integration;
using ScadaLink.Commons.Messages.RemoteQuery;
namespace ScadaLink.Communication.Tests;
/// <summary>
/// WP-1: Tests that message contracts have correlation IDs and proper structure.
/// </summary>
public class MessageContractTests
{
[Fact]
public void IntegrationCallRequest_HasCorrelationId()
{
var msg = new IntegrationCallRequest(
"corr-123", "site1", "inst1", "ExtSys1", "GetData",
new Dictionary<string, object?>(), DateTimeOffset.UtcNow);
Assert.Equal("corr-123", msg.CorrelationId);
}
[Fact]
public void IntegrationCallResponse_HasCorrelationId()
{
var msg = new IntegrationCallResponse(
"corr-123", "site1", true, "{}", null, DateTimeOffset.UtcNow);
Assert.Equal("corr-123", msg.CorrelationId);
}
[Fact]
public void EventLogQueryRequest_HasCorrelationId()
{
var msg = new EventLogQueryRequest(
"corr-456", "site1", null, null, null, null, null, null, null, 25, DateTimeOffset.UtcNow);
Assert.Equal("corr-456", msg.CorrelationId);
}
[Fact]
public void EventLogQueryResponse_HasCorrelationId()
{
var msg = new EventLogQueryResponse(
"corr-456", "site1", [], null, false, true, null, DateTimeOffset.UtcNow);
Assert.Equal("corr-456", msg.CorrelationId);
}
[Fact]
public void ParkedMessageQueryRequest_HasCorrelationId()
{
var msg = new ParkedMessageQueryRequest(
"corr-789", "site1", 1, 25, DateTimeOffset.UtcNow);
Assert.Equal("corr-789", msg.CorrelationId);
}
[Fact]
public void ParkedMessageQueryResponse_HasCorrelationId()
{
var msg = new ParkedMessageQueryResponse(
"corr-789", "site1", [], 0, 1, 25, true, null, DateTimeOffset.UtcNow);
Assert.Equal("corr-789", msg.CorrelationId);
}
[Fact]
public void AllMessagePatterns_ExistAsRecordTypes()
{
// Verify all 8 patterns have proper request/response types
// Pattern 1: Deployment
Assert.True(typeof(Commons.Messages.Deployment.DeployInstanceCommand).IsValueType == false);
Assert.True(typeof(Commons.Messages.Deployment.DeploymentStatusResponse).IsValueType == false);
// Pattern 2: Lifecycle
Assert.True(typeof(Commons.Messages.Lifecycle.DisableInstanceCommand).IsValueType == false);
Assert.True(typeof(Commons.Messages.Lifecycle.InstanceLifecycleResponse).IsValueType == false);
// Pattern 3: Artifacts
Assert.True(typeof(Commons.Messages.Artifacts.DeployArtifactsCommand).IsValueType == false);
Assert.True(typeof(Commons.Messages.Artifacts.ArtifactDeploymentResponse).IsValueType == false);
// Pattern 4: Integration
Assert.True(typeof(IntegrationCallRequest).IsValueType == false);
Assert.True(typeof(IntegrationCallResponse).IsValueType == false);
// Pattern 5: Debug View
Assert.True(typeof(Commons.Messages.DebugView.SubscribeDebugViewRequest).IsValueType == false);
Assert.True(typeof(Commons.Messages.DebugView.DebugViewSnapshot).IsValueType == false);
// Pattern 6: Health
Assert.True(typeof(Commons.Messages.Health.SiteHealthReport).IsValueType == false);
// Pattern 7: Remote Queries
Assert.True(typeof(EventLogQueryRequest).IsValueType == false);
Assert.True(typeof(EventLogQueryResponse).IsValueType == false);
Assert.True(typeof(ParkedMessageQueryRequest).IsValueType == false);
Assert.True(typeof(ParkedMessageQueryResponse).IsValueType == false);
// Pattern 8: Heartbeat
Assert.True(typeof(Commons.Messages.Health.HeartbeatMessage).IsValueType == false);
}
}

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
@@ -9,8 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.5.62" />
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
@@ -21,6 +23,7 @@
<ItemGroup>
<ProjectReference Include="../../src/ScadaLink.Communication/ScadaLink.Communication.csproj" />
<ProjectReference Include="../../src/ScadaLink.Commons/ScadaLink.Commons.csproj" />
</ItemGroup>
</Project>
</Project>

View File

@@ -0,0 +1,104 @@
using Akka.Actor;
using Akka.TestKit.Xunit2;
using ScadaLink.Commons.Messages.Deployment;
using ScadaLink.Commons.Messages.Lifecycle;
using ScadaLink.Commons.Messages.Integration;
using ScadaLink.Commons.Messages.RemoteQuery;
using ScadaLink.Communication.Actors;
namespace ScadaLink.Communication.Tests;
/// <summary>
/// WP-4: Tests for SiteCommunicationActor message routing to local actors.
/// </summary>
public class SiteCommunicationActorTests : TestKit
{
private readonly CommunicationOptions _options = new();
public SiteCommunicationActorTests()
: base(@"akka.loglevel = DEBUG")
{
}
[Fact]
public void DeployCommand_ForwardedToDeploymentManager()
{
var dmProbe = CreateTestProbe();
var siteActor = Sys.ActorOf(Props.Create(() =>
new SiteCommunicationActor("site1", _options, dmProbe.Ref)));
var command = new DeployInstanceCommand(
"dep1", "inst1", "hash1", "{}", "admin", DateTimeOffset.UtcNow);
siteActor.Tell(command);
dmProbe.ExpectMsg<DeployInstanceCommand>(msg => msg.DeploymentId == "dep1");
}
[Fact]
public void LifecycleCommands_ForwardedToDeploymentManager()
{
var dmProbe = CreateTestProbe();
var siteActor = Sys.ActorOf(Props.Create(() =>
new SiteCommunicationActor("site1", _options, dmProbe.Ref)));
siteActor.Tell(new DisableInstanceCommand("cmd1", "inst1", DateTimeOffset.UtcNow));
dmProbe.ExpectMsg<DisableInstanceCommand>();
siteActor.Tell(new EnableInstanceCommand("cmd2", "inst1", DateTimeOffset.UtcNow));
dmProbe.ExpectMsg<EnableInstanceCommand>();
siteActor.Tell(new DeleteInstanceCommand("cmd3", "inst1", DateTimeOffset.UtcNow));
dmProbe.ExpectMsg<DeleteInstanceCommand>();
}
[Fact]
public void IntegrationCall_WithoutHandler_ReturnsFailure()
{
var dmProbe = CreateTestProbe();
var siteActor = Sys.ActorOf(Props.Create(() =>
new SiteCommunicationActor("site1", _options, dmProbe.Ref)));
var request = new IntegrationCallRequest(
"corr1", "site1", "inst1", "ExtSys1", "GetData",
new Dictionary<string, object?>(), DateTimeOffset.UtcNow);
siteActor.Tell(request);
ExpectMsg<IntegrationCallResponse>(msg =>
!msg.Success && msg.ErrorMessage == "Integration handler not available");
}
[Fact]
public void IntegrationCall_WithHandler_ForwardedToHandler()
{
var dmProbe = CreateTestProbe();
var handlerProbe = CreateTestProbe();
var siteActor = Sys.ActorOf(Props.Create(() =>
new SiteCommunicationActor("site1", _options, dmProbe.Ref)));
// Register integration handler
siteActor.Tell(new RegisterLocalHandler(LocalHandlerType.Integration, handlerProbe.Ref));
var request = new IntegrationCallRequest(
"corr1", "site1", "inst1", "ExtSys1", "GetData",
new Dictionary<string, object?>(), DateTimeOffset.UtcNow);
siteActor.Tell(request);
handlerProbe.ExpectMsg<IntegrationCallRequest>(msg => msg.CorrelationId == "corr1");
}
[Fact]
public void EventLogQuery_WithoutHandler_ReturnsFailure()
{
var dmProbe = CreateTestProbe();
var siteActor = Sys.ActorOf(Props.Create(() =>
new SiteCommunicationActor("site1", _options, dmProbe.Ref)));
var request = new EventLogQueryRequest(
"corr1", "site1", null, null, null, null, null, null, null, 25, DateTimeOffset.UtcNow);
siteActor.Tell(request);
ExpectMsg<EventLogQueryResponse>(msg => !msg.Success);
}
}

View File

@@ -1,10 +0,0 @@
namespace ScadaLink.Communication.Tests;
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}