Skip to content

Commit

Permalink
Create std::chrono timestamp provider and move fml_unittests to u…
Browse files Browse the repository at this point in the history
…se this (flutter#27708)
  • Loading branch information
iskakaushik authored and filmil committed Apr 21, 2022
1 parent 492e9ef commit ce2f307
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 44 deletions.
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Expand Up @@ -290,12 +290,15 @@ FILE: ../../../flutter/fml/thread_local.cc
FILE: ../../../flutter/fml/thread_local.h
FILE: ../../../flutter/fml/thread_local_unittests.cc
FILE: ../../../flutter/fml/thread_unittests.cc
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.cc
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.h
FILE: ../../../flutter/fml/time/time_delta.h
FILE: ../../../flutter/fml/time/time_delta_unittest.cc
FILE: ../../../flutter/fml/time/time_point.cc
FILE: ../../../flutter/fml/time/time_point.h
FILE: ../../../flutter/fml/time/time_point_unittest.cc
FILE: ../../../flutter/fml/time/time_unittest.cc
FILE: ../../../flutter/fml/time/timestamp_provider.h
FILE: ../../../flutter/fml/trace_event.cc
FILE: ../../../flutter/fml/trace_event.h
FILE: ../../../flutter/fml/unique_fd.cc
Expand Down
3 changes: 3 additions & 0 deletions fml/BUILD.gn
Expand Up @@ -80,6 +80,7 @@ source_set("fml") {
"time/time_delta.h",
"time/time_point.cc",
"time/time_point.h",
"time/timestamp_provider.h",
"trace_event.cc",
"trace_event.h",
"unique_fd.cc",
Expand Down Expand Up @@ -281,6 +282,8 @@ if (enable_unittests) {
"task_source_unittests.cc",
"thread_local_unittests.cc",
"thread_unittests.cc",
"time/chrono_timestamp_provider.cc",
"time/chrono_timestamp_provider.h",
"time/time_delta_unittest.cc",
"time/time_point_unittest.cc",
"time/time_unittest.cc",
Expand Down
25 changes: 13 additions & 12 deletions fml/message_loop_task_queues_merge_unmerge_unittests.cc
Expand Up @@ -9,6 +9,7 @@
#include "flutter/fml/message_loop_task_queues.h"
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/time/chrono_timestamp_provider.h"
#include "gtest/gtest.h"

namespace fml {
Expand All @@ -29,7 +30,7 @@ class TestWakeable : public fml::Wakeable {
static int CountRemainingTasks(fml::RefPtr<MessageLoopTaskQueues> task_queue,
const TaskQueueId& queue_id,
bool run_invocation = false) {
const auto now = fml::TimePoint::Now();
const auto now = ChronoTicksSinceEpoch();
int count = 0;
fml::closure invocation;
do {
Expand All @@ -53,12 +54,12 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
auto queue_id_2 = task_queue->CreateTaskQueue();

task_queue->RegisterTask(
queue_id_1, []() {}, fml::TimePoint::Now());
queue_id_1, []() {}, ChronoTicksSinceEpoch());
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_1));

task_queue->Merge(queue_id_1, queue_id_2);
task_queue->RegisterTask(
queue_id_1, []() {}, fml::TimePoint::Now());
queue_id_1, []() {}, ChronoTicksSinceEpoch());

ASSERT_EQ(2u, task_queue->GetNumPendingTasks(queue_id_1));
ASSERT_EQ(0u, task_queue->GetNumPendingTasks(queue_id_2));
Expand All @@ -72,7 +73,7 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
auto queue_id_2 = task_queue->CreateTaskQueue();

task_queue->RegisterTask(
queue_id_2, []() {}, fml::TimePoint::Now());
queue_id_2, []() {}, ChronoTicksSinceEpoch());
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_2));

task_queue->Merge(queue_id_1, queue_id_2);
Expand All @@ -87,9 +88,9 @@ TEST(MessageLoopTaskQueueMergeUnmerge, MergeUnmergeTasksPreserved) {
auto queue_id_2 = task_queue->CreateTaskQueue();

task_queue->RegisterTask(
queue_id_1, []() {}, fml::TimePoint::Now());
queue_id_1, []() {}, ChronoTicksSinceEpoch());
task_queue->RegisterTask(
queue_id_2, []() {}, fml::TimePoint::Now());
queue_id_2, []() {}, ChronoTicksSinceEpoch());

ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_1));
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_2));
Expand Down Expand Up @@ -145,7 +146,7 @@ TEST(MessageLoopTaskQueueMergeUnmerge, MergeInvokesBothWakeables) {
new TestWakeable([&](fml::TimePoint wake_time) { latch.CountDown(); }));

task_queue->RegisterTask(
queue_id_1, []() {}, fml::TimePoint::Now());
queue_id_1, []() {}, ChronoTicksSinceEpoch());

task_queue->Merge(queue_id_1, queue_id_2);

Expand All @@ -171,9 +172,9 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
new TestWakeable([&](fml::TimePoint wake_time) { latch_2.Signal(); }));

task_queue->RegisterTask(
queue_id_1, []() {}, fml::TimePoint::Now());
queue_id_1, []() {}, ChronoTicksSinceEpoch());
task_queue->RegisterTask(
queue_id_2, []() {}, fml::TimePoint::Now());
queue_id_2, []() {}, ChronoTicksSinceEpoch());

task_queue->Merge(queue_id_1, queue_id_2);
task_queue->Unmerge(queue_id_1);
Expand All @@ -197,7 +198,7 @@ TEST(MessageLoopTaskQueueMergeUnmerge, GetTasksToRunNowBlocksMerge) {
merge_end;

task_queue->RegisterTask(
queue_id_1, []() {}, fml::TimePoint::Now());
queue_id_1, []() {}, ChronoTicksSinceEpoch());
task_queue->SetWakeable(queue_id_1,
new TestWakeable([&](fml::TimePoint wake_time) {
wake_up_start.Signal();
Expand Down Expand Up @@ -246,10 +247,10 @@ TEST(MessageLoopTaskQueueMergeUnmerge,

task_queue->RegisterTask(
queue_id_2, [&]() { task_queue->Merge(queue_id_1, queue_id_2); },
fml::TimePoint::Now());
ChronoTicksSinceEpoch());

task_queue->RegisterTask(
queue_id_2, []() {}, fml::TimePoint::Now());
queue_id_2, []() {}, ChronoTicksSinceEpoch());

ASSERT_EQ(CountRemainingTasks(task_queue, queue_id_2, true), 1);
ASSERT_EQ(CountRemainingTasks(task_queue, queue_id_1, true), 1);
Expand Down
19 changes: 10 additions & 9 deletions fml/message_loop_task_queues_unittests.cc
Expand Up @@ -12,6 +12,7 @@

#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/time/chrono_timestamp_provider.h"
#include "gtest/gtest.h"

namespace fml {
Expand Down Expand Up @@ -55,7 +56,7 @@ TEST(MessageLoopTaskQueue, RegisterTwoTasksAndCount) {
auto task_queue = fml::MessageLoopTaskQueues::GetInstance();
auto queue_id = task_queue->CreateTaskQueue();
task_queue->RegisterTask(
queue_id, [] {}, fml::TimePoint::Now());
queue_id, [] {}, ChronoTicksSinceEpoch());
task_queue->RegisterTask(
queue_id, [] {}, fml::TimePoint::Max());
ASSERT_TRUE(task_queue->HasPendingTasks(queue_id));
Expand All @@ -69,13 +70,13 @@ TEST(MessageLoopTaskQueue, PreserveTaskOrdering) {

// order: 0
task_queue->RegisterTask(
queue_id, [&test_val]() { test_val = 1; }, fml::TimePoint::Now());
queue_id, [&test_val]() { test_val = 1; }, ChronoTicksSinceEpoch());

// order: 1
task_queue->RegisterTask(
queue_id, [&test_val]() { test_val = 2; }, fml::TimePoint::Now());
queue_id, [&test_val]() { test_val = 2; }, ChronoTicksSinceEpoch());

const auto now = fml::TimePoint::Now();
const auto now = ChronoTicksSinceEpoch();
int expected_value = 1;
for (;;) {
fml::closure invocation = task_queue->GetNextTaskToRun(queue_id, now);
Expand Down Expand Up @@ -124,7 +125,7 @@ TEST(MessageLoopTaskQueue, WakeUpIndependentOfTime) {
[&num_wakes](fml::TimePoint wake_time) { ++num_wakes; }));

task_queue->RegisterTask(
queue_id, []() {}, fml::TimePoint::Now());
queue_id, []() {}, ChronoTicksSinceEpoch());
task_queue->RegisterTask(
queue_id, []() {}, fml::TimePoint::Max());

Expand All @@ -147,7 +148,7 @@ TEST(MessageLoopTaskQueue, WokenUpWithNewerTime) {
task_queue->RegisterTask(
queue_id, []() {}, fml::TimePoint::Max());

const auto now = fml::TimePoint::Now();
const auto now = ChronoTicksSinceEpoch();
expected = now;
task_queue->RegisterTask(
queue_id, []() {}, now);
Expand Down Expand Up @@ -221,7 +222,7 @@ TEST(MessageLoopTaskQueue, ConcurrentQueueAndTaskCreatingCounts) {
task_queue_ids[std::rand() % kTaskQueuesCount];
const auto empty_task = []() {};
// The timepoint doesn't matter as the queue is never going to be drained.
const auto task_timepoint = fml::TimePoint::Now();
const auto task_timepoint = ChronoTicksSinceEpoch();

task_queues->RegisterTask(current_task_queue_id, empty_task,
task_timepoint);
Expand Down Expand Up @@ -271,8 +272,8 @@ TEST(MessageLoopTaskQueue, RegisterTaskWakesUpOwnerQueue) {
ASSERT_FALSE(true);
}));

auto time1 = fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(1);
auto time2 = fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(2);
auto time1 = ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(1);
auto time2 = ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(2);

ASSERT_EQ(0UL, wakes.size());

Expand Down
21 changes: 11 additions & 10 deletions fml/message_loop_unittests.cc
Expand Up @@ -14,6 +14,7 @@
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/time/chrono_timestamp_provider.h"
#include "gtest/gtest.h"

#define TIMESENSITIVE(x) TimeSensitiveTest_##x
Expand Down Expand Up @@ -116,7 +117,7 @@ TEST(MessageLoop, DelayedTasksAtSameTimeAreRunInOrder) {
auto& loop = fml::MessageLoop::GetCurrent();
size_t current = 0;
const auto now_plus_some =
fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(2);
fml::ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(2);
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTaskForTime(
PLATFORM_SPECIFIC_CAPTURE(&terminated, i, &current)() {
Expand Down Expand Up @@ -159,10 +160,10 @@ TEST(MessageLoop, TIMESENSITIVE(SingleDelayedTaskByDelta)) {
std::thread thread([&checked]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
auto begin = fml::TimePoint::Now();
auto begin = fml::ChronoTicksSinceEpoch();
loop.GetTaskRunner()->PostDelayedTask(
[begin, &checked]() {
auto delta = fml::TimePoint::Now() - begin;
auto delta = fml::ChronoTicksSinceEpoch() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, 3);
ASSERT_LE(ms, 7);
Expand All @@ -181,17 +182,17 @@ TEST(MessageLoop, TIMESENSITIVE(SingleDelayedTaskForTime)) {
std::thread thread([&checked]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
auto begin = fml::TimePoint::Now();
auto begin = fml::ChronoTicksSinceEpoch();
loop.GetTaskRunner()->PostTaskForTime(
[begin, &checked]() {
auto delta = fml::TimePoint::Now() - begin;
auto delta = fml::ChronoTicksSinceEpoch() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, 3);
ASSERT_LE(ms, 7);
checked = true;
fml::MessageLoop::GetCurrent().Terminate();
},
fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(5));
fml::ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(5));
loop.Run();
});
thread.join();
Expand All @@ -205,10 +206,10 @@ TEST(MessageLoop, TIMESENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
for (int target_ms = 0 + 2; target_ms < count + 2; target_ms++) {
auto begin = fml::TimePoint::Now();
auto begin = fml::ChronoTicksSinceEpoch();
loop.GetTaskRunner()->PostDelayedTask(
PLATFORM_SPECIFIC_CAPTURE(begin, target_ms, &checked)() {
auto delta = fml::TimePoint::Now() - begin;
auto delta = fml::ChronoTicksSinceEpoch() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, target_ms - 2);
ASSERT_LE(ms, target_ms + 2);
Expand All @@ -232,10 +233,10 @@ TEST(MessageLoop, TIMESENSITIVE(MultipleDelayedTasksWithDecreasingDeltas)) {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
for (int target_ms = count + 2; target_ms > 0 + 2; target_ms--) {
auto begin = fml::TimePoint::Now();
auto begin = fml::ChronoTicksSinceEpoch();
loop.GetTaskRunner()->PostDelayedTask(
PLATFORM_SPECIFIC_CAPTURE(begin, target_ms, &checked)() {
auto delta = fml::TimePoint::Now() - begin;
auto delta = fml::ChronoTicksSinceEpoch() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, target_ms - 2);
ASSERT_LE(ms, target_ms + 2);
Expand Down
15 changes: 8 additions & 7 deletions fml/task_source_unittests.cc
Expand Up @@ -7,6 +7,7 @@

#include "flutter/fml/macros.h"
#include "flutter/fml/task_source.h"
#include "flutter/fml/time/chrono_timestamp_provider.h"
#include "flutter/fml/time/time_delta.h"
#include "flutter/fml/time/time_point.h"
#include "gtest/gtest.h"
Expand All @@ -17,24 +18,24 @@ namespace testing {
TEST(TaskSourceTests, SimpleInitialization) {
TaskSource task_source = TaskSource(TaskQueueId(1));
task_source.RegisterTask(
{1, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kUnspecified});
{1, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUnspecified});
ASSERT_EQ(task_source.GetNumPendingTasks(), 1u);
}

TEST(TaskSourceTests, MultipleTaskGrades) {
TaskSource task_source = TaskSource(TaskQueueId(1));
task_source.RegisterTask(
{1, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kUnspecified});
{1, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUnspecified});
task_source.RegisterTask(
{2, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kUserInteraction});
{2, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUserInteraction});
task_source.RegisterTask(
{3, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kDartMicroTasks});
{3, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kDartMicroTasks});
ASSERT_EQ(task_source.GetNumPendingTasks(), 3u);
}

TEST(TaskSourceTests, SimpleOrdering) {
TaskSource task_source = TaskSource(TaskQueueId(1));
auto time_stamp = fml::TimePoint::Now();
auto time_stamp = ChronoTicksSinceEpoch();
int value = 0;
task_source.RegisterTask(
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kUnspecified});
Expand All @@ -51,7 +52,7 @@ TEST(TaskSourceTests, SimpleOrdering) {

TEST(TaskSourceTests, SimpleOrderingMultiTaskHeaps) {
TaskSource task_source = TaskSource(TaskQueueId(1));
auto time_stamp = fml::TimePoint::Now();
auto time_stamp = ChronoTicksSinceEpoch();
int value = 0;
task_source.RegisterTask(
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartMicroTasks});
Expand All @@ -71,7 +72,7 @@ TEST(TaskSourceTests, SimpleOrderingMultiTaskHeaps) {

TEST(TaskSourceTests, OrderingMultiTaskHeapsSecondaryPaused) {
TaskSource task_source = TaskSource(TaskQueueId(1));
auto time_stamp = fml::TimePoint::Now();
auto time_stamp = ChronoTicksSinceEpoch();
int value = 0;
task_source.RegisterTask(
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartMicroTasks});
Expand Down
27 changes: 27 additions & 0 deletions fml/time/chrono_timestamp_provider.cc
@@ -0,0 +1,27 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/fml/time/chrono_timestamp_provider.h"

#include <chrono>

#include "fml/time/time_delta.h"

namespace fml {

ChronoTimestampProvider::ChronoTimestampProvider() = default;

ChronoTimestampProvider::~ChronoTimestampProvider() = default;

fml::TimePoint ChronoTimestampProvider::Now() {
const auto chrono_time_point = std::chrono::steady_clock::now();
const auto ticks_since_epoch = chrono_time_point.time_since_epoch().count();
return fml::TimePoint::FromTicks(ticks_since_epoch);
}

fml::TimePoint ChronoTicksSinceEpoch() {
return ChronoTimestampProvider::Instance().Now();
}

} // namespace fml

0 comments on commit ce2f307

Please sign in to comment.