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_PARALLEL_TEST_LAUNCHER_H_
6 #define BASE_TEST_LAUNCHER_PARALLEL_TEST_LAUNCHER_H_
11 #include "base/callback.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
20 class SequencedWorkerPoolOwner
;
22 // Launches child gtest process in parallel. Keeps track of running processes,
23 // prints a message in case no output is produced for a while.
24 class ParallelTestLauncher
{
26 // Constructor. |jobs| is the maximum number of tests launched in parallel.
27 explicit ParallelTestLauncher(size_t jobs
);
28 ~ParallelTestLauncher();
30 // Callback called after a child process finishes. First argument is the exit
31 // code, second one is child process elapsed time, third one is true if
32 // the child process was terminated because of a timeout, and fourth one
33 // contains output of the child (stdout and stderr together).
34 typedef Callback
<void(int, const TimeDelta
&, bool, const std::string
&)>
35 LaunchChildGTestProcessCallback
;
37 // Launches a child process (assumed to be gtest-based binary) using
38 // |command_line|. If |wrapper| is not empty, it is prepended to the final
39 // command line. If the child process is still running after |timeout|, it
40 // is terminated. After the child process finishes |callback| is called
41 // on the same thread this method was called.
42 void LaunchChildGTestProcess(const CommandLine
& command_line
,
43 const std::string
& wrapper
,
44 base::TimeDelta timeout
,
45 const LaunchChildGTestProcessCallback
& callback
);
47 // Resets the output watchdog, indicating some test results have been printed
48 // out. If a pause between the calls exceeds an internal treshold, a message
49 // will be printed listing all child processes we're still waiting for.
50 void ResetOutputWatchdog();
53 // Called on a worker thread after a child process finishes.
54 void OnLaunchTestProcessFinished(
55 size_t sequence_number
,
56 const LaunchChildGTestProcessCallback
& callback
,
58 const TimeDelta
& elapsed_time
,
60 const std::string
& output
);
62 // Called by the delay timer when no output was made for a while.
63 void OnOutputTimeout();
65 // Make sure we don't accidentally call the wrong methods e.g. on the worker
66 // pool thread. With lots of callbacks used this is non-trivial.
67 // Should be the first member so that it's destroyed last: when destroying
68 // other members, especially the worker pool, we may check the code is running
69 // on the correct thread.
70 ThreadChecker thread_checker_
;
72 // Watchdog timer to make sure we do not go without output for too long.
73 DelayTimer
<ParallelTestLauncher
> timer_
;
75 // Monotonically increasing sequence number to uniquely identify each
76 // launched child process.
77 size_t launch_sequence_number_
;
79 // Map of currently running child processes, keyed by the sequence number.
80 typedef std::map
<size_t, CommandLine
> RunningProcessesMap
;
81 RunningProcessesMap running_processes_map_
;
83 // Worker pool used to launch processes in parallel.
84 scoped_ptr
<SequencedWorkerPoolOwner
> worker_pool_owner_
;
86 DISALLOW_COPY_AND_ASSIGN(ParallelTestLauncher
);
91 #endif // BASE_TEST_LAUNCHER_PARALLEL_TEST_LAUNCHER_H_