Skip to content

POC: lightweight subprocess isolation via pcntl_fork() #5751

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
wire annotations
  • Loading branch information
staabm committed Apr 14, 2024
commit 3628c7b6728614eaac2ddbc0e49fb1a7cc04d288
2 changes: 1 addition & 1 deletion src/Framework/Attributes/RunClassInSeparateProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
private ?bool $forkIfPossible;

public function __construct(bool $forkIfPossible = null)
public function __construct(?bool $forkIfPossible = null)
{
$this->forkIfPossible = $forkIfPossible;
}
Expand Down
63 changes: 61 additions & 2 deletions src/Framework/TestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use PHPUnit\Metadata\ExcludeStaticPropertyFromBackup;
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
use PHPUnit\Metadata\PreserveGlobalState;
use PHPUnit\Metadata\RunClassInSeparateProcess;
use PHPUnit\Metadata\RunInSeparateProcess;
use PHPUnit\Metadata\RunTestsInSeparateProcesses;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use ReflectionClass;

Expand Down Expand Up @@ -51,6 +54,7 @@
$this->shouldTestMethodBeRunInSeparateProcess($className, $methodName),
$this->shouldGlobalStateBePreserved($className, $methodName),
$this->shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess($className),
$this->shouldForkIfPossible($className, $methodName),
$this->backupSettings($className, $methodName),
$groups,
);
Expand All @@ -64,6 +68,7 @@
$this->shouldTestMethodBeRunInSeparateProcess($className, $methodName),
$this->shouldGlobalStateBePreserved($className, $methodName),
$this->shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess($className),
$this->shouldForkIfPossible($className, $methodName),
$this->backupSettings($className, $methodName),
);

Expand All @@ -76,7 +81,7 @@
* @psalm-param array{backupGlobals: ?bool, backupGlobalsExcludeList: list<string>, backupStaticProperties: ?bool, backupStaticPropertiesExcludeList: array<string,list<string>>} $backupSettings
* @psalm-param list<non-empty-string> $groups
*/
private function buildDataProviderTestSuite(string $methodName, string $className, array $data, bool $runTestInSeparateProcess, ?bool $preserveGlobalState, bool $runClassInSeparateProcess, array $backupSettings, array $groups): DataProviderTestSuite
private function buildDataProviderTestSuite(string $methodName, string $className, array $data, bool $runTestInSeparateProcess, ?bool $preserveGlobalState, bool $runClassInSeparateProcess, bool $forkIfPossible, array $backupSettings, array $groups): DataProviderTestSuite
{
$dataProviderTestSuite = DataProviderTestSuite::empty(
$className . '::' . $methodName,
Expand All @@ -98,6 +103,7 @@
$runTestInSeparateProcess,
$preserveGlobalState,
$runClassInSeparateProcess,
$forkIfPossible,
$backupSettings,
);

Expand All @@ -110,7 +116,7 @@
/**
* @psalm-param array{backupGlobals: ?bool, backupGlobalsExcludeList: list<string>, backupStaticProperties: ?bool, backupStaticPropertiesExcludeList: array<string,list<string>>} $backupSettings
*/
private function configureTestCase(TestCase $test, bool $runTestInSeparateProcess, ?bool $preserveGlobalState, bool $runClassInSeparateProcess, array $backupSettings): void
private function configureTestCase(TestCase $test, bool $runTestInSeparateProcess, ?bool $preserveGlobalState, bool $runClassInSeparateProcess, bool $forkIfPossible, array $backupSettings): void
{
if ($runTestInSeparateProcess) {
$test->setRunTestInSeparateProcess(true);
Expand All @@ -120,6 +126,10 @@
$test->setRunClassInSeparateProcess(true);
}

if ($forkIfPossible) {
$test->setForkIfPossible(true);
}

if ($preserveGlobalState !== null) {
$test->setPreserveGlobalState($preserveGlobalState);
}
Expand Down Expand Up @@ -272,4 +282,53 @@
{
return MetadataRegistry::parser()->forClass($className)->isRunClassInSeparateProcess()->isNotEmpty();
}

/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*/
private function shouldForkIfPossible(string $className, string $methodName): bool
{
$metadataForMethod = MetadataRegistry::parser()->forMethod($className, $methodName);

if ($metadataForMethod->isRunInSeparateProcess()->isNotEmpty()) {
$metadata = $metadataForMethod->isRunInSeparateProcess()->asArray()[0];

assert($metadata instanceof RunInSeparateProcess);

$forkIfPossible = $metadata->forkIfPossible();

if ($forkIfPossible !== null) {
return $forkIfPossible;
}
}

$metadataForClass = MetadataRegistry::parser()->forClass($className);

if ($metadataForClass->isRunTestsInSeparateProcesses()->isNotEmpty()) {
$metadata = $metadataForClass->isRunTestsInSeparateProcesses()->asArray()[0];

assert($metadata instanceof RunTestsInSeparateProcesses);

$forkIfPossible = $metadata->forkIfPossible();

if ($forkIfPossible !== null) {
return $forkIfPossible;

Check warning on line 316 in src/Framework/TestBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/TestBuilder.php#L316

Added line #L316 was not covered by tests
}
}

if ($metadataForClass->isRunClassInSeparateProcess()->isNotEmpty()) {
$metadata = $metadataForClass->isRunClassInSeparateProcess()->asArray()[0];

assert($metadata instanceof RunClassInSeparateProcess);

$forkIfPossible = $metadata->forkIfPossible();

if ($forkIfPossible !== null) {
return $forkIfPossible;
}
}

return false;
}
}
10 changes: 10 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
*/
private ?array $backupGlobalExceptionHandlers = null;
private ?bool $runClassInSeparateProcess = null;
private ?bool $forkIfPossible = null;
private ?bool $runTestInSeparateProcess = null;
private bool $preserveGlobalState = false;
private bool $inIsolation = false;
Expand Down Expand Up @@ -340,6 +341,7 @@ final public function run(): void
$this,
$this->runClassInSeparateProcess && !$this->runTestInSeparateProcess,
$this->preserveGlobalState,
$this->forkIfPossible === true,
);
}
}
Expand Down Expand Up @@ -709,6 +711,14 @@ final public function setRunClassInSeparateProcess(bool $runClassInSeparateProce
$this->runClassInSeparateProcess = $runClassInSeparateProcess;
}

/**
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
final public function setForkIfPossible(bool $forkIfPossible): void
{
$this->forkIfPossible = $forkIfPossible;
}

/**
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@
* @throws ProcessIsolationException
* @throws StaticAnalysisCacheNotConfiguredException
*/
public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool $preserveGlobalState): void
public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool $preserveGlobalState, bool $forkIfPossible): void
{
if (PcntlFork::isPcntlForkAvailable()) {
if ($forkIfPossible && PcntlFork::isPcntlForkAvailable()) {
// forking the parent process is a more lightweight way to run a test in isolation.
// it requires the pcntl extension though.
$fork = new PcntlFork;
$fork->runTest($test);

Check warning on line 258 in src/Framework/TestRunner.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/TestRunner.php#L257-L258

Added lines #L257 - L258 were not covered by tests

return;

Check warning on line 260 in src/Framework/TestRunner.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/TestRunner.php#L260

Added line #L260 was not covered by tests
}

$class = new ReflectionClass($test);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Metadata\Attribute;

use PHPUnit\Framework\Attributes\RunClassInSeparateProcess;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

#[RunClassInSeparateProcess(true)]
#[RunTestsInSeparateProcesses]
final class ProcessIsolationForkedTest extends TestCase
{
#[RunInSeparateProcess]
public function testOne(): void
{
}
}
27 changes: 27 additions & 0 deletions tests/_files/TestWithClassLevelIsolationAttributesForked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\TestBuilder;

use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\BackupStaticProperties;
use PHPUnit\Framework\Attributes\RunClassInSeparateProcess;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

#[BackupGlobals(true)]
#[BackupStaticProperties(true)]
#[RunClassInSeparateProcess]
#[RunTestsInSeparateProcesses(true)]
final class TestWithClassLevelIsolationAttributesForked extends TestCase
{
public function testOne(): void
{
}
}
25 changes: 25 additions & 0 deletions tests/_files/TestWithMethodLevelIsolationAttributesForked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\TestBuilder;

use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\BackupStaticProperties;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;

final class TestWithMethodLevelIsolationAttributes extends TestCase
{
#[BackupGlobals(true)]
#[BackupStaticProperties(true)]
#[RunInSeparateProcess(true)]
public function testOne(): void
{
}
}
Loading