fix(drivers): complete the Sql + FOCAS in-place config re-parse (#516)

The first #516 pass deliberately left these two out, because each derives more
than options from config: Sql its ISqlDialect and resolved connection string,
FOCAS the IFocasClientFactory its Backend key selects — all injected at
construction. Adopting new options alone would poll a NEW tag set through the
OLD database/backend, and a half-applied config change is worse than a
discarded one because it looks like it worked.

Rather than accept that exclusion, this fixes the blocker. Each factory now
exposes a ParseBinding returning EVERY config-derived dependency as one value,
and the driver adopts them atomically:

- Sql: ApplyBinding takes (options, dialect, connectionString) and rebuilds
  everything downstream — provider factory, Endpoint, and the SqlPollReader
  that captures all three. It runs BEFORE BuildTagTable, so the tag table and
  the connection it is polled over always come from the same revision. A
  test-injected DbProviderFactory survives a rebind (_explicitFactory), so a
  re-derived dialect cannot displace what a test passed in.
- FOCAS: options and backend move as a pair in InitializeAsync.

Re-resolving Sql's connection string on reinit is a side benefit: a rotated
credential is picked up without a process restart.

Drivers constructed directly get no rebinder and keep their constructor
binding, so every existing "{}"-passing lifecycle test is unaffected by
construction rather than by luck.

The tests pin ATOMICITY, not merely that a re-parse happened — a test checking
only options would have passed against the broken version. Confirmed by
simulating the half-fix (adopt options, skip the backend): 2 of 3 FOCAS tests
go red. Reverting Sql's rebind turns its connection-string test red. Sql also
asserts that a reinit which cannot resolve its new connection leaves the
previous binding whole rather than half-adopting.

All 12 drivers now honour a changed config in place. Sql.Tests 226, FOCAS.Tests
275 pass.
This commit is contained in:
Joseph Doherty
2026-07-28 00:30:52 -04:00
parent 88ce8df099
commit 5184a2e107
7 changed files with 467 additions and 62 deletions
@@ -0,0 +1,67 @@
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests;
/// <summary>
/// Gitea #516 — FOCAS was excluded from the first pass because it derives MORE than options from config:
/// the <c>Backend</c> key selects its <see cref="IFocasClientFactory"/>, injected at construction. Adopting
/// a new <c>FocasDriverOptions</c> alone would poll a NEW device/tag set through the OLD backend, which
/// looks like it worked and does not.
/// <para><c>ParseBinding</c> now returns options + backend together and <c>InitializeAsync</c> adopts both
/// or neither. These tests pin the BACKEND moving — the half that was missing — because a test that only
/// checked options would have passed against the broken version.</para>
/// </summary>
[Trait("Category", "Unit")]
public sealed class FocasReinitConfigAdoptionTests
{
/// <summary>
/// Reinitializing onto <c>Backend: "unimplemented"</c> must make the driver adopt that backend, whose
/// <c>EnsureUsable()</c> throws by design. A driver still holding the constructor's <c>wire</c>
/// backend would not throw — so the throw is a direct read of which backend is live.
/// </summary>
[Fact]
public async Task Reinitialize_adopts_a_changed_backend_not_just_the_options()
{
var driver = FocasDriverFactoryExtensions.CreateInstance(
"focas-1", """{"backend":"wire","devices":[]}""");
var ex = await Record.ExceptionAsync(() => driver.ReinitializeAsync(
"""{"backend":"unimplemented","devices":[]}""", CancellationToken.None));
ex.ShouldBeOfType<NotSupportedException>(
"FocasDriver adopted a reinitialized config but kept its ORIGINAL backend — the half-applied "
+ "adoption (#516) that would poll a new device/tag set through the old backend");
}
/// <summary>The other direction: moving OFF the unimplemented backend must also take effect, so the
/// adoption is not a one-way latch.</summary>
[Fact]
public async Task Reinitialize_away_from_the_unimplemented_backend_also_takes_effect()
{
var driver = FocasDriverFactoryExtensions.CreateInstance(
"focas-1", """{"backend":"unimplemented","devices":[]}""");
// Sanity: the constructor-supplied backend is the throwing one.
(await Record.ExceptionAsync(() => driver.InitializeAsync("{}", CancellationToken.None)))
.ShouldBeOfType<NotSupportedException>();
var ex = await Record.ExceptionAsync(() => driver.ReinitializeAsync(
"""{"backend":"wire","devices":[]}""", CancellationToken.None));
ex.ShouldNotBeOfType<NotSupportedException>(
"FocasDriver stayed on the unimplemented backend after a reinitialize that selected 'wire'");
}
/// <summary>An empty/placeholder document keeps the constructor-supplied pair, so the lifecycle tests
/// that pass <c>"{}"</c> keep meaning what they meant.</summary>
[Fact]
public async Task Reinitialize_with_an_empty_document_keeps_the_constructor_backend()
{
var driver = FocasDriverFactoryExtensions.CreateInstance(
"focas-1", """{"backend":"unimplemented","devices":[]}""");
(await Record.ExceptionAsync(() => driver.ReinitializeAsync("{}", CancellationToken.None)))
.ShouldBeOfType<NotSupportedException>();
}
}