From b3a8706c2f809bc2d09b182c262de3b9509584fa Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 27 Jan 2020 13:23:09 -0800 Subject: [PATCH] Fabric: Basic tests for MountingTelemetry Summary: Some tests which will be useful for the next diff. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D19482255 fbshipit-source-id: 3384662477f750620a37acc4a277a55c2dbd8d0e --- .../mounting/tests/MountingTelemetryTest.cpp | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp diff --git a/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp b/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp new file mode 100644 index 00000000000..391e1d088e8 --- /dev/null +++ b/ReactCommon/fabric/mounting/tests/MountingTelemetryTest.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include + +#include + +#include + +using namespace facebook::react; + +#define EXPECT_EQ_WITH_THRESHOLD(a, b, threshold) \ + EXPECT_TRUE((a >= b - threshold) && (a <= b + threshold)) + +TEST(MountingTelemetryTest, normalUseCase) { + auto threshold = int64_t{30}; + auto telemetry = MountingTelemetry{}; + + telemetry.willCommit(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + telemetry.willLayout(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + telemetry.didLayout(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + telemetry.didCommit(); + + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + + telemetry.willMount(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + telemetry.didMount(); + + auto commitDuration = + telemetry.getCommitEndTime() - telemetry.getCommitStartTime(); + auto layoutDuration = + telemetry.getLayoutEndTime() - telemetry.getLayoutStartTime(); + auto mountDuration = + telemetry.getMountEndTime() - telemetry.getMountStartTime(); + + EXPECT_EQ_WITH_THRESHOLD(commitDuration, 400, threshold); + EXPECT_EQ_WITH_THRESHOLD(layoutDuration, 200, threshold); + EXPECT_EQ_WITH_THRESHOLD(mountDuration, 100, threshold); +} + +TEST(MountingTelemetryTest, abnormalUseCases) { + // Calling `did` before `will` should crash. + EXPECT_DEATH_IF_SUPPORTED( + { + auto telemetry = MountingTelemetry{}; + telemetry.didDiff(); + }, + "diffStartTime_"); + + EXPECT_DEATH_IF_SUPPORTED( + { + auto telemetry = MountingTelemetry{}; + telemetry.didCommit(); + }, + "commitStartTime_"); + + EXPECT_DEATH_IF_SUPPORTED( + { + auto telemetry = MountingTelemetry{}; + telemetry.didMount(); + }, + "mountStartTime_"); + + // Getting `start` *or* `end` timepoints before a pair of `will` and `did` + // should crash. + EXPECT_DEATH_IF_SUPPORTED( + { + auto telemetry = MountingTelemetry{}; + telemetry.willCommit(); + telemetry.getCommitStartTime(); + }, + "commitEndTime_"); + + EXPECT_DEATH_IF_SUPPORTED( + { + auto telemetry = MountingTelemetry{}; + telemetry.willCommit(); + telemetry.getCommitEndTime(); + }, + "commitEndTime_"); +}