From 50d1db71aa7ad6d8969ff48c0325ff5a7930f8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 19 Sep 2024 10:12:05 +0200 Subject: [PATCH] Fix CursorId deprecation in tests (#1423) --- tests/Operation/WatchFunctionalTest.php | 6 +++--- .../RetryableReads/Prose2_RetryOnMongosTest.php | 14 +++++++------- .../Prose4_RetryOnDifferentMongosTest.php | 5 +++-- .../Prose5_RetryOnSameMongosTest.php | 11 +++++------ tests/UnifiedSpecTests/EventCollector.php | 11 +---------- 5 files changed, 19 insertions(+), 28 deletions(-) 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(), diff --git a/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php b/tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php index a740b43b6..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; @@ -65,7 +64,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 +77,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void public function commandFailed(CommandFailedEvent $event): void { - $this->commandFailedServers[] = $event->getServer(); + $this->commandFailedServers[] = $event->getServerConnectionId(); } }; @@ -130,8 +130,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 +139,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..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; /** @@ -50,8 +49,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 +58,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 +77,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); diff --git a/tests/UnifiedSpecTests/EventCollector.php b/tests/UnifiedSpecTests/EventCollector.php index 0a688e41a..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. @@ -120,7 +119,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 +143,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;