Repository navigation
Fix memory leak: install module logger before the application is built #147
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
samdark
wants to merge
9
commits into
Codeception:master
Choose a base branch
from
samdark:fix-issue-78-logger-order
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
971b39e
Install module logger before building the application in startApp()
samdark 5a3644d
Restore module logger after application bootstrap
samdark bfb1f63
Defer yii\log shutdown registrations and run them on application reset
samdark c3d61e7
Store deferred shutdown args as a list
samdark 9facd8c
Address review findings on deferred shutdown handling
samdark 2d0e029
Support the modernized yii2-app-advanced template in tests
samdark dd5f5f2
Make the test suite pass PHPStan 2.3.x
samdark 8b4b342
Exclude generated actor actions from PHPStan and clean the mailer bin…
samdark 6039a30
Regenerate the PHPStan baseline against the rebuilt actor actions
samdark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace app\bootstrapLogger; | ||
|
|
||
| use yii\web\Application as BaseApplication; | ||
|
|
||
| final class App extends BaseApplication | ||
| { | ||
| } |
This file contains hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace app\bootstrapLogger; | ||
|
|
||
| use yii\log\Logger as YiiLogger; | ||
|
|
||
| /** | ||
| * A standalone global logger installed by the application's bootstrap callback. | ||
| * It overrides `init()` (as the module logger does) only to avoid the unrelated | ||
| * shutdown-registration retention root; the point is that the app replaces the | ||
| * global logger while it is being built. | ||
| */ | ||
| final class CustomLogger extends YiiLogger | ||
| { | ||
| public function init(): void | ||
| { | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| paths: | ||
| tests: . | ||
| output: ../../_output | ||
| data: _data | ||
| support: ../../_support | ||
| namespace: tests\ | ||
| modules: | ||
| config: | ||
| Yii2: | ||
| configFile: config.php | ||
| transaction: false | ||
| cleanup: false |
This file contains hidden or 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,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/app/App.php'; | ||
| require_once __DIR__ . '/app/CustomLogger.php'; | ||
|
|
||
| return [ | ||
| 'class' => app\bootstrapLogger\App::class, | ||
| 'id' => 'bootstrap-logger', | ||
| 'basePath' => __DIR__, | ||
| 'bootstrap' => [ | ||
| static function (): void { | ||
| \Yii::setLogger(new \app\bootstrapLogger\CustomLogger()); | ||
| }, | ||
| ], | ||
| 'components' => [ | ||
| 'request' => [ | ||
| 'cookieValidationKey' => 'bootstrap-logger', | ||
| 'enableCsrfValidation' => false, | ||
| ], | ||
| ], | ||
| ]; |
This file contains hidden or 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,7 @@ | ||
| actor: FunctionalTester | ||
| modules: | ||
| enabled: | ||
| - Yii2 | ||
| - Asserts | ||
| steps: | ||
| - Codeception\Step\ConditionalAssertion |
62 changes: 62 additions & 0 deletions
62
tests/cases/bootstrap-logger/functional/BootstrapLoggerCest.php
This file contains hidden or 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,62 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace tests; | ||
|
|
||
| use Codeception\Lib\Connector\Yii2\Logger; | ||
| use Codeception\Lib\Di; | ||
| use Codeception\Lib\ModuleContainer; | ||
| use Codeception\Module\Yii2 as Yii2Module; | ||
| use Codeception\Test\Unit; | ||
|
|
||
| /** | ||
| * Regression test for https://github.com/Codeception/module-yii2/issues/78. | ||
| * | ||
| * When an application's `bootstrap` callback replaces the global logger while the | ||
| * module builds the application, the module must keep reporting the messages | ||
| * emitted during the test. The connector installs the module logger before the | ||
| * application is built (so no plain `yii\log\Logger` is created mid-build, which | ||
| * is the retention root of issue78) and, after the build, restores it, because the | ||
| * application may have replaced the global logger during bootstrap (a `bootstrap` | ||
| * callback calling `Yii::setLogger()`, or a `logger` config entry). Without that | ||
| * restore the replacement stays global, the messages of the test go to the | ||
| * replacement, and `Module\Yii2::_failed()` — which reads its separately held | ||
| * logger — loses the `yii-log` failure report. | ||
| * | ||
| * This test drives the real module through its public `_before`/`_failed`/`_after` | ||
| * lifecycle with an app whose bootstrap installs a custom global logger: a | ||
| * `Yii::warning()` is emitted during the test and the `yii-log` report attached | ||
| * on failure must contain it. The global logger must be the module logger both | ||
| * while the application builds and afterwards. | ||
| */ | ||
| final class BootstrapLoggerCest | ||
| { | ||
| public function testFailedReportKeepsLogsEmittedAfterBootstrapLoggerReplacement(FunctionalTester $I): void | ||
| { | ||
| $modules = new ModuleContainer(new Di(), []); | ||
| $module = new Yii2Module($modules, [ | ||
| 'configFile' => __DIR__ . '/../config.php', | ||
| 'cleanup' => false, | ||
| 'transaction' => false, | ||
| ]); | ||
|
|
||
| $test = new Unit('testBootstrapLogger505'); | ||
|
|
||
| $module->_initialize(); | ||
| $module->_before($test); | ||
|
|
||
| // The global logger must be the module logger both while the application | ||
| // is being built and once it is done — i.e. the bootstrap did not leave its | ||
| // custom global logger in place. | ||
| $I->assertInstanceOf(Logger::class, \Yii::getLogger()); | ||
|
|
||
| \Yii::warning('bootstrap-logger-505', 'bootstrap-logger'); | ||
| $module->_failed($test, new \RuntimeException('intentional test failure')); | ||
|
|
||
| $report = $test->getMetadata()->getReports()['yii-log'] ?? ''; | ||
| $I->assertStringContainsString('bootstrap-logger-505', $report); | ||
|
|
||
| $module->_after($test); | ||
| } | ||
| } |
This file contains hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace app\leak; | ||
|
|
||
| use ReflectionProperty; | ||
| use yii\web\Application as BaseApplication; | ||
|
|
||
| /** | ||
| * Test double that records the class of the logger installed while the application | ||
| * is being constructed. Used to verify that the module logger is in place before the | ||
| * application builds itself, so no plain `yii\log\Logger` (whose `init()` registers a | ||
| * shutdown function that keeps it alive after each test) is created during boot. | ||
| */ | ||
| final class App extends BaseApplication | ||
| { | ||
| public ?string $loggerAtCtorStart = null; | ||
|
|
||
| public function __construct(array $config = []) | ||
| { | ||
| $this->loggerAtCtorStart = $this->loggerClass(); | ||
| parent::__construct($config); | ||
| } | ||
|
|
||
| private function loggerClass(): ?string | ||
| { | ||
| static $property = null; | ||
| $property ??= new ReflectionProperty(\yii\BaseYii::class, '_logger'); | ||
| $logger = $property->getValue(); | ||
| return $logger === null ? null : $logger::class; | ||
| } | ||
| } |
This file contains hidden or 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,13 @@ | ||
| paths: | ||
| tests: . | ||
| output: ../../_output | ||
| data: _data | ||
| support: ../../_support | ||
| namespace: tests\ | ||
| extensions: | ||
| enabled: | ||
| - Codeception\Extension\RunFailed | ||
| modules: | ||
| config: | ||
| Yii2: | ||
| configFile: config.php |
This file contains hidden or 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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/app/App.php'; | ||
|
|
||
| return [ | ||
| 'class' => app\leak\App::class, | ||
| 'id' => 'leak', | ||
| 'basePath' => __DIR__, | ||
| ]; |
This file contains hidden or 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,8 @@ | ||
| # Codeception Test Suite Configuration | ||
| # | ||
| # Suite for functional tests | ||
| actor: FunctionalTester | ||
| modules: | ||
| enabled: | ||
| - Yii2 | ||
| - Asserts |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.