Files
react-native/ReactCommon/react/renderer/telemetry/tests/TransactionTelemetryTest.cpp
T
Micha Reiser 28ed660c2d 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
2021-04-07 01:31:47 -07:00

135 lines
3.6 KiB
C++

/*
* 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 <chrono>
#include <thread>
#include <gtest/gtest.h>
#include <react/renderer/telemetry/TransactionTelemetry.h>
#include <react/utils/Telemetry.h>
using namespace facebook::react;
template <typename ClockT>
void sleep(double durationInSeconds) {
auto timepoint = ClockT::now() +
std::chrono::milliseconds((long long)(durationInSeconds * 1000));
while (ClockT::now() < timepoint) {
}
}
TEST(TransactionTelemetryTest, timepoints) {
auto threshold = int64_t{70};
auto timepointA = telemetryTimePointNow();
sleep<TelemetryClock>(0.1);
auto timepointB = telemetryTimePointNow();
auto duration = telemetryDurationToMilliseconds(timepointB - timepointA);
EXPECT_NEAR(duration, 100, threshold);
}
TEST(TransactionTelemetryTest, normalUseCase) {
auto threshold = int64_t{70};
auto telemetry = TransactionTelemetry{};
telemetry.setAsThreadLocal();
telemetry.willCommit();
sleep<TelemetryClock>(0.1);
telemetry.willLayout();
sleep<TelemetryClock>(0.2);
TransactionTelemetry::threadLocalTelemetry()->willMeasureText();
sleep<TelemetryClock>(0.1);
TransactionTelemetry::threadLocalTelemetry()->didMeasureText();
TransactionTelemetry::threadLocalTelemetry()->willMeasureText();
sleep<TelemetryClock>(0.2);
TransactionTelemetry::threadLocalTelemetry()->didMeasureText();
TransactionTelemetry::threadLocalTelemetry()->willMeasureText();
sleep<TelemetryClock>(0.3);
TransactionTelemetry::threadLocalTelemetry()->didMeasureText();
telemetry.didLayout();
sleep<TelemetryClock>(0.1);
telemetry.didCommit();
telemetry.setRevisionNumber(42);
telemetry.unsetAsThreadLocal();
sleep<TelemetryClock>(0.3);
telemetry.willMount();
sleep<TelemetryClock>(0.1);
telemetry.didMount();
auto commitDuration = telemetryDurationToMilliseconds(
telemetry.getCommitEndTime() - telemetry.getCommitStartTime());
auto layoutDuration = telemetryDurationToMilliseconds(
telemetry.getLayoutEndTime() - telemetry.getLayoutStartTime());
auto mountDuration = telemetryDurationToMilliseconds(
telemetry.getMountEndTime() - telemetry.getMountStartTime());
EXPECT_NEAR(commitDuration, 1000, threshold);
EXPECT_NEAR(layoutDuration, 800, threshold);
EXPECT_NEAR(mountDuration, 100, threshold);
EXPECT_EQ(telemetry.getNumberOfTextMeasurements(), 3);
EXPECT_NEAR(
telemetryDurationToMilliseconds(telemetry.getTextMeasureTime()),
600,
threshold);
EXPECT_EQ(telemetry.getRevisionNumber(), 42);
}
TEST(TransactionTelemetryTest, abnormalUseCases) {
// Calling `did` before `will` should crash.
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.didDiff();
},
"diffStartTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.didCommit();
},
"commitStartTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.didMount();
},
"mountStartTime_");
// Getting `start` *or* `end` timepoints before a pair of `will` and `did`
// should crash.
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.willCommit();
telemetry.getCommitStartTime();
},
"commitEndTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.willCommit();
telemetry.getCommitEndTime();
},
"commitEndTime_");
}