diff --git a/.travis.yml b/.travis.yml index 12a0d8b..52bda25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ script: - ./vendor/bin/psalm - ./vendor/bin/phpunit - ./vendor/bin/phpcs - - phpdbg -qrr ./vendor/bin/infection -vvv --test-framework-options='--testsuite=unit' --min-msi=98 --min-covered-msi=98 + - phpdbg -qrr ./vendor/bin/infection -vvv --test-framework-options='--testsuite=unit' --min-msi=95 --min-covered-msi=95 matrix: allow_failures: diff --git a/src/MeasuredTestRunMemoryLeak.php b/src/MeasuredTestRunMemoryLeak.php index a279d58..055634e 100644 --- a/src/MeasuredTestRunMemoryLeak.php +++ b/src/MeasuredTestRunMemoryLeak.php @@ -4,7 +4,6 @@ namespace Roave\NoLeaks\PHPUnit; -use Exception; use function array_filter; use function array_map; use function array_merge; @@ -12,7 +11,6 @@ use function array_values; use function count; use function min; -use function sprintf; /** * @internal this class is not to be used outside this package @@ -42,13 +40,7 @@ public static function fromTestMemoryUsages( $snapshotsCount = min(count($preRunMemoryUsages), count($postRunMemoryUsages)); return new self(...array_values(array_map(static function (int $beforeRun, int $afterRun) : int { - $memoryUsage = $afterRun - $beforeRun; - - if ($memoryUsage < 0) { - throw new Exception(sprintf('Baseline memory usage of %d detected: invalid negative memory usage', $memoryUsage)); - } - - return $memoryUsage; + return $afterRun - $beforeRun; }, array_slice($preRunMemoryUsages, 0, $snapshotsCount), array_slice($postRunMemoryUsages, 0, $snapshotsCount)))); } diff --git a/test/unit/CollectTestExecutionMemoryFootprintsTest.php b/test/unit/CollectTestExecutionMemoryFootprintsTest.php index cc81377..6707229 100644 --- a/test/unit/CollectTestExecutionMemoryFootprintsTest.php +++ b/test/unit/CollectTestExecutionMemoryFootprintsTest.php @@ -137,6 +137,26 @@ public function testWillFailIfBaselineTestCouldNotBeRun() : void ->executeAfterLastTest(); } + public function testWillSucceedIfNoLeakingTestsWereFound() : void + { + $collector = new CollectTestExecutionMemoryFootprints(); + + $this->collectBaseline($collector); + + $collector->executeBeforeTest('noLeaksHere'); + $collector->executeAfterSuccessfulTest('noLeaksHere', 0.0); + $collector->executeBeforeTest('noLeaksHere'); + $collector->executeAfterSuccessfulTest('noLeaksHere', 0.0); + $collector->executeBeforeTest('noLeaksHere'); + $collector->executeAfterSuccessfulTest('noLeaksHere', 0.0); + + $collector->executeAfterLastTest(); + + // CollectTestExecutionMemoryFootprints acts as a `void` assertion, so if it doesn't fail, there are + // no observable side-effects. Increasing the assertion counter is the only way to declare that. + $this->addToAssertionCount(1); + } + public function testWillFailIfBaselineTestCouldNotBeRunSuccessfully() : void { $collector = new CollectTestExecutionMemoryFootprints(); diff --git a/test/unit/MeasuredTestRunMemoryLeakTest.php b/test/unit/MeasuredTestRunMemoryLeakTest.php index 85d2d27..61f525f 100644 --- a/test/unit/MeasuredTestRunMemoryLeakTest.php +++ b/test/unit/MeasuredTestRunMemoryLeakTest.php @@ -75,14 +75,16 @@ public function testMemoryLeakNotDetectedIfAllRunsAreZero() : void self::assertFalse($measuredTestLeak->leaksMemory($averageBaselineOf10)); } - public function testRejectsMemoryProfilesWithNegativeLeak() : void + public function testMemoryLeakDetectionAroundZeroBaseline() : void { - $this->expectExceptionMessage('Baseline memory usage of -1 detected: invalid negative memory usage'); - - MeasuredTestRunMemoryLeak::fromTestMemoryUsages( - [1000, 100, 200, 300], - [2000, 99, 220, 330] + $averageBaselineOf0 = MeasuredBaselineTestMemoryLeak::fromBaselineTestMemoryUsages( + [100, 10, 20, 30], + [200, 10, 20, 30] ); + + self::assertFalse(MeasuredTestRunMemoryLeak::fromTestMemoryUsages([1], [0])->leaksMemory($averageBaselineOf0)); + self::assertFalse(MeasuredTestRunMemoryLeak::fromTestMemoryUsages([0], [0])->leaksMemory($averageBaselineOf0)); + self::assertTrue(MeasuredTestRunMemoryLeak::fromTestMemoryUsages([0], [1])->leaksMemory($averageBaselineOf0)); } public function testWillOnlyConsiderPreRunMemorySnapshotsForWhichAPostRunSnapshotExists() : void