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

Merge v1.x into v2.x #1431

Merged
merged 2 commits into from
Sep 23, 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
2 changes: 2 additions & 0 deletions tests/UnifiedSpecTests/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
*/
final class Context
{
use ManagesFailPointsTrait;

private ?string $activeClient = null;

private EntityMap $entityMap;
Expand Down
63 changes: 0 additions & 63 deletions tests/UnifiedSpecTests/FailPointObserver.php

This file was deleted.

40 changes: 40 additions & 0 deletions tests/UnifiedSpecTests/ManagesFailPointsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MongoDB\Tests\UnifiedSpecTests;

use MongoDB\Driver\Server;
use MongoDB\Operation\DatabaseCommand;
use stdClass;

use function PHPUnit\Framework\assertIsString;
use function PHPUnit\Framework\assertObjectHasAttribute;

trait ManagesFailPointsTrait
{
/** @var list<list{string, Server}> */
private array $failPointsAndServers = [];

public function configureFailPoint(stdClass $failPoint, Server $server): void
{
assertObjectHasAttribute('configureFailPoint', $failPoint);
assertIsString($failPoint->configureFailPoint);
assertObjectHasAttribute('mode', $failPoint);

$operation = new DatabaseCommand('admin', $failPoint);
$operation->execute($server);

if ($failPoint->mode !== 'off') {
$this->failPointsAndServers[] = [$failPoint->configureFailPoint, $server];
}
}

public function disableFailPoints(): void
{
foreach ($this->failPointsAndServers as [$failPoint, $server]) {
$operation = new DatabaseCommand('admin', ['configureFailPoint' => $failPoint, 'mode' => 'off']);
$operation->execute($server);
}

$this->failPointsAndServers = [];
}
}
8 changes: 4 additions & 4 deletions tests/UnifiedSpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use MongoDB\Model\CollectionInfo;
use MongoDB\Model\DatabaseInfo;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\DatabaseCommand;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -905,16 +904,17 @@ private function executeForTestRunner()
assertArrayHasKey('failPoint', $args);
assertInstanceOf(Client::class, $args['client']);
assertInstanceOf(stdClass::class, $args['failPoint']);
$args['client']->selectDatabase('admin')->command($args['failPoint']);
// Configure the fail point via the Context so it can later be disabled
$this->context->configureFailPoint($args['failPoint'], $args['client']->getManager()->selectServer());
break;
case 'targetedFailPoint':
assertArrayHasKey('session', $args);
assertArrayHasKey('failPoint', $args);
assertInstanceOf(Session::class, $args['session']);
assertInstanceOf(stdClass::class, $args['failPoint']);
assertNotNull($args['session']->getServer(), 'Session is pinned');
$operation = new DatabaseCommand('admin', $args['failPoint']);
$operation->execute($args['session']->getServer());
// Configure the fail point via the Context so it can later be disabled
$this->context->configureFailPoint($args['failPoint'], $args['session']->getServer());
break;
case 'loop':
assertArrayHasKey('operations', $args);
Expand Down
23 changes: 9 additions & 14 deletions tests/UnifiedSpecTests/UnifiedTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ final class UnifiedTestRunner
/** @var callable(EntityMap):void */
private $entityMapObserver;

private ?FailPointObserver $failPointObserver = null;

private ServerParameterHelper $serverParameterHelper;

public function __construct(private string $internalClientUri)
Expand Down Expand Up @@ -147,9 +145,6 @@ private function doSetUp(): void
* after transient error within a transaction" pinning test causes the
* subsequent transaction test to block. */
$this->killAllSessions();

$this->failPointObserver = new FailPointObserver();
$this->failPointObserver->start();
}

private function doTearDown(bool $hasFailed): void
Expand All @@ -160,9 +155,6 @@ private function doTearDown(bool $hasFailed): void
$this->killAllSessions();
}

$this->failPointObserver->stop();
$this->failPointObserver->disableFailPoints();

/* Manually invoking garbage collection since each test is prone to
* create cycles (perhaps due to EntityMap), which can leak and prevent
* sessions from being released back into the pool. */
Expand Down Expand Up @@ -213,14 +205,17 @@ private function doTestCase(stdClass $test, string $schemaVersion, ?array $runOn
$context->startEventObservers();
$context->startEventCollectors();

foreach ($test->operations as $o) {
$operation = new Operation($o, $context);
$operation->assert();
try {
foreach ($test->operations as $o) {
$operation = new Operation($o, $context);
$operation->assert();
}
} finally {
$context->stopEventObservers();
$context->stopEventCollectors();
$context->disableFailPoints();
}

$context->stopEventObservers();
$context->stopEventCollectors();

if (isset($test->expectEvents)) {
assertIsArray($test->expectEvents);
$context->assertExpectedEventsForClients($test->expectEvents);
Expand Down