Skip to content

Commit

Permalink
Use defer in TyphoonReflectorMemoryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Nov 6, 2024
1 parent 4194e39 commit 9f18d3c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dragon-code/benchmark": "^2.6",
"ergebnis/composer-normalize": "^2.44.0",
"friendsofphp/php-cs-fixer": "^3.64.0",
"php-defer/php-defer": "^5.0",
"phpstan/phpstan": "^1.12.6",
"phpunit/phpunit": "^10.5.36",
"phpyh/coding-standard": "^2.6.2",
Expand Down
53 changes: 50 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions tests/TyphoonReflectorMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,20 @@ final class TyphoonReflectorMemoryTest extends TestCase
{
public function testItIsGarbageCollected(): void
{
$gcWasEnabled = gc_enabled();
gc_disable();
if (gc_enabled()) {
gc_disable();
defer($_, gc_enable(...));
}

try {
$reflector = TyphoonReflector::build();
$reflection = $reflector->reflectClass(\AppendIterator::class);
$weakReflector = \WeakReference::create($reflector);
$weakReflection = \WeakReference::create($reflection);
$reflector = TyphoonReflector::build();
$reflection = $reflector->reflectClass(\AppendIterator::class);
$weakReflector = \WeakReference::create($reflector);
$weakReflection = \WeakReference::create($reflection);

unset($reflection, $reflector);
unset($reflection, $reflector);

// assertTrue() is used instead of assertNull() to avoid huge reflector dump in the diff
self::assertTrue($weakReflector->get() === null, 'Reflector is not garbage collected.');
self::assertTrue($weakReflection->get() === null, 'ClassReflection is not garbage collected.');
} finally {
if ($gcWasEnabled) {
gc_enable();
}
}
// assertTrue() is used instead of assertNull() to avoid huge reflector dump in the diff
self::assertTrue($weakReflector->get() === null, 'Reflector is not garbage collected.');
self::assertTrue($weakReflection->get() === null, 'ClassReflection is not garbage collected.');
}
}

0 comments on commit 9f18d3c

Please sign in to comment.