-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on triggering PHP's garbage collector after each n-th test
- Loading branch information
1 parent
1e784dd
commit f132c80
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Runner\GarbageCollection; | ||
|
||
use function gc_collect_cycles; | ||
use function gc_disable; | ||
use function gc_enable; | ||
use PHPUnit\Event\EventFacadeIsSealedException; | ||
use PHPUnit\Event\Facade; | ||
use PHPUnit\Event\UnknownSubscriberTypeException; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class GarbageCollectionHandler | ||
{ | ||
private readonly int $threshold; | ||
private int $tests = 0; | ||
|
||
/** | ||
* @throws EventFacadeIsSealedException | ||
* @throws UnknownSubscriberTypeException | ||
*/ | ||
public function __construct(Facade $facade, int $threshold) | ||
{ | ||
$this->threshold = $threshold; | ||
|
||
$this->registerSubscribers($facade); | ||
} | ||
|
||
public function executionStarted(): void | ||
{ | ||
gc_disable(); | ||
gc_collect_cycles(); | ||
} | ||
|
||
public function executionFinished(): void | ||
{ | ||
gc_collect_cycles(); | ||
gc_enable(); | ||
} | ||
|
||
public function testFinished(): void | ||
{ | ||
$this->tests++; | ||
|
||
if ($this->tests === $this->threshold) { | ||
gc_collect_cycles(); | ||
|
||
$this->tests = 0; | ||
} | ||
} | ||
|
||
/** | ||
* @throws EventFacadeIsSealedException | ||
* @throws UnknownSubscriberTypeException | ||
*/ | ||
private function registerSubscribers(Facade $facade): void | ||
{ | ||
$facade->registerSubscribers( | ||
new ExecutionStartedSubscriber($this), | ||
new ExecutionFinishedSubscriber($this), | ||
new TestFinishedSubscriber($this), | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Runner/GarbageCollection/Subscriber/ExecutionFinishedSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Runner\GarbageCollection; | ||
|
||
use PHPUnit\Event\InvalidArgumentException; | ||
use PHPUnit\Event\TestRunner\ExecutionFinished; | ||
use PHPUnit\Event\TestRunner\ExecutionFinishedSubscriber as TestRunnerExecutionFinishedSubscriber; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class ExecutionFinishedSubscriber extends Subscriber implements TestRunnerExecutionFinishedSubscriber | ||
{ | ||
/** | ||
* @throws \PHPUnit\Framework\InvalidArgumentException | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function notify(ExecutionFinished $event): void | ||
{ | ||
$this->handler()->executionFinished(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Runner/GarbageCollection/Subscriber/ExecutionStartedSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Runner\GarbageCollection; | ||
|
||
use PHPUnit\Event\InvalidArgumentException; | ||
use PHPUnit\Event\TestRunner\ExecutionStarted; | ||
use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber as TestRunnerExecutionStartedSubscriber; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class ExecutionStartedSubscriber extends Subscriber implements TestRunnerExecutionStartedSubscriber | ||
{ | ||
/** | ||
* @throws \PHPUnit\Framework\InvalidArgumentException | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function notify(ExecutionStarted $event): void | ||
{ | ||
$this->handler()->executionStarted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Runner\GarbageCollection; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
abstract class Subscriber | ||
{ | ||
private readonly GarbageCollectionHandler $handler; | ||
|
||
public function __construct(GarbageCollectionHandler $handler) | ||
{ | ||
$this->handler = $handler; | ||
} | ||
|
||
protected function handler(): GarbageCollectionHandler | ||
{ | ||
return $this->handler; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Runner/GarbageCollection/Subscriber/TestFinishedSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Runner\GarbageCollection; | ||
|
||
use PHPUnit\Event\InvalidArgumentException; | ||
use PHPUnit\Event\Test\Finished; | ||
use PHPUnit\Event\Test\FinishedSubscriber; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class TestFinishedSubscriber extends Subscriber implements FinishedSubscriber | ||
{ | ||
/** | ||
* @throws \PHPUnit\Framework\InvalidArgumentException | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function notify(Finished $event): void | ||
{ | ||
$this->handler()->testFinished(); | ||
} | ||
} |