Skip to content

Commit

Permalink
Merge pull request #55 from ingenerator/add_single_timer_assertion
Browse files Browse the repository at this point in the history
Add assertCapturedOneExactTimer helper.
  • Loading branch information
craig410 authored Apr 27, 2023
2 parents 8f835c3 + 590d507 commit 3181bec
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### Unreleased

### v1.19.2 (2023-04-27)

* Add assertCapturedOneExactTimer helper. Allowing you to simultaneously assert only
one timer was recorded and the timer duration in a single method.

### v1.19.1 (2023-03-22)

* Fix a bug in UniqueMap where it throws on attempt to access an offset with a null value
Expand Down
20 changes: 17 additions & 3 deletions src/Monitoring/AssertMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function assertCapturedOneTimer(
);
}

private static function assertOnlyOneMetricTypeAndPayload(array $metrics, string $type, $expected_payload)
private static function assertOnlyOneMetricTypeAndPayload(array $metrics, string $type, $expected_payload): void
{
Assert::assertCount(1, $metrics, 'Expected only 1 metric recorded');
Assert::assertSame($type, $metrics[0]['type'], 'Expected metric to be of type '.$type);
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function assertNoMetricsCaptured(array $metrics): void
Assert::assertEmpty($metrics, "Expected no metrics to be captured");
}

public static function assertNoMetricsFor(array $metrics, string $metric_name)
public static function assertNoMetricsFor(array $metrics, string $metric_name): void
{
Assert::assertNotContains(
$metric_name,
Expand All @@ -83,12 +83,26 @@ public static function assertNoMetricsFor(array $metrics, string $metric_name)
);
}

public static function assertCapturedOneExactTimer(
array $metrics,
float $expect_millis,
string $name,
?string $source = NULL
): void {
AssertMetrics::assertCapturedOneTimer($metrics, $name, $source);
AssertMetrics::assertTimerValues(
$metrics,
MetricId::nameAndSource($name, $source),
[$expect_millis]
);
}

public static function assertTimerValues(
array $metrics,
MetricId $metric,
array $expected_times,
float $tolerance_ms = 0
) {
): void {
$filtered_metrics = array_values(
array_filter($metrics,
fn($m) => ($m['type'] === 'timer' and
Expand Down
49 changes: 49 additions & 0 deletions test/unit/Monitoring/AssertMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ public function test_assertTimerValues(
);
}

/**
* @testWith [[], "no metrics"]
* [[{"name": "foo", "source": "wrong"}], "wrong source"]
* [[{"name": "wrong", "source": "bar"}], "wrong name"]
* [[{"name": "foo", "source": "bar"}, {"name": "foo", "source": "bar"}], "duplicate metric"]
* [[{"name": "foo", "source": "bar"}, {"name": "foo", "source": "other"}], "extra metric"]
**/
public function test_assertCapturedOneExactTimer_fails_if_no_match(array $metrics)
{
$agent = new ArrayMetricsAgent();
foreach ($metrics as $metric) {
$agent->addTimer(
MetricId::nameAndSource($metric['name'], $metric['source']),
new DateTimeImmutable(),
new DateTimeImmutable()
);
}
try {
AssertMetrics::assertCapturedOneExactTimer($agent->getMetrics(), 0,'foo', 'bar');
$this->fail('Expected assertion failure');
} catch (ExpectationFailedException $e) {
// we're expecting this - increment the assertion count
$this->assertTrue(true);
}
}

/**
* @testWith ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123456", 0, true]
* ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123456", 0.001, false]
* ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123457", 0.001, true]
* ["2020-02-02 02:02:02.123456", "2020-02-02 02:02:02.123455", 0.001, false]
*/
public function test_assertCapturedOneExactTimer_asserts_correct_time(
string $start,
string $end,
float $expected_time,
bool $expect_success
) {
$agent = new ArrayMetricsAgent();
$metric = MetricId::nameAndSource('timer', 'test');
$agent->addTimer($metric, new DateTimeImmutable($start), new DateTimeImmutable($end));
$this->assertAssertionResult(
$expect_success,
fn() => AssertMetrics::assertCapturedOneExactTimer(
$agent->getMetrics(),
$expected_time, 'timer', 'test')
);
}

/**
* @testWith ["something", "test", true]
* ["something_else", "test", false]
Expand Down

0 comments on commit 3181bec

Please sign in to comment.