Skip to content

Commit 730e5eb

Browse files
committed
Make CollectionRetriever most_popular fixtures deterministic
Fixes #903. `object_ids.sample(5)` across five check-ins left both the occurrence counts and the `$limit 10` cut-off to chance, so "makes occurrences counts available after retrieve" failed intermittently: Failure/Error: expect(min_occurrence).to be < max_occurrence expected: < 2 got: 2 25 random draws over 20 objects regularly leaves the ten highest-counted objects tied, and CollectionRetriever#most_popular caps `occurrences` at ten, so min == max and the strict comparison fails. Measured at 5 failures in 500 runs on master. Replaced the random sample with nested slices, which give every object a known count: 5 5 | 4 4 4 | 3 3 3 3 3 || 2 2 2 2 2 | 1 1 1 1 1 That fixes two separate sources of nondeterminism, not just the visible one. The top ten now have a real spread (5 down to 3), and the tenth and eleventh differ (3 vs 2), so the `$limit 10` never has to break a tie at the cut-off -- which would otherwise make *which* objects come back arbitrary and could flake "retrieves the most popular" the same way. Since the counts are now known, the weak `min < max` assertion is replaced with the exact sequence. This tests strictly more: it covers the descending sort and the counts themselves rather than just the existence of a spread. Which objects tie at a given count is still undefined -- the aggregation sorts on count alone -- so the assertion is on counts only. The sibling assertions still hold and are now deterministic too: the objects outside the top ten have counts 2 and 1 against a minimum of 3, and all twenty objects are global, so "limits results to 10" always finds ten. Verified: 0 failures in 300 runs, against 5 in 500 on master. Full suite green (320 examples), standardrb clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent ffc6505 commit 730e5eb

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

‎backend/spec/services/collection_retriever_spec.rb‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ def occurrences_for(object_id, ids_key)
2424
let(:objects) { create_list(model_slug, 20) }
2525
let(:object_ids) { objects.map(&:id) }
2626

27+
# Nested slices give every object a known occurrence count. The first 2
28+
# objects land in all five check-ins, the next 3 in four of them, and so
29+
# on, so the counts run:
30+
#
31+
# 5 5 | 4 4 4 | 3 3 3 3 3 || 2 2 2 2 2 | 1 1 1 1 1
32+
#
33+
# The top ten therefore have a real spread (5 down to 3), and the tenth
34+
# and eleventh differ (3 vs 2), so the `$limit 10` in
35+
# CollectionRetriever#most_popular never has to break a tie at the
36+
# cut-off. `object_ids.sample(5)` left both of those to chance.
2737
before do
28-
5.times do
29-
create(:checkin, ids_key => object_ids.sample(5))
38+
[20, 15, 10, 5, 2].each do |size|
39+
create(:checkin, ids_key => object_ids.first(size))
3040
end
3141
end
3242

@@ -49,9 +59,11 @@ def occurrences_for(object_id, ids_key)
4959

5060
it "makes occurrences counts available after retrieve" do
5161
subject.retrieve
52-
max_occurrence = subject.occurrences.to_a.first["count"]
53-
min_occurrence = subject.occurrences.to_a.last["count"]
54-
expect(min_occurrence).to be < max_occurrence
62+
63+
# Which objects tie at a given count is not defined -- the aggregation
64+
# sorts on count alone -- but the counts themselves are.
65+
expect(subject.occurrences.map { |o| o["count"] }).to eq [5, 5, 4, 4, 4, 3, 3, 3, 3, 3]
66+
5567
subject.occurrences.each do |o|
5668
expected_count = occurrences_for(o["_id"], ids_key)
5769
expect(o["count"]).to eq expected_count

0 commit comments

Comments
 (0)