blob: 8291bd2f64c67bec3c1bde0bc09dcb009a01ca70 [file] [log] [blame]
Andrew Top0d1858f2019-05-15 22:01:47 -07001// Copyright 2013 The Chromium 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 BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
6#define BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
7
8#include <memory>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/compiler_specific.h"
14#include "base/macros.h"
15#include "base/process/launch.h"
16#include "base/test/gtest_util.h"
17#include "base/test/launcher/test_result.h"
18#include "base/test/launcher/test_results_tracker.h"
19#include "base/threading/thread_checker.h"
20#include "base/time/time.h"
21#include "base/timer/timer.h"
22#include "build/build_config.h"
23#include "starboard/types.h"
24
25#if !defined(STARBOARD)
26
27namespace base {
28
29class CommandLine;
30struct LaunchOptions;
31class TestLauncher;
32
33// Constants for GTest command-line flags.
34extern const char kGTestFilterFlag[];
35extern const char kGTestFlagfileFlag[];
36extern const char kGTestHelpFlag[];
37extern const char kGTestListTestsFlag[];
38extern const char kGTestRepeatFlag[];
39extern const char kGTestRunDisabledTestsFlag[];
40extern const char kGTestOutputFlag[];
41extern const char kGTestShuffleFlag[];
42extern const char kGTestRandomSeedFlag[];
43
44// Interface for use with LaunchTests that abstracts away exact details
45// which tests and how are run.
46class TestLauncherDelegate {
47 public:
48 // Called to get names of tests available for running. The delegate
49 // must put the result in |output| and return true on success.
50 virtual bool GetTests(std::vector<TestIdentifier>* output) = 0;
51
52 // Called before a test is considered for running. If it returns false,
53 // the test is not run. If it returns true, the test will be run provided
54 // it is part of the current shard.
55 virtual bool ShouldRunTest(const std::string& test_case_name,
56 const std::string& test_name) = 0;
57
58 // Called to make the delegate run the specified tests. The delegate must
59 // return the number of actual tests it's going to run (can be smaller,
60 // equal to, or larger than size of |test_names|). It must also call
61 // |test_launcher|'s OnTestFinished method once per every run test,
62 // regardless of its success.
63 virtual size_t RunTests(TestLauncher* test_launcher,
64 const std::vector<std::string>& test_names) = 0;
65
66 // Called to make the delegate retry the specified tests. The delegate must
67 // return the number of actual tests it's going to retry (can be smaller,
68 // equal to, or larger than size of |test_names|). It must also call
69 // |test_launcher|'s OnTestFinished method once per every retried test,
70 // regardless of its success.
71 virtual size_t RetryTests(TestLauncher* test_launcher,
72 const std::vector<std::string>& test_names) = 0;
73
74 protected:
75 virtual ~TestLauncherDelegate();
76};
77
78#if !defined(STARBOARD)
79// An observer of child process lifetime events generated by
80// LaunchChildGTestProcess.
81class ProcessLifetimeObserver {
82 public:
83 virtual ~ProcessLifetimeObserver() = default;
84
85 // Invoked once the child process is started. |handle| is a handle to the
86 // child process and |id| is its pid. NOTE: this method is invoked on the
87 // thread the process is launched on immediately after it is launched. The
88 // caller owns the ProcessHandle.
89 virtual void OnLaunched(ProcessHandle handle, ProcessId id) {}
90
91 // Invoked when a test process exceeds its runtime, immediately before it is
92 // terminated. |command_line| is the command line used to launch the process.
93 // NOTE: this method is invoked on the thread the process is launched on.
94 virtual void OnTimedOut(const CommandLine& command_line) {}
95
96 // Invoked after a child process finishes, reporting the process |exit_code|,
97 // child process |elapsed_time|, whether or not the process was terminated as
98 // a result of a timeout, and the output of the child (stdout and stderr
99 // together). NOTE: this method is invoked on the same thread as
100 // LaunchChildGTestProcess.
101 virtual void OnCompleted(int exit_code,
102 TimeDelta elapsed_time,
103 bool was_timeout,
104 const std::string& output) {}
105
106 protected:
107 ProcessLifetimeObserver() = default;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(ProcessLifetimeObserver);
111};
112#endif // !defined(STARBOARD)
113
114// Launches tests using a TestLauncherDelegate.
115class TestLauncher {
116 public:
117 // Flags controlling behavior of LaunchChildGTestProcess.
118 enum LaunchChildGTestProcessFlags {
119 // Allows usage of job objects on Windows. Helps properly clean up child
120 // processes.
121 USE_JOB_OBJECTS = (1 << 0),
122
123 // Allows breakaway from job on Windows. May result in some child processes
124 // not being properly terminated after launcher dies if these processes
125 // fail to cooperate.
126 ALLOW_BREAKAWAY_FROM_JOB = (1 << 1),
127 };
128
129 struct LaunchOptions {
130 LaunchOptions();
131 LaunchOptions(const LaunchOptions& other);
132 ~LaunchOptions();
133
134 int flags = 0;
135 // These mirror values in base::LaunchOptions, see it for details.
136#if defined(OS_WIN)
137 base::LaunchOptions::Inherit inherit_mode =
138 base::LaunchOptions::Inherit::kSpecific;
139 base::HandlesToInheritVector handles_to_inherit;
140#else
141 FileHandleMappingVector fds_to_remap;
142#endif
143 };
144
145 // Constructor. |parallel_jobs| is the limit of simultaneous parallel test
146 // jobs.
147 TestLauncher(TestLauncherDelegate* launcher_delegate, size_t parallel_jobs);
148 ~TestLauncher();
149
150 // Runs the launcher. Must be called at most once.
151 bool Run() WARN_UNUSED_RESULT;
152
153 // Launches a child process (assumed to be gtest-based binary) using
154 // |command_line|. If |wrapper| is not empty, it is prepended to the final
155 // command line. |observer|, if not null, is used to convey process lifetime
156 // events to the caller. |observer| is destroyed after its OnCompleted
157 // method is invoked.
158 void LaunchChildGTestProcess(
159 const CommandLine& command_line,
160 const std::string& wrapper,
161 TimeDelta timeout,
162 const LaunchOptions& options,
163 std::unique_ptr<ProcessLifetimeObserver> observer);
164
165 // Called when a test has finished running.
166 void OnTestFinished(const TestResult& result);
167
168 private:
169 bool Init() WARN_UNUSED_RESULT;
170
171 // Runs all tests in current iteration.
172 void RunTests();
173
174 void CombinePositiveTestFilters(std::vector<std::string> filter_a,
175 std::vector<std::string> filter_b);
176
177 void RunTestIteration();
178
179#if defined(OS_POSIX)
180 void OnShutdownPipeReadable();
181#endif
182
183 // Saves test results summary as JSON if requested from command line.
184 void MaybeSaveSummaryAsJSON(const std::vector<std::string>& additional_tags);
185
186 // Called when a test iteration is finished.
187 void OnTestIterationFinished();
188
189 // Called by the delay timer when no output was made for a while.
190 void OnOutputTimeout();
191
192 // Make sure we don't accidentally call the wrong methods e.g. on the worker
193 // pool thread. Should be the first member so that it's destroyed last: when
194 // destroying other members, especially the worker pool, we may check the code
195 // is running on the correct thread.
196 ThreadChecker thread_checker_;
197
198 TestLauncherDelegate* launcher_delegate_;
199
200 // Support for outer sharding, just like gtest does.
201 int32_t total_shards_; // Total number of outer shards, at least one.
202 int32_t shard_index_; // Index of shard the launcher is to run.
203
204 int cycles_; // Number of remaining test iterations, or -1 for infinite.
205
206 // Test filters (empty means no filter).
207 bool has_at_least_one_positive_filter_;
208 std::vector<std::string> positive_test_filter_;
209 std::vector<std::string> negative_test_filter_;
210
211 // Tests to use (cached result of TestLauncherDelegate::GetTests).
212 std::vector<TestIdentifier> tests_;
213
214 // Number of tests found in this binary.
215 size_t test_found_count_;
216
217 // Number of tests started in this iteration.
218 size_t test_started_count_;
219
220 // Number of tests finished in this iteration.
221 size_t test_finished_count_;
222
223 // Number of tests successfully finished in this iteration.
224 size_t test_success_count_;
225
226 // Number of tests either timing out or having an unknown result,
227 // likely indicating a more systemic problem if widespread.
228 size_t test_broken_count_;
229
230 // Number of retries in this iteration.
231 size_t retry_count_;
232
233 // Maximum number of retries per iteration.
234 size_t retry_limit_;
235
236 // If true will not early exit nor skip retries even if too many tests are
237 // broken.
238 bool force_run_broken_tests_;
239
240 // Tests to retry in this iteration.
241 std::set<std::string> tests_to_retry_;
242
243 // Result to be returned from Run.
244 bool run_result_;
245
246 // Support for test shuffling, just like gtest does.
247 bool shuffle_;
248 uint32_t shuffle_seed_;
249
250 TestResultsTracker results_tracker_;
251
252 // Watchdog timer to make sure we do not go without output for too long.
253 DelayTimer watchdog_timer_;
254
255 // Number of jobs to run in parallel.
256 size_t parallel_jobs_;
257
258 DISALLOW_COPY_AND_ASSIGN(TestLauncher);
259};
260
261// Return the number of parallel jobs to use, or 0U in case of error.
262size_t NumParallelJobs();
263
264// Extract part from |full_output| that applies to |result|.
265std::string GetTestOutputSnippet(const TestResult& result,
266 const std::string& full_output);
267
268} // namespace base
269
270#endif // !defined(STARBOARD)
271
272#endif // BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_