Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverSpawnPlannerTests.cs
Joseph Doherty da141497f8 feat(runtime): F7 spawn lifecycle + F20 ShouldStub gate
DriverHostActor.ApplyAndAck now reads the deployment artifact and
reconciles its set of DriverInstanceActor children — spawn the missing,
ApplyDelta to those with changed config, stop the removed/disabled.
The diff lives in pure DriverSpawnPlanner so it can be unit-tested
without an ActorSystem.

Adds IDriverFactory in Core.Abstractions (consumed by Runtime) +
DriverFactoryRegistryAdapter in Core.Hosting that wraps the existing
v1 DriverFactoryRegistry — Runtime stays decoupled from Polly/Serilog,
the Host wires the adapter once driver assemblies have registered.

ShouldStub(type, roles) is now actually called on every spawn — Galaxy
+ Wonderware-Historian boot stubbed on macOS/Linux or whenever the host
carries the dev role. Missing factory ⇒ stub fallback, never a crash.

Tests: 24 → 34 in Runtime (+10):
- DriverSpawnPlannerTests x7 (diff cases, type change ⇒ stop+respawn)
- DeploymentArtifactTests  x5 (empty/malformed/missing fields tolerant)
- DriverHostActorReconcileTests x4 (spawn count, stub fallback,
  ShouldStub gate, second-apply stops the removed)
All 6 v2 test suites green: 120 tests passing.

Closes F20 (ShouldStub wired). F7 marked partial — subscription
publishing + write path still stubbed in DriverInstanceActor itself.
2026-05-26 08:57:16 -04:00

119 lines
3.5 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
public sealed class DriverSpawnPlannerTests
{
private static DriverInstanceSpec Spec(string id, string type = "Modbus", string config = "{\"host\":\"127.0.0.1\"}", bool enabled = true) =>
new(Guid.NewGuid(), id, id, type, enabled, config);
[Fact]
public void All_new_drivers_go_into_ToSpawn_when_current_is_empty()
{
var current = new Dictionary<string, DriverChildSnapshot>();
var target = new[] { Spec("a"), Spec("b") };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToSpawn.Count.ShouldBe(2);
plan.ToApplyDelta.ShouldBeEmpty();
plan.ToStop.ShouldBeEmpty();
}
[Fact]
public void Same_config_yields_empty_plan()
{
var current = new Dictionary<string, DriverChildSnapshot>
{
["a"] = new("Modbus", "{\"host\":\"127.0.0.1\"}"),
};
var target = new[] { Spec("a") };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToSpawn.ShouldBeEmpty();
plan.ToApplyDelta.ShouldBeEmpty();
plan.ToStop.ShouldBeEmpty();
}
[Fact]
public void Different_config_routes_to_ApplyDelta()
{
var current = new Dictionary<string, DriverChildSnapshot>
{
["a"] = new("Modbus", "{\"host\":\"old\"}"),
};
var target = new[] { Spec("a", config: "{\"host\":\"new\"}") };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToApplyDelta.Single().DriverInstanceId.ShouldBe("a");
plan.ToSpawn.ShouldBeEmpty();
plan.ToStop.ShouldBeEmpty();
}
[Fact]
public void Removed_driver_routes_to_ToStop()
{
var current = new Dictionary<string, DriverChildSnapshot>
{
["a"] = new("Modbus", "{\"host\":\"127.0.0.1\"}"),
["b"] = new("Modbus", "{}"),
};
var target = new[] { Spec("a") };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToStop.ShouldBe(new[] { "b" });
plan.ToSpawn.ShouldBeEmpty();
plan.ToApplyDelta.ShouldBeEmpty();
}
[Fact]
public void Disabled_driver_with_running_child_routes_to_ToStop()
{
var current = new Dictionary<string, DriverChildSnapshot>
{
["a"] = new("Modbus", "{}"),
};
var target = new[] { Spec("a", enabled: false) };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToStop.Single().ShouldBe("a");
plan.ToSpawn.ShouldBeEmpty();
plan.ToApplyDelta.ShouldBeEmpty();
}
[Fact]
public void Disabled_new_driver_is_not_spawned()
{
var current = new Dictionary<string, DriverChildSnapshot>();
var target = new[] { Spec("a", enabled: false) };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToSpawn.ShouldBeEmpty();
plan.ToApplyDelta.ShouldBeEmpty();
plan.ToStop.ShouldBeEmpty();
}
[Fact]
public void Driver_type_change_triggers_stop_plus_respawn()
{
var current = new Dictionary<string, DriverChildSnapshot>
{
["a"] = new("Modbus", "{}"),
};
var target = new[] { Spec("a", type: "AbCip") };
var plan = DriverSpawnPlanner.Compute(current, target);
plan.ToStop.Single().ShouldBe("a");
plan.ToSpawn.Single().DriverType.ShouldBe("AbCip");
plan.ToApplyDelta.ShouldBeEmpty();
}
}