584 lines
26 KiB
C#
584 lines
26 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Feature", "GrpcData")]
|
|
[Trait("Category", "Unit")]
|
|
[Trait("Phase", "P04")]
|
|
public sealed class GrpcDataSnapshotTests
|
|
{
|
|
[Fact]
|
|
public async Task Nonblocking_ticket_validation_rejects_old_partition_during_repartition()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var ticket = HubTest.Ticket("driver", "routing-gap");
|
|
hub.TryBind(ticket).ShouldBeTrue();
|
|
var epoch = hub.RoutingEpoch;
|
|
var cancellationToken = TestContext.Current.CancellationToken;
|
|
using var entered = new ManualResetEventSlim();
|
|
using var release = new ManualResetEventSlim();
|
|
var holder = Task.Run(() => hub.WithPartitionLockForTest(ticket.Key, () =>
|
|
{
|
|
entered.Set();
|
|
release.Wait(cancellationToken);
|
|
}), cancellationToken);
|
|
Task? repartition = null;
|
|
try
|
|
{
|
|
entered.Wait(TimeSpan.FromSeconds(5), cancellationToken).ShouldBeTrue();
|
|
repartition = Task.Run(() => hub.Repartition(2), cancellationToken);
|
|
SpinWait.SpinUntil(() =>
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
return hub.RoutingEpoch != epoch;
|
|
}, TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
|
var isCurrent = await Task.Run(() => hub.IsTicketCurrentNonBlocking(ticket), cancellationToken)
|
|
.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
|
|
isCurrent.ShouldBeFalse();
|
|
}
|
|
finally
|
|
{
|
|
release.Set();
|
|
await holder.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
|
|
if (repartition is not null) await repartition.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Steady_interest_detects_quiet_source_invalidation_after_snapshot_disposal(bool retireProducer)
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var ticket = HubTest.Ticket("driver", "quiet-control");
|
|
var producer = new ProducerFence(1);
|
|
hub.TryBind(ticket, producer).ShouldBeTrue();
|
|
HubTest.Publish(hub, ticket, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureInterestSnapshot([ticket], HubTest.Budget, CancellationToken.None,
|
|
out var snapshot, out var interest).ShouldBeTrue();
|
|
using (interest!)
|
|
{
|
|
snapshot!.Dispose();
|
|
if (retireProducer) producer.Retire();
|
|
else hub.Invalidate(ticket, "source invalidated");
|
|
(interest!.IsReset || !interest.TryReadChanges(1, out _)).ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Status_changes_remain_observable_through_steady_interest_history()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var ticket = HubTest.Ticket("driver", "status-history");
|
|
HubTest.BindAndPublish(hub, ticket, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureInterestSnapshot([ticket], HubTest.Budget, CancellationToken.None,
|
|
out var snapshot, out var interest).ShouldBeTrue();
|
|
using (interest!)
|
|
{
|
|
snapshot!.Dispose();
|
|
hub.TryPublishStatus(ticket, 2, 0x80000000, null, null).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
interest!.IsReset.ShouldBeFalse();
|
|
interest.TryReadChanges(1, out var changes).ShouldBeTrue();
|
|
changes.ShouldHaveSingleItem().Ticket.ShouldBe(ticket);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Source_control_and_status_reset_the_snapshot_but_ordinary_values_do_not()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var source = HubTest.Ticket("driver", "control");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var frozen).ShouldBeTrue();
|
|
using (frozen!)
|
|
{
|
|
HubTest.Publish(hub, source, 2, 2);
|
|
hub.ProcessPending();
|
|
frozen!.IsReset.ShouldBeFalse();
|
|
frozen.IsItemCurrent(source).ShouldBeTrue();
|
|
frozen.Items[source].Sample!.Value.GetScalar().ShouldBe(1);
|
|
hub.TryPublishStatus(source, 3, 0x80000000, null, null).ShouldBeTrue();
|
|
frozen.IsReset.ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Active_partition_control_cannot_be_accepted_by_snapshot_validation()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var source = HubTest.Ticket("driver", "active-control");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
var signal = hub.ControlSignalForTests(source.Key);
|
|
signal.Begin();
|
|
try { snapshot!.IsReset.ShouldBeTrue(); }
|
|
finally { signal.End(); }
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Control_counter_overflow_permanently_rejects_snapshots()
|
|
{
|
|
var signal = new PartitionControlSignal();
|
|
typeof(PartitionControlSignal).GetField("_generation", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
|
|
.SetValue(signal, long.MaxValue);
|
|
signal.Begin();
|
|
signal.TryCapture(out _).ShouldBeFalse();
|
|
signal.End();
|
|
signal.TryCapture(out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Routing_epoch_exhaustion_rejects_repartition_before_mutation()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
typeof(PartitionedSampleHub).GetField("_routingEpoch", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
|
|
.SetValue(hub, long.MaxValue);
|
|
Should.Throw<InvalidOperationException>(() => hub.Repartition(2));
|
|
hub.RoutingEpoch.ShouldBe(long.MaxValue);
|
|
hub.GetPartition(HubTest.Ticket("driver", "epoch-limit").Key).ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Retirement_help_covers_all_partitions_when_first_caller_pauses_mid_vector()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 2);
|
|
var firstSource = HubTest.Ticket("driver", "first-signal");
|
|
var secondSource = Enumerable.Range(0, 1000).Select(i => HubTest.Ticket("driver", "second-signal-" + i))
|
|
.First(t => hub.GetPartition(t.Key) != hub.GetPartition(firstSource.Key));
|
|
var producer = new ProducerFence(1);
|
|
foreach (var source in new[] { firstSource, secondSource })
|
|
{
|
|
hub.TryBind(source, producer).ShouldBeTrue();
|
|
HubTest.Publish(hub, source, 1, 1);
|
|
}
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([firstSource, secondSource], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
using (var entered = new ManualResetEventSlim())
|
|
using (var release = new ManualResetEventSlim())
|
|
{
|
|
var cancellationToken = TestContext.Current.CancellationToken;
|
|
var calls = 0;
|
|
producer.AfterSignalBegunForTests = () =>
|
|
{
|
|
if (Interlocked.Increment(ref calls) == 1) { entered.Set(); release.Wait(); }
|
|
};
|
|
var first = Task.Run(producer.Retire, cancellationToken);
|
|
try
|
|
{
|
|
entered.Wait(TimeSpan.FromSeconds(5), cancellationToken).ShouldBeTrue();
|
|
snapshot!.IsReset.ShouldBeTrue();
|
|
await Task.Run(producer.Retire, cancellationToken).WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
|
|
producer.IsRetired.ShouldBeTrue();
|
|
}
|
|
finally { release.Set(); await first.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken); }
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Concurrent_retire_helps_complete_while_first_caller_is_paused()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var source = HubTest.Ticket("driver", "retire-help");
|
|
var producer = new ProducerFence(1);
|
|
hub.TryBind(source, producer).ShouldBeTrue();
|
|
HubTest.Publish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
using (var entered = new ManualResetEventSlim())
|
|
using (var release = new ManualResetEventSlim())
|
|
{
|
|
var cancellationToken = TestContext.Current.CancellationToken;
|
|
var calls = 0;
|
|
producer.BeforeRetirementPublicationForTests = () =>
|
|
{
|
|
if (Interlocked.Increment(ref calls) == 1) { entered.Set(); release.Wait(); }
|
|
};
|
|
var first = Task.Run(producer.Retire, cancellationToken);
|
|
try
|
|
{
|
|
entered.Wait(TimeSpan.FromSeconds(5), cancellationToken).ShouldBeTrue();
|
|
await Task.Run(producer.Retire, cancellationToken).WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
|
|
producer.IsRetired.ShouldBeTrue();
|
|
snapshot!.IsReset.ShouldBeTrue();
|
|
hub.TryBind(HubTest.Ticket("driver", "late"), producer).ShouldBeFalse();
|
|
}
|
|
finally { release.Set(); await first.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken); }
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Producer_is_owned_by_one_hub_and_routing_replaces_old_signal_vectors()
|
|
{
|
|
using var first = HubTest.Create(partitions: 2);
|
|
using var second = HubTest.Create(partitions: 2);
|
|
var producer = new ProducerFence(1);
|
|
first.TryBind(HubTest.Ticket("driver", "first"), producer).ShouldBeTrue();
|
|
second.TryBind(HubTest.Ticket("driver", "second"), producer).ShouldBeFalse();
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
first.Repartition(2);
|
|
first.TryBind(HubTest.Ticket("driver", "epoch-" + i), producer).ShouldBeTrue();
|
|
producer.AttachedSignalCountForTests.ShouldBeLessThanOrEqualTo(2);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Producer_retirement_never_waits_for_a_partition_lock()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var source = HubTest.Ticket("driver", "locked-partition");
|
|
var producer = new ProducerFence(1);
|
|
hub.TryBind(source, producer).ShouldBeTrue();
|
|
using var entered = new ManualResetEventSlim();
|
|
using var release = new ManualResetEventSlim();
|
|
var cancellationToken = TestContext.Current.CancellationToken;
|
|
var holder = Task.Run(() => hub.WithPartitionLockForTest(source.Key, () => { entered.Set(); release.Wait(); }), cancellationToken);
|
|
try
|
|
{
|
|
entered.Wait(TimeSpan.FromSeconds(5), cancellationToken).ShouldBeTrue();
|
|
await Task.Run(producer.Retire, cancellationToken).WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
|
|
producer.IsRetired.ShouldBeTrue();
|
|
}
|
|
finally { release.Set(); await holder.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken); }
|
|
}
|
|
|
|
[Fact]
|
|
public void Producer_rejects_stale_epoch_attachment_after_repartition()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var producer = new ProducerFence(1);
|
|
hub.TryBind(HubTest.Ticket("driver", "before"), producer).ShouldBeTrue();
|
|
var oldEpoch = hub.RoutingEpoch;
|
|
hub.Repartition(1);
|
|
hub.TryBind(HubTest.Ticket("driver", "after"), producer).ShouldBeTrue();
|
|
producer.TryAttach(hub, oldEpoch, new PartitionControlSignal()).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Missing_ticket_cannot_be_accepted_as_a_captured_source()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var missing = HubTest.Ticket("driver", "missing");
|
|
hub.TryCaptureSnapshot([missing], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeFalse();
|
|
snapshot.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Delta_positions_advance_the_partition_history_minimum()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var source = HubTest.Ticket("driver", "cursor");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
for (var version = 2; version <= 5; version++)
|
|
{
|
|
HubTest.Publish(hub, source, version, version);
|
|
hub.ProcessPending();
|
|
snapshot!.TryReadDelta(source, out var delta).ShouldBeTrue();
|
|
delta!.Dispose();
|
|
snapshot.IsReset.ShouldBeFalse();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Repeated_snapshot_validity_checks_do_not_rebuild_groups_for_every_captured_item()
|
|
{
|
|
var limits = new DataResourceLimits
|
|
{
|
|
PartitionCount = 4, MaxRetainedBytes = 16_000_000, MaxTrackedSources = 512,
|
|
MaxIngressItemsPerPartition = 512, MaxDirtySourcesPerPartition = 512,
|
|
MaxControlItemsPerPartition = 32, MaxSnapshotItems = 512, MaxSnapshotBytes = 8_000_000,
|
|
MaxSnapshotCatchUpItems = 512, MaxSnapshotLifetime = TimeSpan.FromMinutes(1)
|
|
};
|
|
using var hub = new PartitionedSampleHub(limits, new ManualTimeProvider(), autoDrain: false);
|
|
var tickets = Enumerable.Range(0, 256).Select(i => HubTest.Ticket("driver", "snapshot-" + i)).ToArray();
|
|
foreach (var ticket in tickets) HubTest.BindAndPublish(hub, ticket, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot(tickets, new DataBudget(TimeSpan.FromSeconds(30), 512, 8_000_000),
|
|
CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
snapshot!.IsReset.ShouldBeFalse(); // warm JIT outside measured work
|
|
var before = GC.GetAllocatedBytesForCurrentThread();
|
|
var workBefore = hub.SnapshotValidationWorkForTests;
|
|
for (var i = 0; i < 128; i++) if (snapshot.IsReset) throw new InvalidOperationException();
|
|
var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
|
|
allocated.ShouldBeLessThan(64 * 1024, "each check must not regroup all 256 source entries");
|
|
(hub.SnapshotValidationWorkForTests - workBefore).ShouldBeLessThan(128 * 16,
|
|
"a validity check must not walk every source in the snapshot");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Delayed_reserved_ingress_commit_keeps_original_normalized_receipt_time()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
using var hub = HubTest.Create(clock: clock);
|
|
var source = HubTest.Ticket("driver", "staged");
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
var received = clock.GetTimestamp();
|
|
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
|
|
using (reservation!)
|
|
{
|
|
clock.Advance(TimeSpan.FromSeconds(5));
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 8), 0, null, null, HubTest.Serving, 1);
|
|
if (!reservation!.TryCommit(sample, 1)) { sample.Dispose(); throw new InvalidOperationException(); }
|
|
}
|
|
hub.ProcessPending();
|
|
using var read = hub.Read(source);
|
|
read.ReceivedTimestamp.ShouldBe(received);
|
|
clock.GetElapsedTime(read.ReceivedTimestamp).ShouldBe(TimeSpan.FromSeconds(5));
|
|
}
|
|
|
|
[Fact]
|
|
public void Steady_interest_survives_snapshot_disposal_and_observes_later_dirty_generation()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "interest");
|
|
HubTest.BindAndPublish(hub, source, 1, 10);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureInterestSnapshot([source], HubTest.Budget, CancellationToken.None,
|
|
out var snapshot, out var interest).ShouldBeTrue();
|
|
using (interest!)
|
|
{
|
|
snapshot!.Items[source].Sample!.Value.GetScalar().ShouldBe(10);
|
|
snapshot.Dispose();
|
|
HubTest.Publish(hub, source, 2, 20);
|
|
hub.ProcessPending();
|
|
interest!.TryReadChanges(1, out var changes).ShouldBeTrue();
|
|
changes.ShouldHaveSingleItem().Ticket.ShouldBe(source);
|
|
using var latest = hub.Read(source);
|
|
latest.Sample!.Value.GetScalar().ShouldBe(20);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Atomic_interest_registration_cannot_lose_a_racing_source_update()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "interest-race");
|
|
HubTest.BindAndPublish(hub, source, 1, 10);
|
|
hub.ProcessPending();
|
|
SnapshotLease? snapshot = null;
|
|
SourceInterestLease? interest = null;
|
|
var accepted = false;
|
|
var captured = false;
|
|
Parallel.Invoke(
|
|
() => captured = hub.TryCaptureInterestSnapshot([source], HubTest.Budget, CancellationToken.None,
|
|
out snapshot, out interest),
|
|
() =>
|
|
{
|
|
if (!hub.TryReserveIngress(source, 288, out var reservation)) return;
|
|
using (reservation!)
|
|
{
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 20), 0, null, null, HubTest.Serving, 2);
|
|
accepted = reservation!.TryCommit(sample, 2);
|
|
if (!accepted) sample.Dispose();
|
|
}
|
|
hub.ProcessPending();
|
|
});
|
|
if (!captured) return; // bounded contention is an explicit retry boundary
|
|
using (snapshot!)
|
|
using (interest!)
|
|
{
|
|
if (!accepted) { snapshot!.IsReset.ShouldBeTrue(); return; }
|
|
var value = (int)snapshot!.Items[source].Sample!.Value.GetScalar();
|
|
if (value == 10)
|
|
{
|
|
interest!.TryReadChanges(1, out var changes).ShouldBeTrue();
|
|
changes.ShouldContain(x => x.Ticket == source);
|
|
}
|
|
else value.ShouldBe(20);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_retains_original_monotonic_acquisition_time_after_status_and_reconnect()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
using var hub = HubTest.Create(clock: clock);
|
|
var source = HubTest.Ticket("driver", "age");
|
|
var acquired = clock.GetTimestamp();
|
|
HubTest.BindAndPublish(hub, source, 1, 42);
|
|
hub.ProcessPending();
|
|
clock.Advance(TimeSpan.FromSeconds(3));
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var first).ShouldBeTrue();
|
|
using (first!)
|
|
{
|
|
first!.Items[source].ReceivedTimestamp.ShouldBe(acquired);
|
|
first.Items[source].Sample!.SourceTimestampUtc.ShouldBeNull();
|
|
first.Items[source].Sample!.ServerTimestampUtc.ShouldBeNull();
|
|
}
|
|
hub.TryPublishStatus(source, 2, 0x80000000, null, null).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
clock.Advance(TimeSpan.FromSeconds(4));
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var reconnected).ShouldBeTrue();
|
|
using (reconnected!)
|
|
{
|
|
reconnected!.Items[source].ReceivedTimestamp.ShouldBe(acquired);
|
|
clock.GetElapsedTime(reconnected.Items[source].ReceivedTimestamp!.Value).ShouldBe(TimeSpan.FromSeconds(7));
|
|
reconnected.Items[source].Sample!.StatusCode.ShouldBe(0x80000000u);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_with_no_sample_has_no_acquisition_timestamp()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "waiting");
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
snapshot!.Items[source].Sample.ShouldBeNull();
|
|
snapshot.Items[source].ReceivedTimestamp.ShouldBeNull();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_freezes_samples_and_its_watermarks_cover_every_participating_partition()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 2);
|
|
var first = HubTest.Ticket("driver", "a");
|
|
var second = Enumerable.Range(0, 1000).Select(i => HubTest.Ticket("driver", $"other-{i}"))
|
|
.First(t => hub.GetPartition(t.Key) != hub.GetPartition(first.Key));
|
|
HubTest.BindAndPublish(hub, first, 1, 10);
|
|
HubTest.BindAndPublish(hub, second, 1, 20);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
snapshot!.Watermarks.Count.ShouldBe(2);
|
|
snapshot.Items[first].Sample!.Value.GetScalar().ShouldBe(10);
|
|
snapshot.Items[second].Sample!.Value.GetScalar().ShouldBe(20);
|
|
HubTest.Publish(hub, first, 2, 11);
|
|
hub.ProcessPending();
|
|
snapshot.Items[first].Sample!.Value.GetScalar().ShouldBe(10);
|
|
snapshot.TryReadDelta(first, out var delta).ShouldBeTrue();
|
|
using (delta!) delta!.Sample!.Value.GetScalar().ShouldBe(11);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Racing_registration_and_update_cannot_lose_the_newer_value()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "race");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
SnapshotLease? snapshot = null;
|
|
var captured = false;
|
|
var accepted = false;
|
|
Parallel.Invoke(
|
|
() => captured = hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out snapshot),
|
|
() =>
|
|
{
|
|
if (hub.TryReserveIngress(source, 288, out var reservation))
|
|
{
|
|
using (reservation!)
|
|
{
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 2), 0, null, null, HubTest.Serving, 2);
|
|
accepted = reservation!.TryCommit(sample, 2);
|
|
if (!accepted) sample.Dispose();
|
|
}
|
|
}
|
|
hub.ProcessPending();
|
|
});
|
|
if (!accepted)
|
|
{
|
|
if (captured) { snapshot!.IsReset.ShouldBeTrue(); snapshot.Dispose(); }
|
|
using var invalidated = hub.Read(source);
|
|
invalidated.Readiness.ShouldNotBe(DataReadiness.Ready);
|
|
return;
|
|
}
|
|
if (!captured) return; // a reset during registration is an explicit retry boundary
|
|
using (snapshot!)
|
|
{
|
|
var capturedValue = (int)snapshot!.Items[source].Sample!.Value.GetScalar();
|
|
if (capturedValue == 1)
|
|
{
|
|
snapshot.TryReadDelta(source, out var delta).ShouldBeTrue();
|
|
using (delta!) delta!.Sample!.Value.GetScalar().ShouldBe(2);
|
|
}
|
|
else capturedValue.ShouldBe(2);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Cancellation_and_deadline_actively_release_all_snapshot_resources()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
using var hub = HubTest.Create(clock: clock);
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
HubTest.BindAndPublish(hub, source, 1, 9);
|
|
hub.ProcessPending();
|
|
var baseline = hub.Resources.RetainedBytes;
|
|
using var cancellation = new CancellationTokenSource();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, cancellation.Token, out var canceled).ShouldBeTrue();
|
|
hub.Resources.RetainedBytes.ShouldBeGreaterThan(baseline);
|
|
cancellation.Cancel();
|
|
canceled!.IsReset.ShouldBeTrue();
|
|
hub.Resources.RetainedBytes.ShouldBe(baseline);
|
|
canceled.Dispose(); // cancellation and explicit disposal are idempotent
|
|
hub.Resources.RetainedBytes.ShouldBe(baseline);
|
|
|
|
var shortBudget = HubTest.Budget with { Timeout = TimeSpan.FromSeconds(2) };
|
|
hub.TryCaptureSnapshot([source], shortBudget, CancellationToken.None, out var expired).ShouldBeTrue();
|
|
clock.Advance(TimeSpan.FromSeconds(2));
|
|
expired!.IsReset.ShouldBeTrue();
|
|
hub.Resources.RetainedBytes.ShouldBe(baseline);
|
|
expired.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_admission_is_global_and_rejects_before_retaining_more_payload()
|
|
{
|
|
using var hub = HubTest.Create(bytes: 1700);
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
HubTest.BindAndPublish(hub, source, 1, 5);
|
|
hub.ProcessPending();
|
|
var before = hub.Resources.RetainedBytes;
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var rejected).ShouldBeFalse();
|
|
rejected.ShouldBeNull();
|
|
hub.Resources.RetainedBytes.ShouldBe(before);
|
|
using var read = hub.Read(source);
|
|
read.Status.ShouldBe(DataItemStatus.ResourceExhausted);
|
|
read.Sample.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Catch_up_overflow_resets_snapshot_instead_of_silently_losing_deltas()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var first = HubTest.Ticket("driver", "first");
|
|
var second = HubTest.Ticket("driver", "second");
|
|
HubTest.BindAndPublish(hub, first, 1, 1);
|
|
HubTest.BindAndPublish(hub, second, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
HubTest.Publish(hub, first, 2, 2);
|
|
HubTest.Publish(hub, second, 2, 2);
|
|
hub.ProcessPending();
|
|
snapshot!.IsReset.ShouldBeTrue();
|
|
}
|
|
}
|
|
}
|