From c659b9a590a7f81802f8075dc26d19a188d5765a Mon Sep 17 00:00:00 2001 From: toneyzeng Date: Fri, 9 Jun 2023 05:12:12 +0800 Subject: [PATCH] Fix: invalid time-point comparison between each from different clock source (flutter/engine#42409) Sometimes the time point created late will less than the time point create earlier since the CLOCK SOURCE has changed. We must make sure the CLOCK SOURCE (fml::TimePoint) is always keep the same while we are testing. Corrects the test case is needed before we fix the problem completely. --- .../flutter/shell/common/shell_unittests.cc | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 2edc3b173d..7f9473a2b1 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -5,9 +5,11 @@ #define FML_USED_ON_EMBEDDER #include +#include #include #include #include +#include #include #include @@ -676,10 +678,14 @@ static void CheckFrameTimings(const std::vector& timings, } TEST_F(ShellTest, ReportTimingsIsCalled) { - fml::TimePoint start = fml::TimePoint::Now(); auto settings = CreateSettingsForFixture(); std::unique_ptr shell = CreateShell(settings); + // We MUST put |start| after |CreateShell| because the clock source will be + // reset through |TimePoint::SetClockSource()| in + // |DartVMInitializer::Initialize()| within |CreateShell()|. + fml::TimePoint start = fml::TimePoint::Now(); + // Create the surface needed by rasterizer PlatformViewNotifyCreated(shell.get()); @@ -726,19 +732,10 @@ TEST_F(ShellTest, ReportTimingsIsCalled) { } TEST_F(ShellTest, FrameRasterizedCallbackIsCalled) { - fml::TimePoint start = fml::TimePoint::Now(); - auto settings = CreateSettingsForFixture(); - fml::AutoResetWaitableEvent timingLatch; + FrameTiming timing; - - for (auto phase : FrameTiming::kPhases) { - timing.Set(phase, fml::TimePoint()); - // Check that the time points are initially smaller than start, so - // CheckFrameTimings will fail if they're not properly set later. - ASSERT_TRUE(timing.Get(phase) < start); - } - + fml::AutoResetWaitableEvent timingLatch; settings.frame_rasterized_callback = [&timing, &timingLatch](const FrameTiming& t) { timing = t; @@ -747,6 +744,22 @@ TEST_F(ShellTest, FrameRasterizedCallbackIsCalled) { std::unique_ptr shell = CreateShell(settings); + // Wait to make |start| bigger than zero + using namespace std::chrono_literals; + std::this_thread::sleep_for(1ms); + + // We MUST put |start| after |CreateShell()| because the clock source will be + // reset through |TimePoint::SetClockSource()| in + // |DartVMInitializer::Initialize()| within |CreateShell()|. + fml::TimePoint start = fml::TimePoint::Now(); + + for (auto phase : FrameTiming::kPhases) { + timing.Set(phase, fml::TimePoint()); + // Check that the time points are initially smaller than start, so + // CheckFrameTimings will fail if they're not properly set later. + ASSERT_TRUE(timing.Get(phase) < start); + } + // Create the surface needed by rasterizer PlatformViewNotifyCreated(shell.get());