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

@@ -8,6 +8,7 @@ using ScadaLink.Commons.Types.Enums;
using ScadaLink.Commons.Types.Flattening;
using ScadaLink.SiteRuntime.Actors;
using ScadaLink.SiteRuntime.Persistence;
using ScadaLink.SiteRuntime.Scripts;
using System.Text.Json;
namespace ScadaLink.SiteRuntime.Tests.Actors;
@@ -19,6 +20,8 @@ namespace ScadaLink.SiteRuntime.Tests.Actors;
public class DeploymentManagerActorTests : TestKit, IDisposable
{
private readonly SiteStorageService _storage;
private readonly ScriptCompilationService _compilationService;
private readonly SharedScriptLibrary _sharedScriptLibrary;
private readonly string _dbFile;
public DeploymentManagerActorTests()
@@ -28,6 +31,10 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
$"Data Source={_dbFile}",
NullLogger<SiteStorageService>.Instance);
_storage.InitializeAsync().GetAwaiter().GetResult();
_compilationService = new ScriptCompilationService(
NullLogger<ScriptCompilationService>.Instance);
_sharedScriptLibrary = new SharedScriptLibrary(
_compilationService, NullLogger<SharedScriptLibrary>.Instance);
}
void IDisposable.Dispose()
@@ -36,6 +43,18 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
try { File.Delete(_dbFile); } catch { /* cleanup */ }
}
private IActorRef CreateDeploymentManager(SiteRuntimeOptions? options = null)
{
options ??= new SiteRuntimeOptions();
return ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage,
_compilationService,
_sharedScriptLibrary,
null, // no stream manager in tests
options,
NullLogger<DeploymentManagerActor>.Instance)));
}
private static string MakeConfigJson(string instanceName)
{
var config = new FlattenedConfiguration
@@ -56,14 +75,13 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
await _storage.StoreDeployedConfigAsync("Pump1", MakeConfigJson("Pump1"), "d1", "h1", true);
await _storage.StoreDeployedConfigAsync("Pump2", MakeConfigJson("Pump2"), "d2", "h2", true);
var options = new SiteRuntimeOptions { StartupBatchSize = 100, StartupBatchDelayMs = 10 };
var actor = ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage, options, NullLogger<DeploymentManagerActor>.Instance)));
var actor = CreateDeploymentManager(
new SiteRuntimeOptions { StartupBatchSize = 100, StartupBatchDelayMs = 10 });
// Allow time for async startup (load configs + create actors)
await Task.Delay(2000);
// Verify by deploying if actors already exist, we'd get a warning
// Verify by deploying -- if actors already exist, we'd get a warning
// Instead, verify by checking we can send lifecycle commands
actor.Tell(new DisableInstanceCommand("cmd-1", "Pump1", DateTimeOffset.UtcNow));
var response = ExpectMsg<InstanceLifecycleResponse>(TimeSpan.FromSeconds(5));
@@ -77,14 +95,13 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
await _storage.StoreDeployedConfigAsync("Active1", MakeConfigJson("Active1"), "d1", "h1", true);
await _storage.StoreDeployedConfigAsync("Disabled1", MakeConfigJson("Disabled1"), "d2", "h2", false);
var options = new SiteRuntimeOptions { StartupBatchSize = 100, StartupBatchDelayMs = 10 };
var actor = ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage, options, NullLogger<DeploymentManagerActor>.Instance)));
var actor = CreateDeploymentManager(
new SiteRuntimeOptions { StartupBatchSize = 100, StartupBatchDelayMs = 10 });
await Task.Delay(2000);
// The disabled instance should NOT have an actor running
// Try to disable it it should succeed (no actor to stop, but SQLite update works)
// Try to disable it -- it should succeed (no actor to stop, but SQLite update works)
actor.Tell(new DisableInstanceCommand("cmd-2", "Disabled1", DateTimeOffset.UtcNow));
var response = ExpectMsg<InstanceLifecycleResponse>(TimeSpan.FromSeconds(5));
Assert.True(response.Success);
@@ -101,9 +118,8 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
}
// Use a small batch size to force multiple batches
var options = new SiteRuntimeOptions { StartupBatchSize = 2, StartupBatchDelayMs = 50 };
var actor = ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage, options, NullLogger<DeploymentManagerActor>.Instance)));
var actor = CreateDeploymentManager(
new SiteRuntimeOptions { StartupBatchSize = 2, StartupBatchDelayMs = 50 });
// Wait for all batches to complete (3 batches with 50ms delay = ~150ms + processing)
await Task.Delay(3000);
@@ -120,9 +136,7 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
[Fact]
public async Task DeploymentManager_Deploy_CreatesNewInstance()
{
var options = new SiteRuntimeOptions();
var actor = ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage, options, NullLogger<DeploymentManagerActor>.Instance)));
var actor = CreateDeploymentManager();
await Task.Delay(500); // Wait for empty startup
@@ -137,9 +151,7 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
[Fact]
public async Task DeploymentManager_Lifecycle_DisableEnableDelete()
{
var options = new SiteRuntimeOptions();
var actor = ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage, options, NullLogger<DeploymentManagerActor>.Instance)));
var actor = CreateDeploymentManager();
await Task.Delay(500);
@@ -150,7 +162,6 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
ExpectMsg<DeploymentStatusResponse>(TimeSpan.FromSeconds(5));
// Wait for the async deploy persistence (PipeTo) to complete
// The deploy handler replies immediately but persists asynchronously
await Task.Delay(1000);
// Disable
@@ -179,15 +190,9 @@ public class DeploymentManagerActorTests : TestKit, IDisposable
[Fact]
public void DeploymentManager_SupervisionStrategy_ResumesOnException()
{
// Verify the supervision strategy by creating the actor and checking
// that it uses OneForOneStrategy
var options = new SiteRuntimeOptions();
var actor = ActorOf(Props.Create(() => new DeploymentManagerActor(
_storage, options, NullLogger<DeploymentManagerActor>.Instance)));
var actor = CreateDeploymentManager();
// The actor exists and is responsive supervision is configured
// The actual Resume behavior is verified implicitly: if an Instance Actor
// throws during message handling, it resumes rather than restarting
// The actor exists and is responsive -- supervision is configured
actor.Tell(new DeployInstanceCommand(
"dep-sup", "SupervisedPump", "sha256:sup",
MakeConfigJson("SupervisedPump"), "admin", DateTimeOffset.UtcNow));