Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- Bug #191: Refactor test imports for consistency and add support files (@terabytesoftw)
- Bug #192: Add copyright and license information to `MockerExtension::class` and refactor `tests/support/bootstrap.php` (@terabytesoftw)
- Bug #193: Update `.editorconfig` and `.gitignore` for improved consistency and clarity (@terabytesoftw)
- Bug #194: Update symplify/easy-coding-standard requirement from `^12.5` to `^13.0` (@dependabot)
- Dep #194: Update symplify/easy-coding-standard requirement from `^12.5` to `^13.0` (@dependabot)
- Bug #195: Add test for memory usage at `90%` threshold in `ApplicationMemoryTest` class (@terabytesoftw)

## 0.1.1 October 6, 2025

Expand Down
36 changes: 21 additions & 15 deletions tests/http/stateless/ApplicationMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ final class ApplicationMemoryTest extends TestCase
* @throws InvalidConfigException if the configuration is invalid or incomplete.
*/
#[DataProviderExternal(StatelessApplicationProvider::class, 'memoryThreshold')]
public function testCleanBehaviorWithDifferentMemoryLimits(
string $memoryLimit,
bool $shouldTriggerSpecialTest,
string $assertionMessage,
): void {
public function testCleanBehaviorWithDifferentMemoryLimits(string $memoryLimit, string $assertionMessage): void
{
$originalLimit = ini_get('memory_limit');

ini_set('memory_limit', $memoryLimit);
Expand All @@ -58,21 +55,30 @@ public function testCleanBehaviorWithDifferentMemoryLimits(

$app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals());

if ($shouldTriggerSpecialTest === false) {
self::assertFalse($app->clean(), $assertionMessage);
}
self::assertFalse($app->clean(), $assertionMessage);

if ($shouldTriggerSpecialTest === true) {
$currentUsage = memory_get_usage(true);
ini_set('memory_limit', $originalLimit);
}

$artificialLimit = (int) ($currentUsage / 0.9);
/**
* @throws InvalidConfigException if the configuration is invalid or incomplete.
*/
public function testCleanReturnsTrueWhenMemoryUsageIsExactlyPercentThreshold(): void
{
$app = $this->statelessApplication();

$app->setMemoryLimit($artificialLimit);
$app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals());

self::assertTrue($app->clean(), $assertionMessage);
}
$currentUsage = memory_get_usage(true);

ini_set('memory_limit', $originalLimit);
$exactLimit = (int) ($currentUsage / 0.9);

$app->setMemoryLimit($exactLimit);

self::assertTrue(
$app->clean(),
"Should return 'true' when memory usage is exactly at '90%' threshold (boundary condition).",
);
}

/**
Expand Down
22 changes: 6 additions & 16 deletions tests/provider/StatelessApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,36 +443,26 @@ public static function memoryLimitPositive(): array
* This provider supplies test cases for validating the logic that determines whether memory cleanup should be
* triggered based on current memory usage and configured memory limits.
*
* Each test case includes the memory limit string, the expected boolean result indicating whether cleanup should
* occur, and an assertion message describing the expected outcome.
* Each test case includes the memory limit string and an assertion message describing the expected outcome.
*
* @return array test data with memory limit, expected cleanup result, and assertion message.
* @return array test data with memory limit and assertion message.
*
* @phpstan-return array<string, array{string, bool, string}>
* @phpstan-return array<string, array{string, string}>
*/
public static function memoryThreshold(): array
{
return [
'low usage - should not clean' => [
'1G',
false,
"'clean()' should return 'false' when memory usage is below '90%' threshold with '1G' limit.",
"Should return 'false' when memory usage is below '90%' threshold with '1G' limit.",
],
'moderate usage - should not clean' => [
'512M',
false,
"'clean()' should return 'false' when memory usage is below '90%' threshold with '512M' limit.",
"Should return 'false' when memory usage is below '90%' threshold with '512M' limit.",
],
'threshold calculation - 100M' => [
'100M',
false,
"'clean()' should return 'false' with '100M' limit and verify correct '90%' threshold calculation.",
],
'high memory setup - 2G' => [
'2G',
true,
"'clean()' should return 'true' when memory usage equals the calculated '90%' threshold " .
'(using adjusted limit).',
"Should return 'false' with '100M' limit and verify correct '90%' threshold calculation.",
],
];
}
Expand Down
Loading