test(grpc-data): #928 — overflow test asserted one of two legal outcomes; deterministic Reset test + either-outcome contract test; DataConnections.md A4 reconcile (#930 retries, condition metrics, AdminUI fields)

The origin item sees a hub eviction only at its next hub read: Reset if the last snapshot chunk is
still undelivered, otherwise a silent fresh snapshot on the same stream (both per A2, both loss-free
since the A3 consumer swaps a buffered re-snapshot in as a full replacement). Since A2 review S5 the
3,000-record snapshot is 59 chunks and the silent path wins when run alone (fails 3/3 alone at the A2
merge too; passes in its class). No product change. Also fixes the stale ack/confirm/shelve comment on
ConditionMethodTimeoutMilliseconds.
This commit is contained in:
Joseph Doherty
2026-11-25 00:08:06 -05:00
parent 3be51e3b13
commit b2fab8b075
4 changed files with 204 additions and 32 deletions
@@ -106,18 +106,91 @@ public sealed class GrpcDataConditionStreamTests
f.StreamOf(stream)!.Resets.ShouldBe(1);
}
/// <summary>
/// #928: an eviction the item detects while its last snapshot chunk is still undelivered ⇒ stream <c>Reset</c>
/// ("overflow"). Deterministic: the whole snapshot (begin + 6 chunks) fits the 16-frame condition queue, and the
/// consumer reads nothing after the ack, so the last chunk is still queued when the session tick makes the item read
/// the hub. (The former version raced a 3,000-record delivery against the producer — see the contract test below.)
/// </summary>
[Fact]
public async Task Overflow_before_the_snapshot_is_drained_resets_the_stream()
{
// 3,000 records = 24 chunks, more than the 16-frame condition queue: the last chunk is produced only after the
// consumer drained most of them, and the item reads the hub right after producing it — still undelivered.
using var f = new ConditionFixture(conditions: 300, maxConditions: 5000, partitionCapacity: 2);
await using var stream = await f.OpenAsync();
await stream.ReadAckAsync(); // the ack's delivery activates the item; nothing after it is read until the Reset
var item = await WaitForAsync(() => f.StreamOf(stream, allowMissing: true) is { ChargedBytes: > 0 } s ? s : null);
// The snapshot is taken (its start cursor precedes the raises) and charged, so its last chunk is undelivered.
for (var i = 0; i < 6; i++) f.Source.Raise(0, active: i % 2 == 0); // the hub evicts above the item's start cursor
await WaitForAsync(() => { f.Tick(); return item.Overflowed ? item : null; }); // the session tick runs the pump
var reset = await stream.ReadUntilAsync<DataSubscriptionControl>(f, c => c.Kind == DataStreamControlKind.Reset);
reset.Diagnostic.ShouldNotBeNull().ShouldContain("overflow");
item.Snapshots.ShouldBe(1, "an overflow resets the stream; the item does not re-snapshot behind an undelivered snapshot");
}
/// <summary>
/// #928 contract: an eviction during a long snapshot delivery that the consumer is draining has TWO legal outcomes,
/// decided by whether the item reads the hub before or after the session commits the snapshot's last chunk — a
/// stream <c>Reset</c> (the consumer reconnects and re-snapshots) or a silent fresh snapshot on the same stream (a new
/// begin; A3 buffers it and swaps at <c>snapshot_complete</c>). Either way no change is lost: the state the consumer
/// ends with matches the source.
/// </summary>
[Fact]
public async Task An_eviction_during_a_long_snapshot_delivery_resets_or_resnapshots_without_losing_a_change()
{
using var f = new ConditionFixture(conditions: 3000, maxConditions: 5000, partitionCapacity: 2);
await using var stream = await f.OpenAsync();
await stream.ReadAckAsync();
(await stream.ReadConditionsAsync(f)).Total.ShouldBe(3000);
for (var i = 0; i < 6; i++) f.Source.Raise(0, active: i % 2 == 0); // the hub evicts above the item's start cursor
var reset = await stream.ReadUntilAsync<DataSubscriptionControl>(f, c => c.Kind == DataStreamControlKind.Reset);
reset.Diagnostic.ShouldNotBeNull().ShouldContain("overflow");
var item = f.StreamOf(stream)!;
for (var i = 0; i < 6; i++) f.Source.Raise(0, active: i % 2 == 0); // evicts above the item's start cursor
var expected = f.Source.IsActive(0);
while (true)
{
var frame = await stream.NextAsync() ?? throw new InvalidOperationException("Neither a Reset nor a fresh snapshot.");
if (frame is DataSubscriptionControl { Kind: DataStreamControlKind.Reset } reset)
{
reset.Diagnostic.ShouldNotBeNull().ShouldContain("overflow");
item.Overflowed.ShouldBeTrue();
await using var fresh = await f.OpenAsync(); // what the consumer does next: reconnect and re-snapshot
var snapshot = await fresh.ReadSnapshotAsync();
snapshot.Complete.ShouldBeTrue();
snapshot.Records.Count.ShouldBe(3000);
snapshot.Records.Single(r => r.ConditionKey == f.KeyOf(0)).Active.ShouldBe(expected);
return;
}
if (frame is DataSubscriptionConditions { IsSnapshot: true, Total: { } total })
{
// The silent path: a hub reader reset after the last chunk was delivered ⇒ a complete fresh snapshot.
total.ShouldBe(3000);
var records = new List<DataConditionState>();
var complete = false;
while (!complete)
{
var chunk = await stream.ReadConditionsAsync(f);
chunk.IsSnapshot.ShouldBeTrue("no delta interleaves a snapshot");
records.AddRange(chunk.Items);
complete = chunk.SnapshotComplete;
}
records.Count.ShouldBe(3000);
records.Single(r => r.ConditionKey == f.KeyOf(0)).Active.ShouldBe(expected);
item.Overflowed.ShouldBeFalse();
item.Resets.ShouldBe(1);
item.Snapshots.ShouldBe(2);
stream.Ended.ShouldBeFalse("a re-snapshot is not a stream reset");
return;
}
}
}
private static async Task<T> WaitForAsync<T>(Func<T?> probe) where T : class
{
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(10);
while (DateTime.UtcNow < deadline)
{
if (probe() is { } value) return value;
await Task.Delay(10, Ct);
}
throw new TimeoutException("Condition not reached.");
}
[Fact]