Skip to content

Commit cfa6c2b

Browse files
committed
Replace FailPointObserver with different logic
1 parent 50d1db7 commit cfa6c2b

File tree

4 files changed

+48
-80
lines changed

4 files changed

+48
-80
lines changed

tests/UnifiedSpecTests/Context.php

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use LogicException;
66
use MongoDB\Client;
77
use MongoDB\Driver\ClientEncryption;
8+
use MongoDB\Driver\Server;
89
use MongoDB\Driver\ServerApi;
910
use MongoDB\Model\BSONArray;
1011
use MongoDB\Tests\FunctionalTestCase;
@@ -60,6 +61,9 @@ final class Context
6061

6162
private ?object $advanceClusterTime = null;
6263

64+
/** @var list<array{failPoint: stdClass, server: Server}> */
65+
private array $failPoints = [];
66+
6367
public function __construct(Client $internalClient, string $uri)
6468
{
6569
$this->entityMap = new EntityMap();
@@ -232,6 +236,20 @@ public function stopEventCollectors(): void
232236
}
233237
}
234238

239+
public function registerFailPoint(stdClass $failPoint, Server $server): void
240+
{
241+
$this->failPoints[] = [
242+
'failPoint' => $failPoint,
243+
'server' => $server,
244+
];
245+
}
246+
247+
/** @return list<array{failPoint: stdClass, server: Server}> */
248+
public function getFailPoints(): array
249+
{
250+
return $this->failPoints;
251+
}
252+
235253
/** @param string|array $readPreferenceTags */
236254
private function convertReadPreferenceTags($readPreferenceTags): array
237255
{

tests/UnifiedSpecTests/FailPointObserver.php

-63
This file was deleted.

tests/UnifiedSpecTests/Operation.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -909,16 +909,16 @@ private function executeForTestRunner()
909909
assertArrayHasKey('failPoint', $args);
910910
assertInstanceOf(Client::class, $args['client']);
911911
assertInstanceOf(stdClass::class, $args['failPoint']);
912-
$args['client']->selectDatabase('admin')->command($args['failPoint']);
912+
$server = $args['client']->selectServer();
913+
$this->createFailPoint($args['failPoint'], $server);
913914
break;
914915
case 'targetedFailPoint':
915916
assertArrayHasKey('session', $args);
916917
assertArrayHasKey('failPoint', $args);
917918
assertInstanceOf(Session::class, $args['session']);
918919
assertInstanceOf(stdClass::class, $args['failPoint']);
919920
assertNotNull($args['session']->getServer(), 'Session is pinned');
920-
$operation = new DatabaseCommand('admin', $args['failPoint']);
921-
$operation->execute($args['session']->getServer());
921+
$this->createFailPoint($args['failPoint'], $args['session']->getServer());
922922
break;
923923
case 'loop':
924924
assertArrayHasKey('operations', $args);
@@ -937,6 +937,14 @@ private function executeForTestRunner()
937937
}
938938
}
939939

940+
private function createFailPoint(stdClass $failPoint, Server $server): void
941+
{
942+
$operation = new DatabaseCommand('admin', $failPoint);
943+
$operation->execute($server);
944+
945+
$this->context->registerFailPoint($failPoint, $server);
946+
}
947+
940948
private function getIndexNames(string $databaseName, string $collectionName): array
941949
{
942950
return array_map(

tests/UnifiedSpecTests/UnifiedTestRunner.php

+19-14
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ final class UnifiedTestRunner
7777
/** @var callable(EntityMap):void */
7878
private $entityMapObserver;
7979

80-
private ?FailPointObserver $failPointObserver = null;
81-
8280
private ServerParameterHelper $serverParameterHelper;
8381

8482
public function __construct(string $internalClientUri)
@@ -150,9 +148,6 @@ private function doSetUp(): void
150148
* after transient error within a transaction" pinning test causes the
151149
* subsequent transaction test to block. */
152150
$this->killAllSessions();
153-
154-
$this->failPointObserver = new FailPointObserver();
155-
$this->failPointObserver->start();
156151
}
157152

158153
private function doTearDown(bool $hasFailed): void
@@ -163,9 +158,6 @@ private function doTearDown(bool $hasFailed): void
163158
$this->killAllSessions();
164159
}
165160

166-
$this->failPointObserver->stop();
167-
$this->failPointObserver->disableFailPoints();
168-
169161
/* Manually invoking garbage collection since each test is prone to
170162
* create cycles (perhaps due to EntityMap), which can leak and prevent
171163
* sessions from being released back into the pool. */
@@ -216,13 +208,17 @@ private function doTestCase(stdClass $test, string $schemaVersion, ?array $runOn
216208
$context->startEventObservers();
217209
$context->startEventCollectors();
218210

219-
foreach ($test->operations as $o) {
220-
$operation = new Operation($o, $context);
221-
$operation->assert();
222-
}
211+
try {
212+
foreach ($test->operations as $o) {
213+
$operation = new Operation($o, $context);
214+
$operation->assert();
215+
}
223216

224-
$context->stopEventObservers();
225-
$context->stopEventCollectors();
217+
$context->stopEventObservers();
218+
$context->stopEventCollectors();
219+
} finally {
220+
$this->disableFailPoints($context->getFailPoints());
221+
}
226222

227223
if (isset($test->expectEvents)) {
228224
assertIsArray($test->expectEvents);
@@ -235,6 +231,15 @@ private function doTestCase(stdClass $test, string $schemaVersion, ?array $runOn
235231
}
236232
}
237233

234+
/** @param list<array{failPoint: stdClass, server: Server}> $failPoints */
235+
private function disableFailPoints(array $failPoints): void
236+
{
237+
foreach ($failPoints as ['failPoint' => $failPoint, 'server' => $server]) {
238+
$operation = new DatabaseCommand('admin', ['configureFailPoint' => $failPoint, 'mode' => 'off']);
239+
$operation->execute($server);
240+
}
241+
}
242+
238243
/**
239244
* Checks server version and topology requirements.
240245
*

0 commit comments

Comments
 (0)