-
Notifications
You must be signed in to change notification settings - Fork 0
ManagesFailPointsTrait and always stop event listeners #1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few assertions before we access object properties. I had these in for testing, but I suppose you can remove them if want to rely on the command succeeding. |
||
|
||
$operation = new DatabaseCommand('admin', $failPoint); | ||
$operation->execute($server); | ||
|
||
if ($failPoint->mode !== 'off') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe this is used in any tests, but it seemed reasonable to check for it since there's no point in disabling a fail point twice. |
||
$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 = []; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,11 +213,10 @@ private function doTestCase(stdClass $test, string $schemaVersion, ?array $runOn | |
$operation = new Operation($o, $context); | ||
$operation->assert(); | ||
} | ||
|
||
} finally { | ||
$context->stopEventObservers(); | ||
$context->stopEventCollectors(); | ||
} finally { | ||
$this->disableFailPoints($context->getFailPoints()); | ||
$context->disableFailPoints(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that I moved
It's possible that this could have resulted in memory leaks after failing tests. The subscriber would never get unregistered, and since they were registered globally they'd continue to accumulate in the process. Both EventCollector and EventObserver also hold a reference to a Context object, so those would also linger. I'm not sure if I put this change in a separate commit, so if you think it deserves its own PHPLIB ticket for tracking feel free to create that and incorporate it into the message when merging this. |
||
} | ||
|
||
if (isset($test->expectEvents)) { | ||
|
@@ -231,15 +230,6 @@ private function doTestCase(stdClass $test, string $schemaVersion, ?array $runOn | |
} | ||
} | ||
|
||
/** @param list<array{failPoint: stdClass, server: Server}> $failPoints */ | ||
private function disableFailPoints(array $failPoints): void | ||
{ | ||
foreach ($failPoints as ['failPoint' => $failPoint, 'server' => $server]) { | ||
$operation = new DatabaseCommand('admin', ['configureFailPoint' => $failPoint, 'mode' => 'off']); | ||
$operation->execute($server); | ||
} | ||
} | ||
|
||
/** | ||
* Checks server version and topology requirements. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your original PR you were storing the entire
configureFailPoint
command document. We only need the name of the fail point, so I've reduced this to a tuple like we originally had in FailPointObserver.