From 5dc6eded1b160fa6e923b6bb11633acff484ea74 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 19 Apr 2020 22:50:34 -0700 Subject: [PATCH] Fabric: Using proper clock in MountingTelemetryTest Summary: Apparently, `std::this_thread::sleep_for` uses a different clock to measure time which causes ofter misalignment with the clock which Telemery uses which makes the test flaky. Using the same clock should fix it. Changelog: [Internal] Fabric-specific internal change. Reviewed By: JoshuaGross Differential Revision: D21116058 fbshipit-source-id: 52dde2e325776d365431a2a957dcc12dfe53f890 --- .../mounting/tests/MountingTelemetryTest.cpp | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp b/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp index afeaf6f2f31..8d1426675b8 100644 --- a/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp +++ b/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp @@ -18,11 +18,19 @@ 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() + + std::chrono::milliseconds((long long)(durationInSeconds * 1000)); + while (ClockT::now() < timepoint) { + } +} + TEST(MountingTelemetryTest, timepoints) { - auto threshold = int64_t{100}; + auto threshold = int64_t{10}; auto timepointA = telemetryTimePointNow(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sleep(0.1); auto timepointB = telemetryTimePointNow(); auto duration = telemetryDurationToMilliseconds(timepointB - timepointA); @@ -31,21 +39,21 @@ TEST(MountingTelemetryTest, timepoints) { } TEST(MountingTelemetryTest, normalUseCase) { - auto threshold = int64_t{100}; + auto threshold = int64_t{10}; auto telemetry = MountingTelemetry{}; telemetry.willCommit(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sleep(0.1); telemetry.willLayout(); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + sleep(0.2); telemetry.didLayout(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sleep(0.1); telemetry.didCommit(); - std::this_thread::sleep_for(std::chrono::milliseconds(300)); + sleep(0.3); telemetry.willMount(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sleep(0.1); telemetry.didMount(); auto commitDuration = telemetryDurationToMilliseconds( @@ -55,7 +63,7 @@ TEST(MountingTelemetryTest, normalUseCase) { auto mountDuration = telemetryDurationToMilliseconds( telemetry.getMountEndTime() - telemetry.getMountStartTime()); - EXPECT_EQ_WITH_THRESHOLD(commitDuration, 400, threshold * 2); + EXPECT_EQ_WITH_THRESHOLD(commitDuration, 400, threshold); EXPECT_EQ_WITH_THRESHOLD(layoutDuration, 200, threshold); EXPECT_EQ_WITH_THRESHOLD(mountDuration, 100, threshold); }