Files
scadalink-design/tests/ScadaLink.Communication.Tests/CentralCommunicationActorTests.cs
Joseph Doherty 389f5a0378 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.
2026-03-16 20:57:25 -04:00

106 lines
3.9 KiB
C#

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");
}
}