Skip to content

PHPLIB-1115: Fix waitForSnapshot() for MongoDB 7.0+ #1083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function microtime;
use function ob_end_clean;
use function ob_start;
use function usleep;
use function var_dump;
use function version_compare;

Expand Down Expand Up @@ -1973,27 +1974,21 @@ private function waitForSnapshot(string $databaseName, string $collectionName):
$collection = new Collection($this->manager, $databaseName, $collectionName);
$session = $this->manager->startSession(['snapshot' => true]);

/* Retry until a snapshot query succeeds or ten seconds elapse,
* whichwever comes first.
*
* TODO: use hrtime() once the library requires PHP 7.3+ */
Copy link
Member

Choose a reason for hiding this comment

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

This todo makes sense.
You can also remove the microtime calls and count 10000 iterations with 1ms sleep.

Copy link
Member Author

Choose a reason for hiding this comment

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

count 10000 iterations with 1ms sleep.

Doing so wouldn't consider the time spent executing the FindOne operation.

This todo makes sense

I don't recall my original intention behind adding a TODO for hrtime() (it wasn't discussed in #900), but I gave this some thought when implementing this PR and couldn't see any reason to use it:

  • microtime() isn't deprecated and we don't need the precision offered by hrtime()
  • The variable return types for hrtime() seemed more complicated to work with
  • A monotonic timer is nice, but not necessary for a rarely used test method where we also don't need to worry about system clock changes

Side note: I did review the 7.3 polyfill while working on this PR.

Having said all that, we do have many uses of microtime() in the test suite:

  • DocumentationExamplesTest
  • FindFunctionalTest
  • WatchFunctionalTest
  • UnifiedSpecTests/EventCollector and Loop (these should remain as-is)

The first three instances all pertain to calculating durations (actual timestamp isn't relevant) so I wouldn't object if you want to create a ticket to integrate hrtime() there.

// Retry until a snapshot query succeeds or ten seconds elapse.
$retryUntil = microtime(true) + 10;

do {
try {
$collection->aggregate(
[['$match' => ['_id' => ['$exists' => true]]]],
['session' => $session]
);

break;
if ($collection->findOne([], ['session' => $session]) !== null) {
break;
}
} catch (CommandException $e) {
if ($e->getCode() === 246 /* SnapshotUnavailable */) {
continue;
if ($e->getCode() !== 246 /* SnapshotUnavailable */) {
throw $e;
}

throw $e;
}

usleep(1000);
} while (microtime(true) < $retryUntil);
}

Expand Down