Skip to content
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

Fix CursorId and Event::getServer() deprecation in tests #1423

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions tests/Operation/WatchFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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(),
Expand Down
14 changes: 7 additions & 7 deletions tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Copy link
Member

@jmikola jmikola Sep 19, 2024

Choose a reason for hiding this comment

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

Later, this was used for the following assertion:

/* Step 6: Assert that two failed command events occurred. Assert that
 * the failed command events occurred on different mongoses. */

Per getServerConnectionId(), the connection ID is unique within the context of a single server. It is also only reported by MongoDB 4.2+. I think the correct change here would have been to replace getServer() with a concatenation of the server host/port. I'll make the change in a subsequent PR.

As-is, I think this might sometimes fail if both mongoses reported the same connection ID or if we test on mongos 4.0.


public function commandStarted(CommandStartedEvent $event): void
{
Expand All @@ -77,7 +77,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void

public function commandFailed(CommandFailedEvent $event): void
{
$this->commandFailedServers[] = $event->getServer();
$this->commandFailedServers[] = $event->getServerConnectionId();
}
};

Expand Down Expand Up @@ -130,21 +130,21 @@ 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;
Copy link
Member

Choose a reason for hiding this comment

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

This relates the following assertion:

/* Step 6: Assert that exactly one failed command event and one
 * succeeded command event occurred. Assert that both events occurred on
 * the same mongos. */

Those rely on assertNotNull(), so this could also fail on a 4.0 server where connection IDs are not reported.


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();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -79,7 +80,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void

public function commandFailed(CommandFailedEvent $event): void
{
$this->commandFailedServers[] = $event->getServer();
$this->commandFailedServers[] = $event->getServerConnectionId();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -50,21 +49,21 @@ 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
{
}

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();
}
};

Expand All @@ -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);
Expand Down
11 changes: 1 addition & 10 deletions tests/UnifiedSpecTests/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
Copy link
Member Author

Choose a reason for hiding this comment

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

This logs and the $eventList property are never used. They can be useful for debugging.

Copy link
Member

@jmikola jmikola Sep 19, 2024

Choose a reason for hiding this comment

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

Quoting the Workload Executor Specification from drivers-atlas-testing:

each object is expected to have a name string field and an observedAt numeric field, in addition to any other fields specific to the event's type.

The only other field here that corresponds to an existing field in the Unified Test Format spec's expectedEvent section is commandName. That said, I think the original intention of connectionId here was from the client's perspective, so this should also change to a host/port combination. I'd also propose renaming this to server and adding serverConnectionId (which may not have existed at the time we implemented Atlas testing). I'll address that in my forthcoming PR to address my other feedback.

Copy link
Member

Choose a reason for hiding this comment

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

See: #1428

'requestId' => $event->getRequestId(),
'operationId' => $event->getOperationId(),
];
Expand All @@ -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;
Expand Down
Loading