Skip to content

Fix flaky session test#1895

Open
paulinevos wants to merge 1 commit into
mongodb:v2.xfrom
paulinevos:flake-fix
Open

Fix flaky session test#1895
paulinevos wants to merge 1 commit into
mongodb:v2.xfrom
paulinevos:flake-fix

Conversation

@paulinevos
Copy link
Copy Markdown
Contributor

Cluster time advancement is needed for snapshot sessions, since initialData uses an internal client that does not gossip its cluster time to test entities. Without advancement, a snapshot session may establish its atClusterTime before the initial data is visible, causing snapshot reads to return no results.

Copilot AI review requested due to automatic review settings May 8, 2026 11:35
@paulinevos paulinevos requested a review from a team as a code owner May 8, 2026 11:35
@paulinevos paulinevos requested a review from GromNaN May 8, 2026 11:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the unified spec test runner’s initial data setup to decide when to advance cluster time by additionally considering session entities configured with snapshot: true, aiming to reduce flakiness in session-related unified spec tests.

Changes:

  • Pass createEntities into isAdvanceClusterTimeNeeded() when preparing initialData.
  • Extend isAdvanceClusterTimeNeeded() to return true when a snapshot session is defined in createEntities.
  • Expand the method’s docblock to describe why snapshot sessions may require cluster time advancement.

Comment thread tests/UnifiedSpecTests/UnifiedTestRunner.php Outdated
Comment thread tests/UnifiedSpecTests/UnifiedTestRunner.php
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.72%. Comparing base (f1ce3a6) to head (2133e5c).
⚠️ Report is 1 commits behind head on v2.x.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff              @@
##               v2.x    #1895      +/-   ##
============================================
+ Coverage     87.70%   87.72%   +0.02%     
- Complexity     3324     3328       +4     
============================================
  Files           452      454       +2     
  Lines          6644     6656      +12     
============================================
+ Hits           5827     5839      +12     
  Misses          817      817              
Flag Coverage Δ
6.0-replica_set 86.56% <ø> (+0.02%) ⬆️
6.0-server 82.61% <ø> (+0.03%) ⬆️
6.0-sharded_cluster 86.35% <ø> (+0.02%) ⬆️
8.0-replica_set 87.59% <ø> (+0.02%) ⬆️
8.0-server 83.35% <ø> (+0.03%) ⬆️
8.0-sharded_cluster ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread tests/UnifiedSpecTests/UnifiedTestRunner.php Outdated
Cluster time advancement is needed for snapshot sessions, since
initialData uses an internal client that does not gossip its cluster
time to test entities. Without advancement, a snapshot session may
establish its atClusterTime before the initial data is visible, causing
snapshot reads to return no results.
@paulinevos paulinevos force-pushed the flake-fix branch 2 times, most recently from 1e3b4b3 to 2133e5c Compare May 8, 2026 14:32
@GromNaN
Copy link
Copy Markdown
Member

GromNaN commented May 11, 2026

The test is still failing 😢

1) MongoDB\Tests\UnifiedSpecTests\UnifiedSpecTest::testSessions with data set "sessions/snapshot-sessions: Find operation with snapshot" (MongoDB\Tests\UnifiedSpecTests\UnifiedTestCase Object (...))
Failed asserting that expected value matches actual value.
$actual count is 0, expected 1
--- Expected
+++ Actual
@@ @@
-MongoDB\Model\BSONArray Object #150220 (
-    0 => MongoDB\Model\BSONDocument Object #150203 (
-        '_id' => 1,
-        'x' => 11,
-    ),
-)
+MongoDB\Model\BSONArray Object #150202 ()

foreach ($createEntities ?? [] as $entity) {
$session = $entity->session ?? null;
if ($session === null) {
return false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a continue -- there could still be an entity with a snapshot session later in the list that might necessitate a cluster time advancement.

* atClusterTime before the initial data is visible, causing snapshot reads to return no results.
*/
private function isAdvanceClusterTimeNeeded(array $operations): bool
private function isAdvanceClusterTimeNeeded(array $operations, ?array $createEntities = null): bool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the nullable array is because we have tests without entities, in which case we get null? Would it make sense to normalise that to an empty array before we pass it around?

foreach ($createEntities ?? [] as $entity) {
$session = $entity->session ?? null;
if ($session === null) {
return false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason the test is still failing after this PR.

The createEntities array in snapshot-sessions.json starts with client, database, and collection entries before the session entries:

[{client: {id: "client0", ...}}, {database: {...}}, {collection: {...}}, {session: {snapshot: true}}, ...]

When the loop reaches the first entity ({client: {...}}), $entity->session is null (the entity has no session key), so it hits return false and exits immediately — never reaching the actual snapshot sessions.

This should be continue (as @alcaeus noted) so the loop keeps checking remaining entities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants