Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1cd1304

Browse files
authored
Control test timeouts based on debugger status or command line flags. (#16375)
1 parent c264e1c commit 1cd1304

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed

testing/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ source_set("testing") {
2727
testonly = true
2828

2929
sources = [
30+
"debugger_detection.cc",
31+
"debugger_detection.h",
3032
"run_all_unittests.cc",
3133
"test_timeout_listener.cc",
3234
"test_timeout_listener.h",

testing/debugger_detection.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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/testing/debugger_detection.h"
6+
7+
#include "flutter/fml/build_config.h"
8+
#include "flutter/fml/logging.h"
9+
10+
#if OS_MACOSX
11+
12+
#include <assert.h>
13+
#include <stdbool.h>
14+
#include <sys/sysctl.h>
15+
#include <sys/types.h>
16+
#include <unistd.h>
17+
18+
#endif // OS_MACOSX
19+
20+
#if OS_WIN
21+
22+
#include <windows.h>
23+
24+
#endif // OS_WIN
25+
26+
namespace flutter {
27+
namespace testing {
28+
29+
DebuggerStatus GetDebuggerStatus() {
30+
#if OS_MACOSX
31+
// From Technical Q&A QA1361 Detecting the Debugger
32+
// https://developer.apple.com/library/archive/qa/qa1361/_index.html
33+
int management_info_base[4];
34+
struct kinfo_proc info;
35+
size_t size;
36+
37+
// Initialize the flags so that, if sysctl fails for some bizarre
38+
// reason, we get a predictable result.
39+
info.kp_proc.p_flag = 0;
40+
41+
// Initialize management_info_base, which tells sysctl the info we want, in
42+
// this case we're looking for information about a specific process ID.
43+
management_info_base[0] = CTL_KERN;
44+
management_info_base[1] = KERN_PROC;
45+
management_info_base[2] = KERN_PROC_PID;
46+
management_info_base[3] = getpid();
47+
48+
// Call sysctl.
49+
50+
size = sizeof(info);
51+
auto status =
52+
::sysctl(management_info_base,
53+
sizeof(management_info_base) / sizeof(*management_info_base),
54+
&info, &size, NULL, 0);
55+
FML_CHECK(status == 0);
56+
57+
// We're being debugged if the P_TRACED flag is set.
58+
return ((info.kp_proc.p_flag & P_TRACED) != 0) ? DebuggerStatus::kAttached
59+
: DebuggerStatus::kDontKnow;
60+
61+
#elif OS_WIN
62+
return ::IsDebuggerPresent() ? DebuggerStatus::kAttached
63+
: DebuggerStatus::kDontKnow;
64+
65+
#else
66+
return DebuggerStatus::kDontKnow;
67+
#endif
68+
} // namespace testing
69+
70+
} // namespace testing
71+
} // namespace flutter

testing/debugger_detection.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#ifndef FLUTTER_TESTING_DEBUGGER_DETECTION_H_
6+
#define FLUTTER_TESTING_DEBUGGER_DETECTION_H_
7+
8+
#include "flutter/fml/macros.h"
9+
10+
namespace flutter {
11+
namespace testing {
12+
13+
enum class DebuggerStatus {
14+
kDontKnow,
15+
kAttached,
16+
};
17+
18+
DebuggerStatus GetDebuggerStatus();
19+
20+
} // namespace testing
21+
} // namespace flutter
22+
23+
#endif // FLUTTER_TESTING_DEBUGGER_DETECTION_H_

testing/run_all_unittests.cc

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <iostream>
6+
#include <optional>
7+
#include <string>
8+
59
#include "flutter/fml/build_config.h"
10+
#include "flutter/fml/command_line.h"
11+
#include "flutter/testing/debugger_detection.h"
612
#include "flutter/testing/test_timeout_listener.h"
713
#include "gtest/gtest.h"
814

915
#ifdef OS_IOS
1016
#include <asl.h>
1117
#endif // OS_IOS
1218

19+
std::optional<fml::TimeDelta> GetTestTimeoutFromArgs(int argc, char** argv) {
20+
const auto command_line = fml::CommandLineFromArgcArgv(argc, argv);
21+
22+
std::string timeout_seconds;
23+
if (!command_line.GetOptionValue("timeout", &timeout_seconds)) {
24+
// No timeout specified. Default to 30s.
25+
return fml::TimeDelta::FromSeconds(30u);
26+
}
27+
28+
const auto seconds = std::stoi(timeout_seconds);
29+
30+
if (seconds < 1) {
31+
return std::nullopt;
32+
}
33+
34+
return fml::TimeDelta::FromSeconds(seconds);
35+
}
36+
1337
int main(int argc, char** argv) {
1438
#ifdef OS_IOS
1539
asl_log_descriptor(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO,
@@ -19,7 +43,23 @@ int main(int argc, char** argv) {
1943
#endif // OS_IOS
2044

2145
::testing::InitGoogleTest(&argc, argv);
22-
auto timeout_listener = new flutter::testing::TestTimeoutListener();
46+
47+
// Check if the user has specified a timeout.
48+
const auto timeout = GetTestTimeoutFromArgs(argc, argv);
49+
if (!timeout.has_value()) {
50+
FML_LOG(INFO) << "Timeouts disabled via a command line flag.";
51+
return RUN_ALL_TESTS();
52+
}
53+
54+
// Check if the user is debugging the process.
55+
if (flutter::testing::GetDebuggerStatus() ==
56+
flutter::testing::DebuggerStatus::kAttached) {
57+
FML_LOG(INFO) << "Debugger is attached. Suspending test timeouts.";
58+
return RUN_ALL_TESTS();
59+
}
60+
61+
auto timeout_listener =
62+
new flutter::testing::TestTimeoutListener(timeout.value());
2363
auto& listeners = ::testing::UnitTest::GetInstance()->listeners();
2464
listeners.Append(timeout_listener);
2565
auto result = RUN_ALL_TESTS();

testing/test_timeout_listener.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ TestTimeoutListener::TestTimeoutListener(fml::TimeDelta timeout)
7272
: timeout_(timeout),
7373
listener_thread_("test_timeout_listener"),
7474
listener_thread_runner_(listener_thread_.GetTaskRunner()),
75-
pending_tests_(PendingTests::Create(listener_thread_runner_, timeout_)) {}
75+
pending_tests_(PendingTests::Create(listener_thread_runner_, timeout_)) {
76+
FML_LOG(INFO) << "Test timeout of " << timeout_.ToSeconds()
77+
<< " seconds per test case will be enforced.";
78+
}
7679

7780
TestTimeoutListener::~TestTimeoutListener() {
7881
listener_thread_runner_->PostTask(

testing/test_timeout_listener.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class PendingTests;
1919

2020
class TestTimeoutListener : public ::testing::EmptyTestEventListener {
2121
public:
22-
TestTimeoutListener(
23-
fml::TimeDelta timeout = fml::TimeDelta::FromSeconds(30u));
22+
TestTimeoutListener(fml::TimeDelta timeout);
2423

2524
~TestTimeoutListener();
2625

0 commit comments

Comments
 (0)