From c6f9f0043ee96e98a7fcfc1ea960129fcfea7fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 19 Sep 2024 08:55:37 +0200 Subject: [PATCH 1/4] Fix CursorId deprecation in tests --- tests/Model/CodecCursorFunctionalTest.php | 7 +++++-- tests/Operation/WatchFunctionalTest.php | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/Model/CodecCursorFunctionalTest.php b/tests/Model/CodecCursorFunctionalTest.php index 432d71732..48fd2cb59 100644 --- a/tests/Model/CodecCursorFunctionalTest.php +++ b/tests/Model/CodecCursorFunctionalTest.php @@ -55,12 +55,15 @@ function (...$args) use (&$previousErrorHandler, &$deprecations) { E_USER_DEPRECATED | E_DEPRECATED, ); - $cursorId = $codecCursor->getId(); + $cursorId = $codecCursor->getId(true); } finally { restore_error_handler(); } - self::assertInstanceOf(CursorId::class, $cursorId); + self::logicalOr( + self::isInstanceOf(Int64::class), + self::isInstanceOf(CursorId::class), + )->matches($cursorId); // Expect 2 deprecations: 1 from CodecCursor, one from Cursor self::assertCount(2, $deprecations); diff --git a/tests/Operation/WatchFunctionalTest.php b/tests/Operation/WatchFunctionalTest.php index 58b8c844c..deb9d976d 100644 --- a/tests/Operation/WatchFunctionalTest.php +++ b/tests/Operation/WatchFunctionalTest.php @@ -720,7 +720,7 @@ public function testInitialCursorIsNotClosed(): void * reports the cursor as alive. While the cursor ID is accessed through * ChangeStream, we'll need to use reflection to access the internal * Cursor and call isDead(). */ - $this->assertNotEquals('0', (string) $changeStream->getCursorId()); + $this->assertNotEquals('0', (string) $changeStream->getCursorId(true)); $rc = new ReflectionClass(ChangeStream::class); $rp = $rc->getProperty('iterator'); @@ -1370,11 +1370,11 @@ public function testOriginalReadPreferenceIsPreservedOnResume(): void } $changeStream = $operation->execute($secondary); - $previousCursorId = $changeStream->getCursorId(); + $previousCursorId = $changeStream->getCursorId(true); $this->forceChangeStreamResume(); $changeStream->next(); - $this->assertNotSame($previousCursorId, $changeStream->getCursorId()); + $this->assertNotSame($previousCursorId, $changeStream->getCursorId(true)); $getCursor = Closure::bind( fn () => $this->iterator->getInnerIterator(), From 8c0ff420ce0031c202421a239a5c336cc56be564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 19 Sep 2024 09:33:52 +0200 Subject: [PATCH 2/4] Compare serverConnectionId instead of Server instance as Event::getServer is deprecated --- .../RetryableReads/Prose2_RetryOnMongosTest.php | 13 +++++++------ .../Prose4_RetryOnDifferentMongosTest.php | 5 +++-- .../Prose5_RetryOnSameMongosTest.php | 10 +++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php b/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php index a740b43b6..3ae204af6 100644 --- a/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php +++ b/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php @@ -65,7 +65,8 @@ public function testRetryOnDifferentMongos(): void // Step 4: Enable failed command event monitoring for client $subscriber = new class implements CommandSubscriber { - public $commandFailedServers = []; + /** @var int[] */ + public array $commandFailedServers = []; public function commandStarted(CommandStartedEvent $event): void { @@ -77,7 +78,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void public function commandFailed(CommandFailedEvent $event): void { - $this->commandFailedServers[] = $event->getServer(); + $this->commandFailedServers[] = $event->getServerConnectionId(); } }; @@ -130,8 +131,8 @@ public function testRetryOnSameMongos(): void // Step 4: Enable succeeded and failed command event monitoring $subscriber = new class implements CommandSubscriber { - public Server $commandSucceededServer; - public Server $commandFailedServer; + public ?int $commandSucceededServer = null; + public ?int $commandFailedServer = null; public function commandStarted(CommandStartedEvent $event): void { @@ -139,12 +140,12 @@ public function commandStarted(CommandStartedEvent $event): void public function commandSucceeded(CommandSucceededEvent $event): void { - $this->commandSucceededServer = $event->getServer(); + $this->commandSucceededServer = $event->getServerConnectionId(); } public function commandFailed(CommandFailedEvent $event): void { - $this->commandFailedServer = $event->getServer(); + $this->commandFailedServer = $event->getServerConnectionId(); } }; diff --git a/tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php b/tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php index 02b4db6ec..6d2fd7d50 100644 --- a/tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php +++ b/tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php @@ -67,7 +67,8 @@ public function testRetryOnDifferentMongos(): void // Step 4: Enable failed command event monitoring for client $subscriber = new class implements CommandSubscriber { - public $commandFailedServers = []; + /** @var int[] */ + public array $commandFailedServers = []; public function commandStarted(CommandStartedEvent $event): void { @@ -79,7 +80,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void public function commandFailed(CommandFailedEvent $event): void { - $this->commandFailedServers[] = $event->getServer(); + $this->commandFailedServers[] = $event->getServerConnectionId(); } }; diff --git a/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php b/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php index 77f4cdac5..c5ccf251c 100644 --- a/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php +++ b/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php @@ -50,8 +50,8 @@ public function testRetryOnSameMongos(): void // Step 4: Enable succeeded and failed command event monitoring $subscriber = new class implements CommandSubscriber { - public Server $commandSucceededServer; - public Server $commandFailedServer; + public ?int $commandSucceededServer = null; + public ?int $commandFailedServer = null; public function commandStarted(CommandStartedEvent $event): void { @@ -59,12 +59,12 @@ public function commandStarted(CommandStartedEvent $event): void public function commandSucceeded(CommandSucceededEvent $event): void { - $this->commandSucceededServer = $event->getServer(); + $this->commandSucceededServer = $event->getServerConnectionId(); } public function commandFailed(CommandFailedEvent $event): void { - $this->commandFailedServer = $event->getServer(); + $this->commandFailedServer = $event->getServerConnectionId(); } }; @@ -78,7 +78,7 @@ public function commandFailed(CommandFailedEvent $event): void /* Step 6: Assert that exactly one failed command event and one * succeeded command event occurred. Assert that both events occurred on - * the same mongos. */ + * the same mongos connection. */ $this->assertNotNull($subscriber->commandSucceededServer); $this->assertNotNull($subscriber->commandFailedServer); $this->assertEquals($subscriber->commandSucceededServer, $subscriber->commandFailedServer); From 63641735d255d3732e5adcfc47a0d738f2e38442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 19 Sep 2024 09:34:27 +0200 Subject: [PATCH 3/4] Log serverConnectionId instead of a pseudo connection string as Event::getServer is deprecated --- tests/UnifiedSpecTests/EventCollector.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/UnifiedSpecTests/EventCollector.php b/tests/UnifiedSpecTests/EventCollector.php index 0a688e41a..d73767a05 100644 --- a/tests/UnifiedSpecTests/EventCollector.php +++ b/tests/UnifiedSpecTests/EventCollector.php @@ -120,7 +120,7 @@ private function handleCommandMonitoringEvent($event): void 'name' => self::getEventName($event), 'observedAt' => microtime(true), 'commandName' => $event->getCommandName(), - 'connectionId' => self::getConnectionId($event), + 'connectionId' => $event->getServerConnectionId(), 'requestId' => $event->getRequestId(), 'operationId' => $event->getOperationId(), ]; @@ -144,14 +144,6 @@ private function handleCommandMonitoringEvent($event): void $this->eventList[] = $log; } - /** @param CommandStartedEvent|CommandSucceededEvent|CommandFailedEvent $event */ - private static function getConnectionId($event): string - { - $server = $event->getServer(); - - return sprintf('%s:%d', $server->getHost(), $server->getPort()); - } - private static function getEventName(object $event): string { static $eventNamesByClass = null; From e3a370d9d7655061bd1805d4237702f360d7397b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 19 Sep 2024 09:39:44 +0200 Subject: [PATCH 4/4] Deprecations are tested --- tests/Model/CodecCursorFunctionalTest.php | 7 ++----- .../SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php | 1 - .../RetryableWrites/Prose5_RetryOnSameMongosTest.php | 1 - tests/UnifiedSpecTests/EventCollector.php | 1 - 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/Model/CodecCursorFunctionalTest.php b/tests/Model/CodecCursorFunctionalTest.php index 48fd2cb59..432d71732 100644 --- a/tests/Model/CodecCursorFunctionalTest.php +++ b/tests/Model/CodecCursorFunctionalTest.php @@ -55,15 +55,12 @@ function (...$args) use (&$previousErrorHandler, &$deprecations) { E_USER_DEPRECATED | E_DEPRECATED, ); - $cursorId = $codecCursor->getId(true); + $cursorId = $codecCursor->getId(); } finally { restore_error_handler(); } - self::logicalOr( - self::isInstanceOf(Int64::class), - self::isInstanceOf(CursorId::class), - )->matches($cursorId); + self::assertInstanceOf(CursorId::class, $cursorId); // Expect 2 deprecations: 1 from CodecCursor, one from Cursor self::assertCount(2, $deprecations); diff --git a/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php b/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php index 3ae204af6..958e74b07 100644 --- a/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php +++ b/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php @@ -7,7 +7,6 @@ use MongoDB\Driver\Monitoring\CommandStartedEvent; use MongoDB\Driver\Monitoring\CommandSubscriber; use MongoDB\Driver\Monitoring\CommandSucceededEvent; -use MongoDB\Driver\Server; use MongoDB\Tests\SpecTests\FunctionalTestCase; use function assert; diff --git a/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php b/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php index c5ccf251c..7357b30f2 100644 --- a/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php +++ b/tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php @@ -6,7 +6,6 @@ use MongoDB\Driver\Monitoring\CommandStartedEvent; use MongoDB\Driver\Monitoring\CommandSubscriber; use MongoDB\Driver\Monitoring\CommandSucceededEvent; -use MongoDB\Driver\Server; use MongoDB\Tests\SpecTests\FunctionalTestCase; /** diff --git a/tests/UnifiedSpecTests/EventCollector.php b/tests/UnifiedSpecTests/EventCollector.php index d73767a05..7e31328dd 100644 --- a/tests/UnifiedSpecTests/EventCollector.php +++ b/tests/UnifiedSpecTests/EventCollector.php @@ -18,7 +18,6 @@ use function PHPUnit\Framework\assertIsObject; use function PHPUnit\Framework\assertIsString; use function PHPUnit\Framework\assertNotEmpty; -use function sprintf; /** * EventCollector handles "storeEventsAsEntities" for client entities.