xref: /aosp_15_r20/external/cronet/base/test/launcher/test_launcher.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <memory>
12*6777b538SAndroid Build Coastguard Worker #include <set>
13*6777b538SAndroid Build Coastguard Worker #include <string>
14*6777b538SAndroid Build Coastguard Worker #include <string_view>
15*6777b538SAndroid Build Coastguard Worker #include <unordered_set>
16*6777b538SAndroid Build Coastguard Worker #include <vector>
17*6777b538SAndroid Build Coastguard Worker 
18*6777b538SAndroid Build Coastguard Worker #include "base/command_line.h"
19*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h"
20*6777b538SAndroid Build Coastguard Worker #include "base/process/launch.h"
21*6777b538SAndroid Build Coastguard Worker #include "base/test/gtest_util.h"
22*6777b538SAndroid Build Coastguard Worker #include "base/test/launcher/test_result.h"
23*6777b538SAndroid Build Coastguard Worker #include "base/test/launcher/test_results_tracker.h"
24*6777b538SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
25*6777b538SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h"
26*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
27*6777b538SAndroid Build Coastguard Worker #include "base/timer/timer.h"
28*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
29*6777b538SAndroid Build Coastguard Worker 
30*6777b538SAndroid Build Coastguard Worker namespace base {
31*6777b538SAndroid Build Coastguard Worker 
32*6777b538SAndroid Build Coastguard Worker // Constants for GTest command-line flags.
33*6777b538SAndroid Build Coastguard Worker extern const char kGTestFilterFlag[];
34*6777b538SAndroid Build Coastguard Worker extern const char kGTestFlagfileFlag[];
35*6777b538SAndroid Build Coastguard Worker extern const char kGTestHelpFlag[];
36*6777b538SAndroid Build Coastguard Worker extern const char kGTestListTestsFlag[];
37*6777b538SAndroid Build Coastguard Worker extern const char kGTestRepeatFlag[];
38*6777b538SAndroid Build Coastguard Worker extern const char kGTestRunDisabledTestsFlag[];
39*6777b538SAndroid Build Coastguard Worker extern const char kGTestOutputFlag[];
40*6777b538SAndroid Build Coastguard Worker extern const char kGTestShuffleFlag[];
41*6777b538SAndroid Build Coastguard Worker extern const char kGTestRandomSeedFlag[];
42*6777b538SAndroid Build Coastguard Worker extern const char kIsolatedScriptRunDisabledTestsFlag[];
43*6777b538SAndroid Build Coastguard Worker extern const char kIsolatedScriptTestFilterFlag[];
44*6777b538SAndroid Build Coastguard Worker extern const char kIsolatedScriptTestRepeatFlag[];
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker // Interface for use with LaunchTests that abstracts away exact details
47*6777b538SAndroid Build Coastguard Worker // which tests and how are run.
48*6777b538SAndroid Build Coastguard Worker class TestLauncherDelegate {
49*6777b538SAndroid Build Coastguard Worker  public:
50*6777b538SAndroid Build Coastguard Worker   // Called to get names of tests available for running. The delegate
51*6777b538SAndroid Build Coastguard Worker   // must put the result in |output| and return true on success.
52*6777b538SAndroid Build Coastguard Worker   virtual bool GetTests(std::vector<TestIdentifier>* output) = 0;
53*6777b538SAndroid Build Coastguard Worker 
54*6777b538SAndroid Build Coastguard Worker   // Additional delegate TestResult processing.
ProcessTestResults(std::vector<TestResult> & test_results,TimeDelta elapsed_time)55*6777b538SAndroid Build Coastguard Worker   virtual void ProcessTestResults(std::vector<TestResult>& test_results,
56*6777b538SAndroid Build Coastguard Worker                                   TimeDelta elapsed_time) {}
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker   // Called to get the command line for the specified tests.
59*6777b538SAndroid Build Coastguard Worker   // |output_file_| is populated with the path to the result file, and must
60*6777b538SAndroid Build Coastguard Worker   // be inside |temp_dir|.
61*6777b538SAndroid Build Coastguard Worker   virtual CommandLine GetCommandLine(const std::vector<std::string>& test_names,
62*6777b538SAndroid Build Coastguard Worker                                      const FilePath& temp_dir,
63*6777b538SAndroid Build Coastguard Worker                                      FilePath* output_file) = 0;
64*6777b538SAndroid Build Coastguard Worker 
65*6777b538SAndroid Build Coastguard Worker   // Invoked when a test process exceeds its runtime, immediately before it is
66*6777b538SAndroid Build Coastguard Worker   // terminated. |command_line| is the command line used to launch the process.
67*6777b538SAndroid Build Coastguard Worker   // NOTE: this method is invoked on the thread the process is launched on.
OnTestTimedOut(const CommandLine & cmd_line)68*6777b538SAndroid Build Coastguard Worker   virtual void OnTestTimedOut(const CommandLine& cmd_line) {}
69*6777b538SAndroid Build Coastguard Worker 
70*6777b538SAndroid Build Coastguard Worker   // Returns the delegate specific wrapper for command line.
71*6777b538SAndroid Build Coastguard Worker   // If it is not empty, it is prepended to the final command line.
72*6777b538SAndroid Build Coastguard Worker   virtual std::string GetWrapper() = 0;
73*6777b538SAndroid Build Coastguard Worker 
74*6777b538SAndroid Build Coastguard Worker   // Returns the delegate specific flags for launch options.
75*6777b538SAndroid Build Coastguard Worker   // The flags are specified in LaunchChildGTestProcessFlags.
76*6777b538SAndroid Build Coastguard Worker   virtual int GetLaunchOptions() = 0;
77*6777b538SAndroid Build Coastguard Worker 
78*6777b538SAndroid Build Coastguard Worker   // Returns the delegate specific timeout per test.
79*6777b538SAndroid Build Coastguard Worker   virtual TimeDelta GetTimeout() = 0;
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker   // Returns the delegate specific batch size.
82*6777b538SAndroid Build Coastguard Worker   virtual size_t GetBatchSize() = 0;
83*6777b538SAndroid Build Coastguard Worker 
84*6777b538SAndroid Build Coastguard Worker   // Returns true if test should run.
85*6777b538SAndroid Build Coastguard Worker   virtual bool ShouldRunTest(const TestIdentifier& test);
86*6777b538SAndroid Build Coastguard Worker 
87*6777b538SAndroid Build Coastguard Worker  protected:
88*6777b538SAndroid Build Coastguard Worker   virtual ~TestLauncherDelegate();
89*6777b538SAndroid Build Coastguard Worker };
90*6777b538SAndroid Build Coastguard Worker 
91*6777b538SAndroid Build Coastguard Worker // Launches tests using a TestLauncherDelegate.
92*6777b538SAndroid Build Coastguard Worker class TestLauncher {
93*6777b538SAndroid Build Coastguard Worker  public:
94*6777b538SAndroid Build Coastguard Worker   // Flags controlling behavior of LaunchChildGTestProcess.
95*6777b538SAndroid Build Coastguard Worker   enum LaunchChildGTestProcessFlags {
96*6777b538SAndroid Build Coastguard Worker     // Allows usage of job objects on Windows. Helps properly clean up child
97*6777b538SAndroid Build Coastguard Worker     // processes.
98*6777b538SAndroid Build Coastguard Worker     USE_JOB_OBJECTS = (1 << 0),
99*6777b538SAndroid Build Coastguard Worker 
100*6777b538SAndroid Build Coastguard Worker     // Allows breakaway from job on Windows. May result in some child processes
101*6777b538SAndroid Build Coastguard Worker     // not being properly terminated after launcher dies if these processes
102*6777b538SAndroid Build Coastguard Worker     // fail to cooperate.
103*6777b538SAndroid Build Coastguard Worker     ALLOW_BREAKAWAY_FROM_JOB = (1 << 1),
104*6777b538SAndroid Build Coastguard Worker   };
105*6777b538SAndroid Build Coastguard Worker 
106*6777b538SAndroid Build Coastguard Worker   // Enum for subprocess stdio redirect.
107*6777b538SAndroid Build Coastguard Worker   enum StdioRedirect { AUTO, ALWAYS, NEVER };
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker   struct LaunchOptions {
110*6777b538SAndroid Build Coastguard Worker     LaunchOptions();
111*6777b538SAndroid Build Coastguard Worker     LaunchOptions(const LaunchOptions& other);
112*6777b538SAndroid Build Coastguard Worker     ~LaunchOptions();
113*6777b538SAndroid Build Coastguard Worker 
114*6777b538SAndroid Build Coastguard Worker     int flags = 0;
115*6777b538SAndroid Build Coastguard Worker     // These mirror values in base::LaunchOptions, see it for details.
116*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
117*6777b538SAndroid Build Coastguard Worker     base::LaunchOptions::Inherit inherit_mode =
118*6777b538SAndroid Build Coastguard Worker         base::LaunchOptions::Inherit::kSpecific;
119*6777b538SAndroid Build Coastguard Worker     base::HandlesToInheritVector handles_to_inherit;
120*6777b538SAndroid Build Coastguard Worker #else
121*6777b538SAndroid Build Coastguard Worker     FileHandleMappingVector fds_to_remap;
122*6777b538SAndroid Build Coastguard Worker #endif
123*6777b538SAndroid Build Coastguard Worker   };
124*6777b538SAndroid Build Coastguard Worker 
125*6777b538SAndroid Build Coastguard Worker   // Constructor. |parallel_jobs| is the limit of simultaneous parallel test
126*6777b538SAndroid Build Coastguard Worker   // jobs. |retry_limit| is the default limit of retries for bots or all tests.
127*6777b538SAndroid Build Coastguard Worker   TestLauncher(TestLauncherDelegate* launcher_delegate,
128*6777b538SAndroid Build Coastguard Worker                size_t parallel_jobs,
129*6777b538SAndroid Build Coastguard Worker                size_t retry_limit = 1U);
130*6777b538SAndroid Build Coastguard Worker 
131*6777b538SAndroid Build Coastguard Worker   TestLauncher(const TestLauncher&) = delete;
132*6777b538SAndroid Build Coastguard Worker   TestLauncher& operator=(const TestLauncher&) = delete;
133*6777b538SAndroid Build Coastguard Worker 
134*6777b538SAndroid Build Coastguard Worker   // virtual to mock in testing.
135*6777b538SAndroid Build Coastguard Worker   virtual ~TestLauncher();
136*6777b538SAndroid Build Coastguard Worker 
137*6777b538SAndroid Build Coastguard Worker   // Runs the launcher. Must be called at most once.
138*6777b538SAndroid Build Coastguard Worker   // command_line is null by default.
139*6777b538SAndroid Build Coastguard Worker   // if null, uses command line for current process.
140*6777b538SAndroid Build Coastguard Worker   [[nodiscard]] bool Run(CommandLine* command_line = nullptr);
141*6777b538SAndroid Build Coastguard Worker 
142*6777b538SAndroid Build Coastguard Worker   // Launches a child process (assumed to be gtest-based binary) which runs
143*6777b538SAndroid Build Coastguard Worker   // tests indicated by |test_names|.
144*6777b538SAndroid Build Coastguard Worker   // |task_runner| is used to post results back to the launcher on the main
145*6777b538SAndroid Build Coastguard Worker   // thread. |task_temp_dir| is used for child process files such as user data,
146*6777b538SAndroid Build Coastguard Worker   // result file, and flag_file. |child_temp_dir|, if not empty, specifies a
147*6777b538SAndroid Build Coastguard Worker   // directory (within task_temp_dir) that the child process will use as its
148*6777b538SAndroid Build Coastguard Worker   // process-wide temporary directory.
149*6777b538SAndroid Build Coastguard Worker   // virtual to mock in testing.
150*6777b538SAndroid Build Coastguard Worker   virtual void LaunchChildGTestProcess(
151*6777b538SAndroid Build Coastguard Worker       scoped_refptr<TaskRunner> task_runner,
152*6777b538SAndroid Build Coastguard Worker       const std::vector<std::string>& test_names,
153*6777b538SAndroid Build Coastguard Worker       const FilePath& task_temp_dir,
154*6777b538SAndroid Build Coastguard Worker       const FilePath& child_temp_dir);
155*6777b538SAndroid Build Coastguard Worker 
156*6777b538SAndroid Build Coastguard Worker   // Called when a test has finished running.
157*6777b538SAndroid Build Coastguard Worker   void OnTestFinished(const TestResult& result);
158*6777b538SAndroid Build Coastguard Worker 
159*6777b538SAndroid Build Coastguard Worker   // Returns true if child test processes should have dedicated temporary
160*6777b538SAndroid Build Coastguard Worker   // directories.
SupportsPerChildTempDirs()161*6777b538SAndroid Build Coastguard Worker   static constexpr bool SupportsPerChildTempDirs() {
162*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
163*6777b538SAndroid Build Coastguard Worker     return true;
164*6777b538SAndroid Build Coastguard Worker #else
165*6777b538SAndroid Build Coastguard Worker     // TODO(https://crbug.com/1038857): Enable for macOS, Linux, and Fuchsia.
166*6777b538SAndroid Build Coastguard Worker     return false;
167*6777b538SAndroid Build Coastguard Worker #endif
168*6777b538SAndroid Build Coastguard Worker   }
169*6777b538SAndroid Build Coastguard Worker 
170*6777b538SAndroid Build Coastguard Worker  private:
171*6777b538SAndroid Build Coastguard Worker   [[nodiscard]] bool Init(CommandLine* command_line);
172*6777b538SAndroid Build Coastguard Worker 
173*6777b538SAndroid Build Coastguard Worker   // Gets tests from the delegate, and converts to TestInfo objects.
174*6777b538SAndroid Build Coastguard Worker   // Catches and logs uninstantiated parameterized tests.
175*6777b538SAndroid Build Coastguard Worker   // Returns false if delegate fails to return tests.
176*6777b538SAndroid Build Coastguard Worker   bool InitTests();
177*6777b538SAndroid Build Coastguard Worker 
178*6777b538SAndroid Build Coastguard Worker   // Some of the TestLauncherDelegate implementations don't call into gtest
179*6777b538SAndroid Build Coastguard Worker   // until they've already split into test-specific processes. This results
180*6777b538SAndroid Build Coastguard Worker   // in gtest's native shuffle implementation attempting to shuffle one test.
181*6777b538SAndroid Build Coastguard Worker   // Shuffling the list of tests in the test launcher (before the delegate
182*6777b538SAndroid Build Coastguard Worker   // gets involved) ensures that the entire shard is shuffled.
183*6777b538SAndroid Build Coastguard Worker   bool ShuffleTests(CommandLine* command_line);
184*6777b538SAndroid Build Coastguard Worker 
185*6777b538SAndroid Build Coastguard Worker   // Move all PRE_ tests prior to the final test in order.
186*6777b538SAndroid Build Coastguard Worker   // Validate tests names. This includes no name conflict between tests
187*6777b538SAndroid Build Coastguard Worker   // without DISABLED_ prefix, and orphaned PRE_ tests.
188*6777b538SAndroid Build Coastguard Worker   // Add all tests and disabled tests names to result tracker.
189*6777b538SAndroid Build Coastguard Worker   // Filter Disabled tests if not flagged to run.
190*6777b538SAndroid Build Coastguard Worker   // Returns false if any violation is found.
191*6777b538SAndroid Build Coastguard Worker   bool ProcessAndValidateTests();
192*6777b538SAndroid Build Coastguard Worker 
193*6777b538SAndroid Build Coastguard Worker   // Runs all tests in current iteration.
194*6777b538SAndroid Build Coastguard Worker   void RunTests();
195*6777b538SAndroid Build Coastguard Worker 
196*6777b538SAndroid Build Coastguard Worker   // Print test names that almost match a filter (matches *<filter>*).
197*6777b538SAndroid Build Coastguard Worker   void PrintFuzzyMatchingTestNames();
198*6777b538SAndroid Build Coastguard Worker 
199*6777b538SAndroid Build Coastguard Worker   // Retry to run tests that failed during RunTests.
200*6777b538SAndroid Build Coastguard Worker   // Returns false if retry still fails or unable to start.
201*6777b538SAndroid Build Coastguard Worker   bool RunRetryTests();
202*6777b538SAndroid Build Coastguard Worker 
203*6777b538SAndroid Build Coastguard Worker   void CombinePositiveTestFilters(std::vector<std::string> filter_a,
204*6777b538SAndroid Build Coastguard Worker                                   std::vector<std::string> filter_b);
205*6777b538SAndroid Build Coastguard Worker 
206*6777b538SAndroid Build Coastguard Worker   // Rest counters, retry tests list, and test result tracker.
207*6777b538SAndroid Build Coastguard Worker   void OnTestIterationStart();
208*6777b538SAndroid Build Coastguard Worker 
209*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_POSIX)
210*6777b538SAndroid Build Coastguard Worker   void OnShutdownPipeReadable();
211*6777b538SAndroid Build Coastguard Worker #endif
212*6777b538SAndroid Build Coastguard Worker 
213*6777b538SAndroid Build Coastguard Worker   // Saves test results summary as JSON if requested from command line.
214*6777b538SAndroid Build Coastguard Worker   void MaybeSaveSummaryAsJSON(const std::vector<std::string>& additional_tags);
215*6777b538SAndroid Build Coastguard Worker 
216*6777b538SAndroid Build Coastguard Worker   // Called when a test iteration is finished.
217*6777b538SAndroid Build Coastguard Worker   void OnTestIterationFinished();
218*6777b538SAndroid Build Coastguard Worker 
219*6777b538SAndroid Build Coastguard Worker   // Called by the delay timer when no output was made for a while.
220*6777b538SAndroid Build Coastguard Worker   void OnOutputTimeout();
221*6777b538SAndroid Build Coastguard Worker 
222*6777b538SAndroid Build Coastguard Worker   // Creates and starts a ThreadPoolInstance with |num_parallel_jobs| dedicated
223*6777b538SAndroid Build Coastguard Worker   // to foreground blocking tasks (corresponds to the traits used to launch and
224*6777b538SAndroid Build Coastguard Worker   // wait for child processes). virtual to mock in testing.
225*6777b538SAndroid Build Coastguard Worker   virtual void CreateAndStartThreadPool(size_t num_parallel_jobs);
226*6777b538SAndroid Build Coastguard Worker 
227*6777b538SAndroid Build Coastguard Worker   // Callback to receive result of a test.
228*6777b538SAndroid Build Coastguard Worker   // |result_file| is a path to xml file written by child process.
229*6777b538SAndroid Build Coastguard Worker   // It contains information about test and failed
230*6777b538SAndroid Build Coastguard Worker   // EXPECT/ASSERT/DCHECK statements. Test launcher parses that
231*6777b538SAndroid Build Coastguard Worker   // file to get additional information about test run (status,
232*6777b538SAndroid Build Coastguard Worker   // error-messages, stack-traces and file/line for failures).
233*6777b538SAndroid Build Coastguard Worker   // |thread_id| is the actual worker thread that launching the child process.
234*6777b538SAndroid Build Coastguard Worker   // |process_num| is a sequence number of the process executed in the run.
235*6777b538SAndroid Build Coastguard Worker   // |leaked_items| is the number of files and/or directories remaining in the
236*6777b538SAndroid Build Coastguard Worker   // child process's temporary directory upon its termination.
237*6777b538SAndroid Build Coastguard Worker   void ProcessTestResults(const std::vector<std::string>& test_names,
238*6777b538SAndroid Build Coastguard Worker                           const FilePath& result_file,
239*6777b538SAndroid Build Coastguard Worker                           const std::string& output,
240*6777b538SAndroid Build Coastguard Worker                           TimeDelta elapsed_time,
241*6777b538SAndroid Build Coastguard Worker                           int exit_code,
242*6777b538SAndroid Build Coastguard Worker                           bool was_timeout,
243*6777b538SAndroid Build Coastguard Worker                           PlatformThreadId thread_id,
244*6777b538SAndroid Build Coastguard Worker                           int process_num,
245*6777b538SAndroid Build Coastguard Worker                           int leaked_items);
246*6777b538SAndroid Build Coastguard Worker 
247*6777b538SAndroid Build Coastguard Worker   std::vector<std::string> CollectTests();
248*6777b538SAndroid Build Coastguard Worker 
249*6777b538SAndroid Build Coastguard Worker   // Helper to tell if the test runs in current shard.
250*6777b538SAndroid Build Coastguard Worker   // `prefix_stripped_name` is the test name excluding DISABLED_ and
251*6777b538SAndroid Build Coastguard Worker   // PRE_ prefixes.
252*6777b538SAndroid Build Coastguard Worker   bool ShouldRunInCurrentShard(std::string_view prefix_stripped_name) const;
253*6777b538SAndroid Build Coastguard Worker 
254*6777b538SAndroid Build Coastguard Worker   // Helper to check whether only exact positive filter is passed via
255*6777b538SAndroid Build Coastguard Worker   // a filter file.
256*6777b538SAndroid Build Coastguard Worker   bool IsOnlyExactPositiveFilterFromFile(const CommandLine* command_line) const;
257*6777b538SAndroid Build Coastguard Worker 
258*6777b538SAndroid Build Coastguard Worker   // Make sure we don't accidentally call the wrong methods e.g. on the worker
259*6777b538SAndroid Build Coastguard Worker   // pool thread.  Should be the first member so that it's destroyed last: when
260*6777b538SAndroid Build Coastguard Worker   // destroying other members, especially the worker pool, we may check the code
261*6777b538SAndroid Build Coastguard Worker   // is running on the correct thread.
262*6777b538SAndroid Build Coastguard Worker   ThreadChecker thread_checker_;
263*6777b538SAndroid Build Coastguard Worker 
264*6777b538SAndroid Build Coastguard Worker   raw_ptr<TestLauncherDelegate> launcher_delegate_;
265*6777b538SAndroid Build Coastguard Worker 
266*6777b538SAndroid Build Coastguard Worker   // Support for outer sharding, just like gtest does.
267*6777b538SAndroid Build Coastguard Worker   int32_t total_shards_;  // Total number of outer shards, at least one.
268*6777b538SAndroid Build Coastguard Worker   int32_t shard_index_;   // Index of shard the launcher is to run.
269*6777b538SAndroid Build Coastguard Worker 
270*6777b538SAndroid Build Coastguard Worker   int cycles_;  // Number of remaining test iterations, or -1 for infinite.
271*6777b538SAndroid Build Coastguard Worker 
272*6777b538SAndroid Build Coastguard Worker   // Test filters (empty means no filter).
273*6777b538SAndroid Build Coastguard Worker   bool has_at_least_one_positive_filter_;
274*6777b538SAndroid Build Coastguard Worker   std::vector<std::string> positive_test_filter_;
275*6777b538SAndroid Build Coastguard Worker   std::vector<std::string> negative_test_filter_;
276*6777b538SAndroid Build Coastguard Worker 
277*6777b538SAndroid Build Coastguard Worker   // Enforce to run all test cases listed in exact positive filter.
278*6777b538SAndroid Build Coastguard Worker   bool enforce_exact_postive_filter_;
279*6777b538SAndroid Build Coastguard Worker 
280*6777b538SAndroid Build Coastguard Worker   // Class to encapsulate gtest information.
281*6777b538SAndroid Build Coastguard Worker   class TestInfo;
282*6777b538SAndroid Build Coastguard Worker 
283*6777b538SAndroid Build Coastguard Worker   // Tests to use (cached result of TestLauncherDelegate::GetTests).
284*6777b538SAndroid Build Coastguard Worker   std::vector<TestInfo> tests_;
285*6777b538SAndroid Build Coastguard Worker 
286*6777b538SAndroid Build Coastguard Worker   // Threshold for number of broken tests.
287*6777b538SAndroid Build Coastguard Worker   size_t broken_threshold_;
288*6777b538SAndroid Build Coastguard Worker 
289*6777b538SAndroid Build Coastguard Worker   // Number of tests started in this iteration.
290*6777b538SAndroid Build Coastguard Worker   size_t test_started_count_;
291*6777b538SAndroid Build Coastguard Worker 
292*6777b538SAndroid Build Coastguard Worker   // Number of tests finished in this iteration.
293*6777b538SAndroid Build Coastguard Worker   size_t test_finished_count_;
294*6777b538SAndroid Build Coastguard Worker 
295*6777b538SAndroid Build Coastguard Worker   // Number of tests successfully finished in this iteration.
296*6777b538SAndroid Build Coastguard Worker   size_t test_success_count_;
297*6777b538SAndroid Build Coastguard Worker 
298*6777b538SAndroid Build Coastguard Worker   // Number of tests either timing out or having an unknown result,
299*6777b538SAndroid Build Coastguard Worker   // likely indicating a more systemic problem if widespread.
300*6777b538SAndroid Build Coastguard Worker   size_t test_broken_count_;
301*6777b538SAndroid Build Coastguard Worker 
302*6777b538SAndroid Build Coastguard Worker   // How many retries are left.
303*6777b538SAndroid Build Coastguard Worker   size_t retries_left_;
304*6777b538SAndroid Build Coastguard Worker 
305*6777b538SAndroid Build Coastguard Worker   // Maximum number of retries per iteration.
306*6777b538SAndroid Build Coastguard Worker   size_t retry_limit_;
307*6777b538SAndroid Build Coastguard Worker 
308*6777b538SAndroid Build Coastguard Worker   // Maximum number of output bytes per test.
309*6777b538SAndroid Build Coastguard Worker   size_t output_bytes_limit_;
310*6777b538SAndroid Build Coastguard Worker 
311*6777b538SAndroid Build Coastguard Worker   // If true will not early exit nor skip retries even if too many tests are
312*6777b538SAndroid Build Coastguard Worker   // broken.
313*6777b538SAndroid Build Coastguard Worker   bool force_run_broken_tests_;
314*6777b538SAndroid Build Coastguard Worker 
315*6777b538SAndroid Build Coastguard Worker   // Tests to retry in this iteration.
316*6777b538SAndroid Build Coastguard Worker   std::unordered_set<std::string> tests_to_retry_;
317*6777b538SAndroid Build Coastguard Worker 
318*6777b538SAndroid Build Coastguard Worker   TestResultsTracker results_tracker_;
319*6777b538SAndroid Build Coastguard Worker 
320*6777b538SAndroid Build Coastguard Worker   // Watchdog timer to make sure we do not go without output for too long.
321*6777b538SAndroid Build Coastguard Worker   DelayTimer watchdog_timer_;
322*6777b538SAndroid Build Coastguard Worker 
323*6777b538SAndroid Build Coastguard Worker   // Number of jobs to run in parallel.
324*6777b538SAndroid Build Coastguard Worker   size_t parallel_jobs_;
325*6777b538SAndroid Build Coastguard Worker 
326*6777b538SAndroid Build Coastguard Worker   // Switch to control tests stdio :{auto, always, never}
327*6777b538SAndroid Build Coastguard Worker   StdioRedirect print_test_stdio_;
328*6777b538SAndroid Build Coastguard Worker 
329*6777b538SAndroid Build Coastguard Worker   // Skip disabled tests unless explicitly requested.
330*6777b538SAndroid Build Coastguard Worker   bool skip_disabled_tests_;
331*6777b538SAndroid Build Coastguard Worker 
332*6777b538SAndroid Build Coastguard Worker   // Stop test iterations due to failure.
333*6777b538SAndroid Build Coastguard Worker   bool stop_on_failure_;
334*6777b538SAndroid Build Coastguard Worker 
335*6777b538SAndroid Build Coastguard Worker   // Path to JSON summary result file.
336*6777b538SAndroid Build Coastguard Worker   FilePath summary_path_;
337*6777b538SAndroid Build Coastguard Worker 
338*6777b538SAndroid Build Coastguard Worker   // Path to trace file.
339*6777b538SAndroid Build Coastguard Worker   FilePath trace_path_;
340*6777b538SAndroid Build Coastguard Worker 
341*6777b538SAndroid Build Coastguard Worker   // redirect stdio of subprocess
342*6777b538SAndroid Build Coastguard Worker   bool redirect_stdio_;
343*6777b538SAndroid Build Coastguard Worker 
344*6777b538SAndroid Build Coastguard Worker   // Number of times all tests should be repeated during each iteration.
345*6777b538SAndroid Build Coastguard Worker   // 1 if gtest_repeat is not specified or gtest_break_on_failure is specified.
346*6777b538SAndroid Build Coastguard Worker   // Otherwise it matches gtest_repeat value.
347*6777b538SAndroid Build Coastguard Worker   int repeats_per_iteration_ = 1;
348*6777b538SAndroid Build Coastguard Worker };
349*6777b538SAndroid Build Coastguard Worker 
350*6777b538SAndroid Build Coastguard Worker // Watch a gtest XML result file for tests run in a batch to complete.
351*6777b538SAndroid Build Coastguard Worker class ResultWatcher {
352*6777b538SAndroid Build Coastguard Worker  public:
353*6777b538SAndroid Build Coastguard Worker   ResultWatcher(FilePath result_file, size_t num_tests);
354*6777b538SAndroid Build Coastguard Worker 
355*6777b538SAndroid Build Coastguard Worker   // Poll the incomplete result file, blocking until the batch completes or a
356*6777b538SAndroid Build Coastguard Worker   // test timed out. Returns true iff no tests timed out.
357*6777b538SAndroid Build Coastguard Worker   bool PollUntilDone(TimeDelta timeout_per_test);
358*6777b538SAndroid Build Coastguard Worker 
359*6777b538SAndroid Build Coastguard Worker   // Wait and block for up to `timeout` before we poll the result file again.
360*6777b538SAndroid Build Coastguard Worker   // Returns true iff we should stop polling the results early.
361*6777b538SAndroid Build Coastguard Worker   virtual bool WaitWithTimeout(TimeDelta timeout) = 0;
362*6777b538SAndroid Build Coastguard Worker 
363*6777b538SAndroid Build Coastguard Worker  private:
364*6777b538SAndroid Build Coastguard Worker   // Read the results, check if a timeout occurred, and then return how long
365*6777b538SAndroid Build Coastguard Worker   // the polling loop should wait for. A nonpositive return value indicates a
366*6777b538SAndroid Build Coastguard Worker   // timeout (i.e., the next check is overdue).
367*6777b538SAndroid Build Coastguard Worker   //
368*6777b538SAndroid Build Coastguard Worker   // If a timeout did not occur, this method tries to schedule the next check
369*6777b538SAndroid Build Coastguard Worker   // for `timeout_per_test` since the last test completed.
370*6777b538SAndroid Build Coastguard Worker   TimeDelta PollOnce(TimeDelta timeout_per_test);
371*6777b538SAndroid Build Coastguard Worker 
372*6777b538SAndroid Build Coastguard Worker   // Get the timestamp of the test that completed most recently. If no tests
373*6777b538SAndroid Build Coastguard Worker   // have completed, return the null time.
374*6777b538SAndroid Build Coastguard Worker   Time LatestCompletionTimestamp(const std::vector<TestResult>& test_results);
375*6777b538SAndroid Build Coastguard Worker 
376*6777b538SAndroid Build Coastguard Worker   // Path to the results file.
377*6777b538SAndroid Build Coastguard Worker   FilePath result_file_;
378*6777b538SAndroid Build Coastguard Worker 
379*6777b538SAndroid Build Coastguard Worker   // The number of tests that run in this batch.
380*6777b538SAndroid Build Coastguard Worker   size_t num_tests_;
381*6777b538SAndroid Build Coastguard Worker 
382*6777b538SAndroid Build Coastguard Worker   // The threshold past which we attribute a large time since latest completion
383*6777b538SAndroid Build Coastguard Worker   // to daylight savings time instead of a timed out test.
384*6777b538SAndroid Build Coastguard Worker   static constexpr TimeDelta kDaylightSavingsThreshold = Minutes(50);
385*6777b538SAndroid Build Coastguard Worker };
386*6777b538SAndroid Build Coastguard Worker 
387*6777b538SAndroid Build Coastguard Worker // Return the number of parallel jobs to use, or 0U in case of error.
388*6777b538SAndroid Build Coastguard Worker size_t NumParallelJobs(unsigned int cores_per_job);
389*6777b538SAndroid Build Coastguard Worker 
390*6777b538SAndroid Build Coastguard Worker // Extract part from |full_output| that applies to |result|.
391*6777b538SAndroid Build Coastguard Worker std::string GetTestOutputSnippet(const TestResult& result,
392*6777b538SAndroid Build Coastguard Worker                                  const std::string& full_output);
393*6777b538SAndroid Build Coastguard Worker 
394*6777b538SAndroid Build Coastguard Worker // Truncates a snippet to approximately the allowed length, while trying to
395*6777b538SAndroid Build Coastguard Worker // retain fatal messages. Exposed for testing only.
396*6777b538SAndroid Build Coastguard Worker std::string TruncateSnippetFocused(const std::string_view snippet,
397*6777b538SAndroid Build Coastguard Worker                                    size_t byte_limit);
398*6777b538SAndroid Build Coastguard Worker 
399*6777b538SAndroid Build Coastguard Worker }  // namespace base
400*6777b538SAndroid Build Coastguard Worker 
401*6777b538SAndroid Build Coastguard Worker #endif  // BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
402