From 6ce5731824284204a7cd0b481b39ccc96cf3ce92 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 14:18:02 -0400 Subject: [PATCH 1/5] test(StatelessApplicationTest): Add test for correct memory threshold calculation at '90%' limit. --- tests/http/StatelessApplicationTest.php | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index f18117f1..dc0cc1bd 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -219,6 +219,40 @@ public function testCaptchaSessionIsolation(): void ); } + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ + public function testCleanCalculatesCorrectMemoryThresholdWith90Percent(): void + { + $originalLimit = ini_get('memory_limit'); + + ini_set('memory_limit', '100M'); + + $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + + $app = $this->statelessApplication(); + + $app->handle($request); + $memoryLimit = $app->getMemoryLimit(); + + $expectedNinetyPercent = (int) (104_857_600 * 0.9); + + self::assertSame( + 104_857_600, + $memoryLimit, + "Memory limit should be exactly '104_857_600' bytes ('100M') for threshold calculation test in" . + "'StatelessApplication'.", + ); + self::assertSame( + 94_371_840, + $expectedNinetyPercent, + "'90%' of '100M' should be exactly '94_371_840' bytes, not a division result like '116_508_444' bytes " . + "('100M' / '0.9') in 'StatelessApplication'.", + ); + + ini_set('memory_limit', $originalLimit); + } + /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ From 5aea7eb83ac262fe8c5138775fcf23df5e12c04e Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 14:38:16 -0400 Subject: [PATCH 2/5] test(StatelessApplicationTest): Update memory threshold assertions for clarity and accuracy. --- tests/http/StatelessApplicationTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index dc0cc1bd..7bad392e 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -235,8 +235,11 @@ public function testCleanCalculatesCorrectMemoryThresholdWith90Percent(): void $app->handle($request); $memoryLimit = $app->getMemoryLimit(); - $expectedNinetyPercent = (int) (104_857_600 * 0.9); - + self::assertFalse( + $app->clean(), + "'clean()' should return 'false' when memory usage is below the '90%' threshold of the configured memory " . + "limit ('100M'), confirming that no cleanup is needed in 'StatelessApplication'.", + ); self::assertSame( 104_857_600, $memoryLimit, @@ -245,7 +248,7 @@ public function testCleanCalculatesCorrectMemoryThresholdWith90Percent(): void ); self::assertSame( 94_371_840, - $expectedNinetyPercent, + $memoryLimit * 0.9, "'90%' of '100M' should be exactly '94_371_840' bytes, not a division result like '116_508_444' bytes " . "('100M' / '0.9') in 'StatelessApplication'.", ); From f889ece7b2faa9bf2c111d53e2cb3ebb8b1cfea1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 14:44:31 -0400 Subject: [PATCH 3/5] fix(test): Ensure memory threshold calculation returns a float for accuracy. --- tests/http/StatelessApplicationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 7bad392e..6954eeab 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -247,7 +247,7 @@ public function testCleanCalculatesCorrectMemoryThresholdWith90Percent(): void "'StatelessApplication'.", ); self::assertSame( - 94_371_840, + 94_371_840.0, $memoryLimit * 0.9, "'90%' of '100M' should be exactly '94_371_840' bytes, not a division result like '116_508_444' bytes " . "('100M' / '0.9') in 'StatelessApplication'.", From 76dc281f30dbc2fb89b1be74a2272ce73a799fc1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 14:52:10 -0400 Subject: [PATCH 4/5] test(StatelessApplicationTest): Add test for memory usage at threshold limit. --- tests/http/StatelessApplicationTest.php | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/http/StatelessApplicationTest.php b/tests/http/StatelessApplicationTest.php index 6954eeab..841d4832 100644 --- a/tests/http/StatelessApplicationTest.php +++ b/tests/http/StatelessApplicationTest.php @@ -256,6 +256,43 @@ public function testCleanCalculatesCorrectMemoryThresholdWith90Percent(): void ini_set('memory_limit', $originalLimit); } + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ + public function testCleanReturnsTrueWhenMemoryUsageExactlyEqualsThreshold(): void + { + $originalLimit = ini_get('memory_limit'); + + ini_set('memory_limit', '2G'); + + $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + + $app = $this->statelessApplication(); + + $app->handle($request); + + $currentUsage = memory_get_usage(true); + + $artificialLimit = (int) ($currentUsage / 0.9); + + $app->setMemoryLimit($artificialLimit); + $memoryLimit = $app->getMemoryLimit(); + + self::assertTrue( + $app->clean(), + "'clean()' should return 'true' when memory usage is exactly at or above '90%' threshold ('>=' operator)" . + ", not only when strictly greater than ('>' operator) in 'StatelessApplication'.", + ); + self::assertSame( + $artificialLimit, + $memoryLimit, + "Memory limit should be set to the artificial limit '{$artificialLimit}' for threshold calculation test " . + "in 'StatelessApplication'.", + ); + + ini_set('memory_limit', $originalLimit); + } + /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ From 477c54f9e595f2af75c43753ee94823a8d9122c2 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 14:58:42 -0400 Subject: [PATCH 5/5] fix(StatelessApplication): Change memory threshold calculation to return a float for accuracy. --- src/http/StatelessApplication.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/StatelessApplication.php b/src/http/StatelessApplication.php index 86550262..b054ad46 100644 --- a/src/http/StatelessApplication.php +++ b/src/http/StatelessApplication.php @@ -133,7 +133,7 @@ public function clean(): bool $limit = $this->getMemoryLimit(); - $bound = (int) ($limit * 0.9); + $bound = $limit * 0.9; $usage = memory_get_usage(true);