|
| 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 "flutter/shell/profiling/sampling_profiler.h" |
| 6 | +#include "flutter/fml/message_loop_impl.h" |
| 7 | +#include "flutter/fml/thread.h" |
| 8 | +#include "flutter/testing/testing.h" |
| 9 | +#include "gmock/gmock.h" |
| 10 | + |
| 11 | +using testing::_; |
| 12 | +using testing::Invoke; |
| 13 | + |
| 14 | +namespace fml { |
| 15 | +namespace { |
| 16 | +class MockTaskRunner : public fml::TaskRunner { |
| 17 | + public: |
| 18 | + inline static RefPtr<MockTaskRunner> Create() { |
| 19 | + return AdoptRef(new MockTaskRunner()); |
| 20 | + } |
| 21 | + MOCK_METHOD1(PostTask, void(const fml::closure& task)); |
| 22 | + MOCK_METHOD2(PostTaskForTime, |
| 23 | + void(const fml::closure& task, fml::TimePoint target_time)); |
| 24 | + MOCK_METHOD2(PostDelayedTask, |
| 25 | + void(const fml::closure& task, fml::TimeDelta delay)); |
| 26 | + MOCK_METHOD0(RunsTasksOnCurrentThread, bool()); |
| 27 | + MOCK_METHOD0(GetTaskQueueId, TaskQueueId()); |
| 28 | + |
| 29 | + private: |
| 30 | + MockTaskRunner() : TaskRunner(fml::RefPtr<MessageLoopImpl>()) {} |
| 31 | +}; |
| 32 | +} // namespace |
| 33 | +} // namespace fml |
| 34 | + |
| 35 | +namespace flutter { |
| 36 | + |
| 37 | +TEST(SamplingProfilerTest, DeleteAfterStart) { |
| 38 | + auto thread = |
| 39 | + std::make_unique<fml::Thread>(flutter::testing::GetCurrentTestName()); |
| 40 | + auto task_runner = fml::MockTaskRunner::Create(); |
| 41 | + std::atomic<int> invoke_count = 0; |
| 42 | + |
| 43 | + // Ignore calls to PostTask since that would require mocking out calls to |
| 44 | + // Dart. |
| 45 | + EXPECT_CALL(*task_runner, PostDelayedTask(_, _)) |
| 46 | + .WillRepeatedly( |
| 47 | + Invoke([&](const fml::closure& task, fml::TimeDelta delay) { |
| 48 | + invoke_count.fetch_add(1); |
| 49 | + thread->GetTaskRunner()->PostTask(task); |
| 50 | + })); |
| 51 | + |
| 52 | + { |
| 53 | + auto profiler = SamplingProfiler( |
| 54 | + "profiler", |
| 55 | + /*profiler_task_runner=*/task_runner, [] { return ProfileSample(); }, |
| 56 | + /*num_samples_per_sec=*/1000); |
| 57 | + profiler.Start(); |
| 58 | + } |
| 59 | + int invoke_count_at_delete = invoke_count.load(); |
| 60 | + std::this_thread::sleep_for(std::chrono::milliseconds(2)); // nyquist |
| 61 | + ASSERT_EQ(invoke_count_at_delete, invoke_count.load()); |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace flutter |
0 commit comments