Skip to content

Commit

Permalink
Enhancement: Compose maximum duration into SlowTest
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 25, 2021
1 parent 6d7c288 commit 68148e7
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 96 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
* Renamed `Collector` to `Collector\DefaultCollector` and extracted `Collector\Collector` interface ([#24]), by [@localheinz]
* Used `TimeKeeper` instead of `SlowTestCollector` in `Subscriber\TestPreparedSubscriber` ([#25]), by [@localheinz]
* Used `TimeKeeper` and `Collector\Collector` instead of `SlowTestCollector` in `Subscriber\TestPassedSubscriber` ([#26]), by [@localheinz]
* Composed maximum duration into `SlowTest` ([#37]), by [@localheinz]

### Removed

Expand All @@ -51,5 +52,6 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
[#26]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/26
[#34]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/34
[#36]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/36
[#37]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/37

[@localheinz]: https://github.com/localheinz
19 changes: 15 additions & 4 deletions src/SlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ final class SlowTest

private Event\Telemetry\Duration $duration;

private Event\Telemetry\Duration $maximumDuration;

private function __construct(
Event\Code\Test $test,
Event\Telemetry\Duration $duration
Event\Telemetry\Duration $duration,
Event\Telemetry\Duration $maximumDuration
) {
$this->test = $test;
$this->duration = $duration;
$this->maximumDuration = $maximumDuration;
}

public static function fromTestAndDuration(
public static function fromTestDurationAndMaximumDuration(
Event\Code\Test $test,
Event\Telemetry\Duration $duration
Event\Telemetry\Duration $duration,
Event\Telemetry\Duration $maximumDuration
): self {
return new self(
$test,
$duration
$duration,
$maximumDuration
);
}

Expand All @@ -48,4 +54,9 @@ public function duration(): Event\Telemetry\Duration
{
return $this->duration;
}

public function maximumDuration(): Event\Telemetry\Duration
{
return $this->maximumDuration;
}
}
5 changes: 3 additions & 2 deletions src/Subscriber/TestPassedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public function notify(Event\Test\Passed $event): void
return;
}

$slowTest = SlowTest::fromTestAndDuration(
$slowTest = SlowTest::fromTestDurationAndMaximumDuration(
$event->test(),
$duration
$duration,
$this->maximumDuration
);

$this->collector->collect($slowTest);
Expand Down
56 changes: 44 additions & 12 deletions test/Unit/Collector/DefaultCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,48 @@ public function testCollectCollectsSlowTests(): void
{
$faker = self::faker();

$first = SlowTest::fromTestAndDuration(
$first = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'foo',
'foo with data set #123',
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
);

$second = SlowTest::fromTestAndDuration(
$second = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'bar',
'bar',
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
);

$third = SlowTest::fromTestAndDuration(
$third = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'baz',
'baz with data set "string"',
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
Expand All @@ -97,7 +109,7 @@ public function testCollectDoesNotReplaceSlowTestWhenDurationIsLessThanPreviousS
{
$faker = self::faker();

$first = SlowTest::fromTestAndDuration(
$first = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'foo',
Expand All @@ -106,22 +118,30 @@ public function testCollectDoesNotReplaceSlowTestWhenDurationIsLessThanPreviousS
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(1),
$faker->numberBetween(0, 999_999_999)
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
);

$second = SlowTest::fromTestAndDuration(
$second = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'bar',
'bar',
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
);

$thirdForSameTest = SlowTest::fromTestAndDuration(
$thirdForSameTest = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
$first->test()->className(),
$first->test()->methodName(),
Expand All @@ -130,6 +150,10 @@ public function testCollectDoesNotReplaceSlowTestWhenDurationIsLessThanPreviousS
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(0, $first->duration()->seconds() - 1),
$faker->numberBetween(0, 999_999_999)
),
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
);

Expand All @@ -151,7 +175,12 @@ public function testCollectReplacesSlowTestWhenDurationIsGreaterThanPreviousSlow
{
$faker = self::faker();

$first = SlowTest::fromTestAndDuration(
$maximumDuration = Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
);

$first = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'foo',
Expand All @@ -160,10 +189,11 @@ public function testCollectReplacesSlowTestWhenDurationIsGreaterThanPreviousSlow
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
),
$maximumDuration
);

$second = SlowTest::fromTestAndDuration(
$second = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Example\SleeperTest::class,
'bar',
Expand All @@ -172,10 +202,11 @@ public function testCollectReplacesSlowTestWhenDurationIsGreaterThanPreviousSlow
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_999)
)
),
$maximumDuration
);

$thirdForSameTest = SlowTest::fromTestAndDuration(
$thirdForSameTest = SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
$first->test()->className(),
$first->test()->methodName(),
Expand All @@ -184,7 +215,8 @@ public function testCollectReplacesSlowTestWhenDurationIsGreaterThanPreviousSlow
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
$faker->numberBetween($first->duration()->seconds() + 1),
$faker->numberBetween(0, 999_999_999)
)
),
$maximumDuration
);

$collector = new DefaultCollector();
Expand Down
Loading

0 comments on commit 68148e7

Please sign in to comment.