Skip to content

Commit

Permalink
Initial work on triggering PHP's garbage collector after each n-th test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed May 26, 2023
1 parent 8828f3d commit 5ec000f
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Runner/GarbageCollection/GarbageCollectionHandler.php
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),
);
}
}
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();
}
}
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();
}
}
28 changes: 28 additions & 0 deletions src/Runner/GarbageCollection/Subscriber/Subscriber.php
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 src/Runner/GarbageCollection/Subscriber/TestFinishedSubscriber.php
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();
}
}

0 comments on commit 5ec000f

Please sign in to comment.