From 9eec79330302eb1c12ef332b01d191027788c64c Mon Sep 17 00:00:00 2001 From: Darrin Cullop Date: Tue, 26 May 2026 17:26:22 -0700 Subject: [PATCH] Make SizeLimit tests deterministic by deduplicating generator output RandomPersonGenerator emits Person rows drawn from a finite name pool (~21 girls + ~30 boys cross-joined with 24 lastnames squared). Person.Key is Person.Name, so two independent .Take(10) calls can produce overlapping keys with non-trivial probability. When they collide, the second batch's AddOrUpdate produces 9 Adds + 1 Update instead of 10 Adds, breaking the per-message assertions in: - InvokeLimitSizeToWhenOverLimit - AddMoreThanLimitInBatched Both tests now draw 60 candidates up front, dedupe by Key, take the first 20, and split into two non-overlapping batches of 10. Verified: 50/50 consecutive runs of SizeLimitFixture pass with no failures. --- .../Cache/SizeLimitFixture.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/DynamicData.Tests/Cache/SizeLimitFixture.cs b/src/DynamicData.Tests/Cache/SizeLimitFixture.cs index bbc67f04d..378b7fa6d 100644 --- a/src/DynamicData.Tests/Cache/SizeLimitFixture.cs +++ b/src/DynamicData.Tests/Cache/SizeLimitFixture.cs @@ -72,8 +72,13 @@ public void AddMoreThanLimit() [Fact] public void AddMoreThanLimitInBatched() { - _source.AddOrUpdate(_generator.Take(10).ToArray()); - _source.AddOrUpdate(_generator.Take(10).ToArray()); + // _generator.Take(N) draws random Person rows from a finite name pool; a second + // Take(10) call can produce keys that collide with the first batch, turning an + // Add into an Update and breaking the per-message Adds count. Draw a larger pool + // up front, dedupe by Key, then split into two non-overlapping batches of 10. + var people = _generator.Take(60).DistinctBy(p => p.Key).Take(20).ToArray(); + _source.AddOrUpdate(people.Take(10).ToArray()); + _source.AddOrUpdate(people.Skip(10).Take(10).ToArray()); _scheduler.Start(); @@ -96,12 +101,17 @@ public void InvokeLimitSizeToWhenOverLimit() var removesTriggered = false; var subscriber = _source.LimitSizeTo(10, _scheduler).Subscribe(removes => { removesTriggered = true; }); - _source.AddOrUpdate(_generator.Take(10).ToArray()); + // _generator.Take(N) draws random Person rows from a finite name pool; a second + // Take(10) call can produce keys that collide with the first batch, turning an + // Add into an Update and breaking the per-message Adds count. Draw a larger pool + // up front, dedupe by Key, then split into two non-overlapping batches of 10. + var people = _generator.Take(60).DistinctBy(p => p.Key).Take(20).ToArray(); + _source.AddOrUpdate(people.Take(10).ToArray()); _scheduler.AdvanceBy(TimeSpan.FromMilliseconds(150).Ticks); removesTriggered.Should().BeFalse(); - _source.AddOrUpdate(_generator.Take(10).ToArray()); + _source.AddOrUpdate(people.Skip(10).Take(10).ToArray()); _scheduler.AdvanceBy(TimeSpan.FromMilliseconds(150).Ticks);