1 // 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.
5 #ifndef BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
6 #define BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/test/gtest_util.h"
15 #include "base/test/launcher/test_result.h"
16 #include "base/test/launcher/test_results_tracker.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
29 class SequencedWorkerPoolOwner
;
32 // Constants for GTest command-line flags.
33 extern const char kGTestFilterFlag
[];
34 extern const char kGTestHelpFlag
[];
35 extern const char kGTestListTestsFlag
[];
36 extern const char kGTestRepeatFlag
[];
37 extern const char kGTestRunDisabledTestsFlag
[];
38 extern const char kGTestOutputFlag
[];
40 // Interface for use with LaunchTests that abstracts away exact details
41 // which tests and how are run.
42 class TestLauncherDelegate
{
44 // Called to get names of tests available for running. The delegate
45 // must put the result in |output| and return true on success.
46 virtual bool GetTests(std::vector
<TestIdentifier
>* output
) = 0;
48 // Called before a test is considered for running. If it returns false,
49 // the test is not run. If it returns true, the test will be run provided
50 // it is part of the current shard.
51 virtual bool ShouldRunTest(const std::string
& test_case_name
,
52 const std::string
& test_name
) = 0;
54 // Called to make the delegate run the specified tests. The delegate must
55 // return the number of actual tests it's going to run (can be smaller,
56 // equal to, or larger than size of |test_names|). It must also call
57 // |test_launcher|'s OnTestFinished method once per every run test,
58 // regardless of its success.
59 virtual size_t RunTests(TestLauncher
* test_launcher
,
60 const std::vector
<std::string
>& test_names
) = 0;
62 // Called to make the delegate retry the specified tests. The delegate must
63 // return the number of actual tests it's going to retry (can be smaller,
64 // equal to, or larger than size of |test_names|). It must also call
65 // |test_launcher|'s OnTestFinished method once per every retried test,
66 // regardless of its success.
67 virtual size_t RetryTests(TestLauncher
* test_launcher
,
68 const std::vector
<std::string
>& test_names
) = 0;
71 virtual ~TestLauncherDelegate();
74 // Launches tests using a TestLauncherDelegate.
77 // Flags controlling behavior of LaunchChildGTestProcess.
78 enum LaunchChildGTestProcessFlags
{
79 // Allows usage of job objects on Windows. Helps properly clean up child
81 USE_JOB_OBJECTS
= (1 << 0),
83 // Allows breakaway from job on Windows. May result in some child processes
84 // not being properly terminated after launcher dies if these processes
86 ALLOW_BREAKAWAY_FROM_JOB
= (1 << 1),
89 // Constructor. |parallel_jobs| is the limit of simultaneous parallel test
91 TestLauncher(TestLauncherDelegate
* launcher_delegate
, size_t parallel_jobs
);
94 // Runs the launcher. Must be called at most once.
95 bool Run() WARN_UNUSED_RESULT
;
97 // Callback called after a child process finishes. First argument is the exit
98 // code, second one is child process elapsed time, third one is true if
99 // the child process was terminated because of a timeout, and fourth one
100 // contains output of the child (stdout and stderr together).
101 typedef Callback
<void(int, const TimeDelta
&, bool, const std::string
&)>
102 LaunchChildGTestProcessCallback
;
104 // Launches a child process (assumed to be gtest-based binary) using
105 // |command_line|. If |wrapper| is not empty, it is prepended to the final
106 // command line. If the child process is still running after |timeout|, it
107 // is terminated. After the child process finishes |callback| is called
108 // on the same thread this method was called.
109 void LaunchChildGTestProcess(const CommandLine
& command_line
,
110 const std::string
& wrapper
,
111 base::TimeDelta timeout
,
113 const LaunchChildGTestProcessCallback
& callback
);
115 // Called when a test has finished running.
116 void OnTestFinished(const TestResult
& result
);
119 bool Init() WARN_UNUSED_RESULT
;
121 // Runs all tests in current iteration. Uses callbacks to communicate success.
124 void RunTestIteration();
126 // Saves test results summary as JSON if requested from command line.
127 void MaybeSaveSummaryAsJSON();
129 // Called on a worker thread after a child process finishes.
130 void OnLaunchTestProcessFinished(
131 const LaunchChildGTestProcessCallback
& callback
,
133 const TimeDelta
& elapsed_time
,
135 const std::string
& output
);
137 // Called when a test iteration is finished.
138 void OnTestIterationFinished();
140 // Called by the delay timer when no output was made for a while.
141 void OnOutputTimeout();
143 // Make sure we don't accidentally call the wrong methods e.g. on the worker
144 // pool thread. With lots of callbacks used this is non-trivial.
145 // Should be the first member so that it's destroyed last: when destroying
146 // other members, especially the worker pool, we may check the code is running
147 // on the correct thread.
148 ThreadChecker thread_checker_
;
150 TestLauncherDelegate
* launcher_delegate_
;
152 // Support for outer sharding, just like gtest does.
153 int32 total_shards_
; // Total number of outer shards, at least one.
154 int32 shard_index_
; // Index of shard the launcher is to run.
156 int cycles_
; // Number of remaining test itreations, or -1 for infinite.
158 // Test filters (empty means no filter).
159 std::vector
<std::string
> positive_test_filter_
;
160 std::vector
<std::string
> negative_test_filter_
;
162 // Tests to use (cached result of TestLauncherDelegate::GetTests).
163 std::vector
<TestIdentifier
> tests_
;
165 // Number of tests started in this iteration.
166 size_t test_started_count_
;
168 // Number of tests finished in this iteration.
169 size_t test_finished_count_
;
171 // Number of tests successfully finished in this iteration.
172 size_t test_success_count_
;
174 // Number of tests either timing out or having an unknown result,
175 // likely indicating a more systemic problem if widespread.
176 size_t test_broken_count_
;
178 // Number of retries in this iteration.
181 // Maximum number of retries per iteration.
184 // If true will not early exit nor skip retries even if too many tests are
186 bool force_run_broken_tests_
;
188 // Tests to retry in this iteration.
189 std::set
<std::string
> tests_to_retry_
;
191 // Result to be returned from Run.
194 TestResultsTracker results_tracker_
;
196 // Watchdog timer to make sure we do not go without output for too long.
197 DelayTimer
<TestLauncher
> watchdog_timer_
;
199 // Number of jobs to run in parallel.
200 size_t parallel_jobs_
;
202 // Worker pool used to launch processes in parallel.
203 scoped_ptr
<SequencedWorkerPoolOwner
> worker_pool_owner_
;
205 DISALLOW_COPY_AND_ASSIGN(TestLauncher
);
208 // Extract part from |full_output| that applies to |result|.
209 std::string
GetTestOutputSnippet(const TestResult
& result
,
210 const std::string
& full_output
);
214 #endif // BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_