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 27, 2023
1 parent 1e784dd commit f132c80
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)

Check warning on line 31 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L31

Added line #L31 was not covered by tests
{
$this->threshold = $threshold;

Check warning on line 33 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L33

Added line #L33 was not covered by tests

$this->registerSubscribers($facade);

Check warning on line 35 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L35

Added line #L35 was not covered by tests
}

public function executionStarted(): void

Check warning on line 38 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L38

Added line #L38 was not covered by tests
{
gc_disable();
gc_collect_cycles();

Check warning on line 41 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L40-L41

Added lines #L40 - L41 were not covered by tests
}

public function executionFinished(): void

Check warning on line 44 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L44

Added line #L44 was not covered by tests
{
gc_collect_cycles();
gc_enable();

Check warning on line 47 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L46-L47

Added lines #L46 - L47 were not covered by tests
}

public function testFinished(): void

Check warning on line 50 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L50

Added line #L50 was not covered by tests
{
$this->tests++;

Check warning on line 52 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L52

Added line #L52 was not covered by tests

if ($this->tests === $this->threshold) {
gc_collect_cycles();

Check warning on line 55 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L54-L55

Added lines #L54 - L55 were not covered by tests

$this->tests = 0;

Check warning on line 57 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L57

Added line #L57 was not covered by tests
}
}

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
private function registerSubscribers(Facade $facade): void

Check warning on line 65 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L65

Added line #L65 was not covered by tests
{
$facade->registerSubscribers(
new ExecutionStartedSubscriber($this),
new ExecutionFinishedSubscriber($this),
new TestFinishedSubscriber($this),
);

Check warning on line 71 in src/Runner/GarbageCollection/GarbageCollectionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/GarbageCollectionHandler.php#L67-L71

Added lines #L67 - L71 were not covered by tests
}
}
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

Check warning on line 25 in src/Runner/GarbageCollection/Subscriber/ExecutionFinishedSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/ExecutionFinishedSubscriber.php#L25

Added line #L25 was not covered by tests
{
$this->handler()->executionFinished();

Check warning on line 27 in src/Runner/GarbageCollection/Subscriber/ExecutionFinishedSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/ExecutionFinishedSubscriber.php#L27

Added line #L27 was not covered by tests
}
}
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

Check warning on line 25 in src/Runner/GarbageCollection/Subscriber/ExecutionStartedSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/ExecutionStartedSubscriber.php#L25

Added line #L25 was not covered by tests
{
$this->handler()->executionStarted();

Check warning on line 27 in src/Runner/GarbageCollection/Subscriber/ExecutionStartedSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/ExecutionStartedSubscriber.php#L27

Added line #L27 was not covered by tests
}
}
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)

Check warning on line 19 in src/Runner/GarbageCollection/Subscriber/Subscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/Subscriber.php#L19

Added line #L19 was not covered by tests
{
$this->handler = $handler;

Check warning on line 21 in src/Runner/GarbageCollection/Subscriber/Subscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/Subscriber.php#L21

Added line #L21 was not covered by tests
}

protected function handler(): GarbageCollectionHandler

Check warning on line 24 in src/Runner/GarbageCollection/Subscriber/Subscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/Subscriber.php#L24

Added line #L24 was not covered by tests
{
return $this->handler;

Check warning on line 26 in src/Runner/GarbageCollection/Subscriber/Subscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/Subscriber.php#L26

Added line #L26 was not covered by tests
}
}
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

Check warning on line 25 in src/Runner/GarbageCollection/Subscriber/TestFinishedSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/TestFinishedSubscriber.php#L25

Added line #L25 was not covered by tests
{
$this->handler()->testFinished();

Check warning on line 27 in src/Runner/GarbageCollection/Subscriber/TestFinishedSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/GarbageCollection/Subscriber/TestFinishedSubscriber.php#L27

Added line #L27 was not covered by tests
}
}

0 comments on commit f132c80

Please sign in to comment.