Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
sampling_profiler_unittest.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <thread>
6
7#include "flutter/fml/message_loop_impl.h"
8#include "flutter/fml/thread.h"
9#include "flutter/shell/profiling/sampling_profiler.h"
10#include "flutter/testing/testing.h"
11#include "gmock/gmock.h"
12
13using testing::_;
14using testing::Invoke;
15
16namespace fml {
17namespace {
18class MockTaskRunner : public fml::TaskRunner {
19 public:
20 inline static RefPtr<MockTaskRunner> Create() {
21 return AdoptRef(new MockTaskRunner());
22 }
23 MOCK_METHOD(void, PostTask, (const fml::closure& task), (override));
24 MOCK_METHOD(void,
25 PostTaskForTime,
26 (const fml::closure& task, fml::TimePoint target_time),
27 (override));
28 MOCK_METHOD(void,
29 PostDelayedTask,
30 (const fml::closure& task, fml::TimeDelta delay),
31 (override));
32 MOCK_METHOD(bool, RunsTasksOnCurrentThread, (), (override));
33 MOCK_METHOD(TaskQueueId, GetTaskQueueId, (), (override));
34
35 private:
36 MockTaskRunner() : TaskRunner(fml::RefPtr<MessageLoopImpl>()) {}
37};
38} // namespace
39} // namespace fml
40
41namespace flutter {
42
43TEST(SamplingProfilerTest, DeleteAfterStart) {
44 auto thread =
45 std::make_unique<fml::Thread>(flutter::testing::GetCurrentTestName());
46 auto task_runner = fml::MockTaskRunner::Create();
47 std::atomic<int> invoke_count = 0;
48
49 // Ignore calls to PostTask since that would require mocking out calls to
50 // Dart.
51 EXPECT_CALL(*task_runner, PostDelayedTask(_, _))
52 .WillRepeatedly(
53 Invoke([&](const fml::closure& task, fml::TimeDelta delay) {
54 invoke_count.fetch_add(1);
55 thread->GetTaskRunner()->PostTask(task);
56 }));
57
58 {
59 auto profiler = SamplingProfiler(
60 "profiler",
61 /*profiler_task_runner=*/task_runner, [] { return ProfileSample(); },
62 /*num_samples_per_sec=*/1000);
63 profiler.Start();
64 }
65 int invoke_count_at_delete = invoke_count.load();
66 std::this_thread::sleep_for(std::chrono::milliseconds(2)); // nyquist
67 ASSERT_EQ(invoke_count_at_delete, invoke_count.load());
68}
69
70} // namespace flutter
#define TEST(S, s, D, expected)
static sk_sp< Effect > Create()
ObjectPtr Invoke(const Library &lib, const char *name)
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition testing.cc:15
RefPtr< T > AdoptRef(T *ptr)
Definition ref_ptr.h:222
std::function< void()> closure
Definition closure.h:14