Flutter Engine
 
Loading...
Searching...
No Matches
test_timeout_listener.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
6
7#include <map>
8#include <sstream>
9
10namespace flutter::testing {
11
12class PendingTests : public std::enable_shared_from_this<PendingTests> {
13 public:
14 static std::shared_ptr<PendingTests> Create(
15 fml::RefPtr<fml::TaskRunner> host_task_runner,
16 fml::TimeDelta timeout) {
17 return std::shared_ptr<PendingTests>(
18 new PendingTests(std::move(host_task_runner), timeout));
19 }
20
21 ~PendingTests() = default;
22
23 void OnTestBegin(const std::string& test_name, fml::TimePoint test_time) {
24 FML_CHECK(tests_.find(test_name) == tests_.end())
25 << "Attempting to start a test that is already pending.";
26 tests_[test_name] = test_time;
27
28 host_task_runner_->PostDelayedTask(
29 [weak = weak_from_this()] {
30 if (auto strong = weak.lock()) {
31 strong->CheckTimedOutTests();
32 }
33 },
34 timeout_);
35 }
36
37 void OnTestEnd(const std::string& test_name) { tests_.erase(test_name); }
38
39 void CheckTimedOutTests() const {
40 const auto now = fml::TimePoint::Now();
41
42 for (const auto& test : tests_) {
43 auto delay = now - test.second;
44 FML_CHECK(delay < timeout_)
45 << "Test " << test.first << " did not complete in "
46 << timeout_.ToSeconds()
47 << " seconds and is assumed to be hung. Killing the test harness.";
48 }
49 }
50
51 private:
52 using TestData = std::map<std::string, fml::TimePoint>;
53
54 fml::RefPtr<fml::TaskRunner> host_task_runner_;
55 TestData tests_;
56 const fml::TimeDelta timeout_;
57
59 fml::TimeDelta timeout)
60 : host_task_runner_(std::move(host_task_runner)), timeout_(timeout) {}
61
62 FML_DISALLOW_COPY_AND_ASSIGN(PendingTests);
63};
64
65template <class T>
66auto WeakPtr(const std::shared_ptr<T>& pointer) {
67 return std::weak_ptr<T>{pointer};
68}
69
71 : timeout_(timeout),
72 listener_thread_("test_timeout_listener"),
73 listener_thread_runner_(listener_thread_.GetTaskRunner()),
74 pending_tests_(PendingTests::Create(listener_thread_runner_, timeout_)) {
75 FML_LOG(INFO) << "Test timeout of " << timeout_.ToSeconds()
76 << " seconds per test case will be enforced.";
77}
78
80 listener_thread_runner_->PostTask(
81 [tests = std::move(pending_tests_)]() mutable { tests.reset(); });
82 FML_CHECK(pending_tests_ == nullptr);
83}
84
85static std::string GetTestNameFromTestInfo(
86 const ::testing::TestInfo& test_info) {
87 std::stringstream stream;
88 stream << test_info.test_suite_name();
89 stream << ".";
90 stream << test_info.name();
91 if (auto type_param = test_info.type_param()) {
92 stream << "/" << type_param;
93 }
94 if (auto value_param = test_info.value_param()) {
95 stream << "/" << value_param;
96 }
97 return stream.str();
98}
99
100// |testing::EmptyTestEventListener|
101void TestTimeoutListener::OnTestStart(const ::testing::TestInfo& test_info) {
102 listener_thread_runner_->PostTask([weak_tests = WeakPtr(pending_tests_),
103 name = GetTestNameFromTestInfo(test_info),
104 now = fml::TimePoint::Now()]() {
105 if (auto tests = weak_tests.lock()) {
106 tests->OnTestBegin(name, now);
107 }
108 });
109}
110
111// |testing::EmptyTestEventListener|
112void TestTimeoutListener::OnTestEnd(const ::testing::TestInfo& test_info) {
113 listener_thread_runner_->PostTask(
114 [weak_tests = WeakPtr(pending_tests_),
115 name = GetTestNameFromTestInfo(test_info)]() {
116 if (auto tests = weak_tests.lock()) {
117 tests->OnTestEnd(name);
118 }
119 });
120}
121
122} // namespace flutter::testing
void OnTestEnd(const std::string &test_name)
void OnTestBegin(const std::string &test_name, fml::TimePoint test_time)
static std::shared_ptr< PendingTests > Create(fml::RefPtr< fml::TaskRunner > host_task_runner, fml::TimeDelta timeout)
virtual void PostTask(const fml::closure &task) override
virtual void PostDelayedTask(const fml::closure &task, fml::TimeDelta delay)
constexpr int64_t ToSeconds() const
Definition time_delta.h:64
static TimePoint Now()
Definition time_point.cc:49
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
static std::string GetTestNameFromTestInfo(const ::testing::TestInfo &test_info)
auto WeakPtr(const std::shared_ptr< T > &pointer)
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
Definition ref_ptr.h:261