Skip to content

Commit 8625946

Browse files
committed
Disable fail points after tests
1 parent 5d22ccb commit 8625946

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

tests/UnifiedSpecTests/Context.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use stdClass;
1111
use function array_key_exists;
1212
use function assertArrayHasKey;
13+
use function assertContains;
1314
use function assertCount;
1415
use function assertInternalType;
1516
use function assertNotEmpty;
@@ -413,6 +414,6 @@ private static function requireMultipleMongoses(string $uri)
413414
return;
414415
}
415416

416-
assertStringContains(',', parse_url($uri, PHP_URL_HOST));
417+
assertContains(',', parse_url($uri, PHP_URL_HOST));
417418
}
418419
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\UnifiedSpecTests;
4+
5+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
6+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
7+
use MongoDB\Driver\Monitoring\CommandSubscriber;
8+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
9+
use MongoDB\Operation\DatabaseCommand;
10+
use function MongoDB\Driver\Monitoring\addSubscriber;
11+
use function MongoDB\Driver\Monitoring\removeSubscriber;
12+
13+
class FailPointObserver implements CommandSubscriber
14+
{
15+
/** @var array */
16+
private $failPointsAndServers = [];
17+
18+
/**
19+
* @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandfailed.php
20+
*/
21+
public function commandFailed(CommandFailedEvent $event)
22+
{
23+
}
24+
25+
/**
26+
* @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandstarted.php
27+
*/
28+
public function commandStarted(CommandStartedEvent $event)
29+
{
30+
$command = $event->getCommand();
31+
32+
if (! isset($command->configureFailPoint)) {
33+
return;
34+
}
35+
36+
if (isset($command->mode) && $command->mode === 'off') {
37+
return;
38+
}
39+
40+
$this->failPointsAndServers[] = [$command->configureFailPoint, $event->getServer()];
41+
}
42+
43+
/**
44+
* @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandsucceeded.php
45+
*/
46+
public function commandSucceeded(CommandSucceededEvent $event)
47+
{
48+
}
49+
50+
public function disableFailPoints()
51+
{
52+
foreach ($this->failPointsAndServers as list($failPoint, $server)) {
53+
$operation = new DatabaseCommand('admin', ['configureFailPoint' => $failPoint, 'mode' => 'off']);
54+
$operation->execute($server);
55+
}
56+
57+
$this->failPointsAndServers = [];
58+
}
59+
60+
public function start()
61+
{
62+
addSubscriber($this);
63+
}
64+
65+
public function stop()
66+
{
67+
removeSubscriber($this);
68+
}
69+
}

tests/UnifiedSpecTests/UnifiedSpecTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,25 @@ class UnifiedSpecTest extends FunctionalTestCase
3838
/** @var MongoDB\Client */
3939
private static $internalClient;
4040

41+
/** @var FailPointObserver */
42+
private $failPointObserver;
43+
4144
private static function doSetUpBeforeClass()
4245
{
4346
parent::setUpBeforeClass();
4447

48+
/* TODO: FunctionalTestCase::getUri() restricts to a single mongos by
49+
* default. Determine if there's a need to override that. */
4550
self::$internalClient = new Client(static::getUri());
4651
self::killAllSessions();
4752
}
4853

4954
private function doSetUp()
5055
{
5156
parent::setUp();
57+
58+
$this->failPointObserver = new FailPointObserver();
59+
$this->failPointObserver->start();
5260
}
5361

5462
private function doTearDown()
@@ -57,6 +65,9 @@ private function doTearDown()
5765
self::killAllSessions();
5866
}
5967

68+
$this->failPointObserver->stop();
69+
$this->failPointObserver->disableFailPoints();
70+
6071
parent::tearDown();
6172
}
6273

@@ -87,7 +98,8 @@ public function testPassingTests(stdClass $test, string $schemaVersion, array $r
8798
$this->prepareInitialData($initialData);
8899
}
89100

90-
$context = new Context(self::$internalClient, static::getUri());
101+
// Give Context unmodified URI so it can enforce useMultipleMongoses
102+
$context = new Context(self::$internalClient, static::getUri(true));
91103

92104
if (isset($createEntities)) {
93105
$context->createEntities($createEntities);

0 commit comments

Comments
 (0)