Skip to content

Commit

Permalink
Merge pull request #13 from Roave/fix/skip-negative-memory-measuremen…
Browse files Browse the repository at this point in the history
…ts-in-tests

Skipping negative memory measurements for executed tests
  • Loading branch information
Ocramius authored Mar 22, 2019
2 parents f154544 + 0ddf9f5 commit 4f26a4d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 1 addition & 9 deletions src/MeasuredTestRunMemoryLeak.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Roave\NoLeaks\PHPUnit;

use Exception;
use function array_filter;
use function array_map;
use function array_merge;
use function array_slice;
use function array_values;
use function count;
use function min;
use function sprintf;

/**
* @internal this class is not to be used outside this package
Expand Down Expand Up @@ -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))));
}

Expand Down
20 changes: 20 additions & 0 deletions test/unit/CollectTestExecutionMemoryFootprintsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 8 additions & 6 deletions test/unit/MeasuredTestRunMemoryLeakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4f26a4d

Please sign in to comment.