Skip to content

Commit

Permalink
Improved MemoryLeakTest
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 9, 2024
1 parent 1e6e064 commit 39e3411
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions tests/memory/MemoryLeakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,30 @@ public static function setUpBeforeClass(): void
}

self::$phpParser = (new ParserFactory())->createForHostVersion();
self::cleanUpParser();
}

private static function cleanUpParser(): void
{
self::$phpParser->parse('');
}

public function testTyphoonReflectorClassExistsIsNotLeaking(): void
private static function assertMemoryIsConstant(\Closure $action): void
{
foreach (self::CLASSES as $class) {
$memory = memory_get_usage();
$memory = memory_get_usage();

$action();
self::cleanUpParser();

TyphoonReflector::build(phpParser: self::$phpParser)->classExists($class);
self::cleanUpParser();
self::assertLessThanOrEqual(500, memory_get_usage() - $memory);
}

self::assertLessThanOrEqual($memory, memory_get_usage());
public function testTyphoonReflectorClassExistsIsNotLeaking(): void
{
foreach (self::CLASSES as $class) {
self::assertMemoryIsConstant(static function () use ($class): void {
TyphoonReflector::build(phpParser: self::$phpParser)->classExists($class);
});
}
}

Expand All @@ -70,48 +78,39 @@ public function testTyphoonReflectorReflectIsNotLeaking(): void
self::cleanUpParser();

foreach (self::CLASSES as $class) {
$memory = memory_get_usage();

TyphoonReflector::build(phpParser: self::$phpParser)->reflectClass($class);
self::cleanUpParser();

self::assertLessThanOrEqual($memory, memory_get_usage());
self::assertMemoryIsConstant(static function () use ($class): void {
TyphoonReflector::build(phpParser: self::$phpParser)->reflectClass($class);
});
}
}

public function testReflectionSessionClassExistsIsNotLeaking(): void
{
$session = TyphoonReflector::build(phpParser: self::$phpParser)->startSession();
$session = TyphoonReflector::build(phpParser: self::$phpParser)->startSession();
$session->classExists(Type\NamedObjectType::class);
$session->flush();
self::cleanUpParser();

foreach (self::CLASSES as $class) {
$memory = memory_get_usage();

$session->classExists($class);
$session->flush();
self::cleanUpParser();

self::assertLessThanOrEqual($memory, memory_get_usage());
self::assertMemoryIsConstant(static function () use ($session, $class): void {
$session->classExists($class);
$session->flush();
});
}
}

public function testReflectionSessionReflectIsNotLeaking(): void
{
$session = TyphoonReflector::build(phpParser: self::$phpParser)->startSession();
$session = TyphoonReflector::build(phpParser: self::$phpParser)->startSession();
$session->reflectClass(Type\NamedObjectType::class);
$session->flush();
self::cleanUpParser();

foreach (self::CLASSES as $class) {
$memory = memory_get_usage();

$session->reflectClass($class);
$session->flush();
self::cleanUpParser();

self::assertLessThanOrEqual($memory, memory_get_usage());
self::assertMemoryIsConstant(static function () use ($session, $class): void {
$session->reflectClass($class);
$session->flush();
});
}
}
}

0 comments on commit 39e3411

Please sign in to comment.