From 28ed660c2d9ca6dcd3fff3d2371a98dfe7826f9e Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 7 Apr 2021 01:30:14 -0700 Subject: [PATCH] Use EXPECT_NEAR to improve test failure messages Summary: The current test failures don't include the values passed to `EXPECT` which makes it difficult to understand if the test ended earlier or later then expected. ``` Failure: Value of: (commitDuration >= 1000 - threshold) && (commitDuration <= 1000 + threshold) Actual: false Expected: true ``` This diff uses the gtest `EXPECT_NEAR` to get exception messages including the delta: ``` Failure: The difference between telemetryDurationToMilliseconds(telemetry.getTextMeasureTime()) and 600 is 153, which exceeds threshold, where telemetryDurationToMilliseconds(telemetry.getTextMeasureTime()) evaluates to 753, 600 evaluates to 600, and threshold evaluates to 70. ``` This doesn't change the test's flakiness because of how sleep is implemented. Changelog: [Internal] Test only change Reviewed By: sammy-SC Differential Revision: D27595206 fbshipit-source-id: f31bdd92ecc7271c9491dda18639ea08820f5730 --- .../telemetry/tests/TransactionTelemetryTest.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/ReactCommon/react/renderer/telemetry/tests/TransactionTelemetryTest.cpp b/ReactCommon/react/renderer/telemetry/tests/TransactionTelemetryTest.cpp index 484990f5eec..cacb2af4abc 100644 --- a/ReactCommon/react/renderer/telemetry/tests/TransactionTelemetryTest.cpp +++ b/ReactCommon/react/renderer/telemetry/tests/TransactionTelemetryTest.cpp @@ -15,9 +15,6 @@ using namespace facebook::react; -#define EXPECT_EQ_WITH_THRESHOLD(a, b, threshold) \ - EXPECT_TRUE((a >= b - threshold) && (a <= b + threshold)) - template void sleep(double durationInSeconds) { auto timepoint = ClockT::now() + @@ -35,7 +32,7 @@ TEST(TransactionTelemetryTest, timepoints) { auto duration = telemetryDurationToMilliseconds(timepointB - timepointA); - EXPECT_EQ_WITH_THRESHOLD(duration, 100, threshold); + EXPECT_NEAR(duration, 100, threshold); } TEST(TransactionTelemetryTest, normalUseCase) { @@ -82,12 +79,12 @@ TEST(TransactionTelemetryTest, normalUseCase) { auto mountDuration = telemetryDurationToMilliseconds( telemetry.getMountEndTime() - telemetry.getMountStartTime()); - EXPECT_EQ_WITH_THRESHOLD(commitDuration, 1000, threshold); - EXPECT_EQ_WITH_THRESHOLD(layoutDuration, 800, threshold); - EXPECT_EQ_WITH_THRESHOLD(mountDuration, 100, threshold); + EXPECT_NEAR(commitDuration, 1000, threshold); + EXPECT_NEAR(layoutDuration, 800, threshold); + EXPECT_NEAR(mountDuration, 100, threshold); EXPECT_EQ(telemetry.getNumberOfTextMeasurements(), 3); - EXPECT_EQ_WITH_THRESHOLD( + EXPECT_NEAR( telemetryDurationToMilliseconds(telemetry.getTextMeasureTime()), 600, threshold);