Group all 69 projects into category subfolders under src/ and tests/ so the Rider Solution Explorer mirrors the module structure. Folders: Core, Server, Drivers (with a nested Driver CLIs subfolder), Client, Tooling. - Move every project folder on disk with git mv (history preserved as renames). - Recompute relative paths in 57 .csproj files: cross-category ProjectReferences, the lib/ HintPath+None refs in Driver.Historian.Wonderware, and the external mxaccessgw refs in Driver.Galaxy and its test project. - Rebuild ZB.MOM.WW.OtOpcUa.slnx with nested solution folders. - Re-prefix project paths in functional scripts (e2e, compliance, smoke SQL, integration, install). Build green (0 errors); unit tests pass. Docs left for a separate pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
151 lines
5.8 KiB
C#
151 lines
5.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.Server.Hosting;
|
|
using ZB.MOM.WW.OtOpcUa.Server.Redundancy;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Server.Tests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for <see cref="GenerationRefreshHostedService"/>. Exercises the
|
|
/// lease-around-refresh semantics via a stub generation-query delegate — the real
|
|
/// DB path is exercised end-to-end by the Phase 6.3 compliance script.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class GenerationRefreshHostedServiceTests : IDisposable
|
|
{
|
|
private readonly OtOpcUaConfigDbContext _db;
|
|
private readonly IDbContextFactory<OtOpcUaConfigDbContext> _dbFactory;
|
|
|
|
public GenerationRefreshHostedServiceTests()
|
|
{
|
|
var opts = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
|
.UseInMemoryDatabase($"gen-refresh-{Guid.NewGuid():N}")
|
|
.Options;
|
|
_db = new OtOpcUaConfigDbContext(opts);
|
|
_dbFactory = new DbContextFactory(opts);
|
|
}
|
|
|
|
public void Dispose() => _db.Dispose();
|
|
|
|
[Fact]
|
|
public async Task First_tick_applies_current_generation_and_closes_the_lease()
|
|
{
|
|
var coordinator = await SeedCoordinatorAsync();
|
|
var leases = new ApplyLeaseRegistry();
|
|
var service = NewService(coordinator, leases, currentGeneration: () => 42);
|
|
|
|
leases.IsApplyInProgress.ShouldBeFalse("no lease before first tick");
|
|
await service.TickAsync(CancellationToken.None);
|
|
|
|
service.LastAppliedGenerationId.ShouldBe(42);
|
|
service.TickCount.ShouldBe(1);
|
|
service.RefreshCount.ShouldBe(1);
|
|
leases.IsApplyInProgress.ShouldBeFalse("lease must be disposed after the apply window");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Subsequent_tick_with_same_generation_is_a_no_op()
|
|
{
|
|
var coordinator = await SeedCoordinatorAsync();
|
|
var leases = new ApplyLeaseRegistry();
|
|
var service = NewService(coordinator, leases, currentGeneration: () => 42);
|
|
|
|
await service.TickAsync(CancellationToken.None);
|
|
await service.TickAsync(CancellationToken.None);
|
|
|
|
service.TickCount.ShouldBe(2);
|
|
service.RefreshCount.ShouldBe(1, "second identical tick must skip the refresh");
|
|
leases.IsApplyInProgress.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Generation_change_triggers_new_refresh()
|
|
{
|
|
var coordinator = await SeedCoordinatorAsync();
|
|
var leases = new ApplyLeaseRegistry();
|
|
var current = 42L;
|
|
var service = NewService(coordinator, leases, currentGeneration: () => current);
|
|
|
|
await service.TickAsync(CancellationToken.None);
|
|
current = 43L;
|
|
await service.TickAsync(CancellationToken.None);
|
|
|
|
service.LastAppliedGenerationId.ShouldBe(43);
|
|
service.RefreshCount.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Null_generation_means_no_published_config_yet_and_does_not_apply()
|
|
{
|
|
var coordinator = await SeedCoordinatorAsync();
|
|
var leases = new ApplyLeaseRegistry();
|
|
var service = NewService(coordinator, leases, currentGeneration: () => null);
|
|
|
|
await service.TickAsync(CancellationToken.None);
|
|
|
|
service.LastAppliedGenerationId.ShouldBeNull();
|
|
service.RefreshCount.ShouldBe(0);
|
|
service.TickCount.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Lease_is_opened_during_the_refresh_window()
|
|
{
|
|
// Drive a query delegate that *also* observes lease state mid-call: the delegate
|
|
// fires before BeginApplyLease, so it sees IsApplyInProgress=false here, not
|
|
// during the lease window. We observe the lease from the outside by checking
|
|
// OpenLeaseCount on completion — if the `await using` mis-disposed we'd see 1
|
|
// dangling. Cleanest assertion in a stub-only world.
|
|
var coordinator = await SeedCoordinatorAsync();
|
|
var leases = new ApplyLeaseRegistry();
|
|
var service = NewService(coordinator, leases, currentGeneration: () => 42);
|
|
|
|
await service.TickAsync(CancellationToken.None);
|
|
|
|
leases.OpenLeaseCount.ShouldBe(0, "IAsyncDisposable dispose must fire regardless of outcome");
|
|
}
|
|
|
|
// ---- fixture helpers ---------------------------------------------------
|
|
|
|
private async Task<RedundancyCoordinator> SeedCoordinatorAsync()
|
|
{
|
|
_db.ServerClusters.Add(new ServerCluster
|
|
{
|
|
ClusterId = "c1", Name = "W", Enterprise = "zb", Site = "w",
|
|
RedundancyMode = RedundancyMode.None, CreatedBy = "test",
|
|
});
|
|
_db.ClusterNodes.Add(new ClusterNode
|
|
{
|
|
NodeId = "A", ClusterId = "c1",
|
|
RedundancyRole = RedundancyRole.Primary, Host = "a",
|
|
ApplicationUri = "urn:A", CreatedBy = "test",
|
|
});
|
|
await _db.SaveChangesAsync();
|
|
|
|
var coordinator = new RedundancyCoordinator(
|
|
_dbFactory, NullLogger<RedundancyCoordinator>.Instance, "A", "c1");
|
|
await coordinator.InitializeAsync(CancellationToken.None);
|
|
return coordinator;
|
|
}
|
|
|
|
private static GenerationRefreshHostedService NewService(
|
|
RedundancyCoordinator coordinator,
|
|
ApplyLeaseRegistry leases,
|
|
Func<long?> currentGeneration) =>
|
|
new(new NodeOptions { NodeId = "A", ClusterId = "c1", ConfigDbConnectionString = "unused" },
|
|
leases, coordinator, NullLogger<GenerationRefreshHostedService>.Instance,
|
|
tickInterval: TimeSpan.FromSeconds(1),
|
|
currentGenerationQuery: _ => Task.FromResult(currentGeneration()));
|
|
|
|
private sealed class DbContextFactory(DbContextOptions<OtOpcUaConfigDbContext> options)
|
|
: IDbContextFactory<OtOpcUaConfigDbContext>
|
|
{
|
|
public OtOpcUaConfigDbContext CreateDbContext() => new(options);
|
|
}
|
|
}
|