Merge branch 'review/wp7' into review/integration

This commit is contained in:
Joseph Doherty
2026-12-03 10:41:19 -05:00
47 changed files with 1584 additions and 152 deletions
@@ -1326,6 +1326,84 @@ public class DeploymentServiceTests : TestKit
Assert.Equal(1, serializationCounters.MaxConcurrent);
}
// ── DEP-2: validate the state transition AFTER the operation lock is held ──
/// <summary>
/// A command that waited behind another one must judge the state that command left behind:
/// here the instance was Enabled when loaded, but another Disable finished while this one
/// waited for the lock. Validating the stale copy would send a second Disable to the site.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DisableInstanceAsync_StateChangedWhileWaitingForLock_FailsTransitionCheckOnFreshState()
{
var instance = new Instance("RaceDisable") { Id = 80, SiteId = 1, State = InstanceState.Enabled };
_repo.GetInstanceByIdAsync(80, Arg.Any<CancellationToken>()).Returns(instance);
// The reload observes what the lock holder committed.
_repo.ReloadInstanceAsync(instance, Arg.Any<CancellationToken>()).Returns(_ =>
{
instance.State = InstanceState.Disabled;
return Task.FromResult(false);
});
using var held = await _lockManager.AcquireAsync("RaceDisable", TimeSpan.FromSeconds(5), CancellationToken.None);
var pending = _service.DisableInstanceAsync(80, "admin");
await Task.Delay(100);
Assert.False(pending.IsCompleted, "the command must wait for the lock before validating");
held.Dispose();
var result = await pending;
// The default _service has no comm actor: reaching the site would throw, so a clean
// failure proves the transition check ran on the fresh Disabled state first.
Assert.True(result.IsFailure);
Assert.Contains("not allowed", result.Error);
}
/// <summary>
/// Instance X was NotDeployed when the Delete loaded it, then a deploy that held the lock left it
/// Enabled. The Delete must take the site round-trip, not the NotDeployed central-only shortcut.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DeleteInstanceAsync_DeployCompletedWhileWaiting_TearsDownSiteInsteadOfSkippingIt()
{
var instance = new Instance("RaceDelete") { Id = 81, SiteId = 1, State = InstanceState.NotDeployed };
_repo.GetInstanceByIdAsync(81, Arg.Any<CancellationToken>()).Returns(instance);
_repo.ReloadInstanceAsync(instance, Arg.Any<CancellationToken>()).Returns(_ =>
{
instance.State = InstanceState.Enabled;
return Task.FromResult(false);
});
var counters = new ReconcileProbeCounters();
var commActor = Sys.ActorOf(Props.Create(() =>
new ReconcileProbeActor(counters, siteHash: "x", failQuery: false)));
var service = CreateServiceWithCommActor(commActor);
var result = await service.DeleteInstanceAsync(81, "admin");
Assert.True(result.IsSuccess);
await _audit.Received().LogAsync(
"admin", "Delete", "Instance", "81", "RaceDelete",
Arg.Is<object>(o => !o.ToString()!.Contains("SiteRoundTripSkipped")), Arg.Any<CancellationToken>());
}
/// <summary>An instance deleted by another command while this one waited is reported as not found.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task EnableInstanceAsync_InstanceDeletedWhileWaiting_ReturnsNotFound()
{
var instance = new Instance("RaceGone") { Id = 82, SiteId = 1, State = InstanceState.Disabled };
_repo.GetInstanceByIdAsync(82, Arg.Any<CancellationToken>()).Returns(instance);
_repo.ReloadInstanceAsync(instance, Arg.Any<CancellationToken>()).Returns(true);
var result = await _service.EnableInstanceAsync(82, "admin");
Assert.True(result.IsFailure);
Assert.Contains("not found", result.Error);
}
// ── Query-the-site-before-redeploy idempotency ──
/// <summary>