xref: /aosp_15_r20/external/libchrome/base/test/multiprocess_test.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TEST_MULTIPROCESS_TEST_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_TEST_MULTIPROCESS_TEST_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <string>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/process/launch.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h"
13*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
14*635a8641SAndroid Build Coastguard Worker #include "testing/platform_test.h"
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker namespace base {
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker class CommandLine;
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker // Helpers to spawn a child for a multiprocess test and execute a designated
21*635a8641SAndroid Build Coastguard Worker // function. Use these when you already have another base class for your test
22*635a8641SAndroid Build Coastguard Worker // fixture, but you want (some) of your tests to be multiprocess (otherwise you
23*635a8641SAndroid Build Coastguard Worker // may just want to derive your fixture from |MultiProcessTest|, below).
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // Use these helpers as follows:
26*635a8641SAndroid Build Coastguard Worker //
27*635a8641SAndroid Build Coastguard Worker //   TEST_F(MyTest, ATest) {
28*635a8641SAndroid Build Coastguard Worker //     CommandLine command_line(
29*635a8641SAndroid Build Coastguard Worker //         base::GetMultiProcessTestChildBaseCommandLine());
30*635a8641SAndroid Build Coastguard Worker //     // Maybe add our own switches to |command_line|....
31*635a8641SAndroid Build Coastguard Worker //
32*635a8641SAndroid Build Coastguard Worker //     LaunchOptions options;
33*635a8641SAndroid Build Coastguard Worker //     // Maybe set some options (e.g., |start_hidden| on Windows)....
34*635a8641SAndroid Build Coastguard Worker //
35*635a8641SAndroid Build Coastguard Worker //     // Start a child process and run |a_test_func|.
36*635a8641SAndroid Build Coastguard Worker //     base::Process test_child_process =
37*635a8641SAndroid Build Coastguard Worker //         base::SpawnMultiProcessTestChild("a_test_func", command_line,
38*635a8641SAndroid Build Coastguard Worker //                                          options);
39*635a8641SAndroid Build Coastguard Worker //
40*635a8641SAndroid Build Coastguard Worker //     // Do stuff involving |test_child_process| and the child process....
41*635a8641SAndroid Build Coastguard Worker //
42*635a8641SAndroid Build Coastguard Worker //     int rv = -1;
43*635a8641SAndroid Build Coastguard Worker //     ASSERT_TRUE(base::WaitForMultiprocessTestChildExit(test_child_process,
44*635a8641SAndroid Build Coastguard Worker //         TestTimeouts::action_timeout(), &rv));
45*635a8641SAndroid Build Coastguard Worker //     EXPECT_EQ(0, rv);
46*635a8641SAndroid Build Coastguard Worker //   }
47*635a8641SAndroid Build Coastguard Worker //
48*635a8641SAndroid Build Coastguard Worker //   // Note: |MULTIPROCESS_TEST_MAIN()| is defined in
49*635a8641SAndroid Build Coastguard Worker //   // testing/multi_process_function_list.h.
50*635a8641SAndroid Build Coastguard Worker //   MULTIPROCESS_TEST_MAIN(a_test_func) {
51*635a8641SAndroid Build Coastguard Worker //     // Code here runs in a child process....
52*635a8641SAndroid Build Coastguard Worker //     return 0;
53*635a8641SAndroid Build Coastguard Worker //   }
54*635a8641SAndroid Build Coastguard Worker //
55*635a8641SAndroid Build Coastguard Worker // If you need to terminate the child process, use the
56*635a8641SAndroid Build Coastguard Worker // TerminateMultiProcessTestChild method to ensure that test will work on
57*635a8641SAndroid Build Coastguard Worker // Android.
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker // Spawns a child process and executes the function |procname| declared using
60*635a8641SAndroid Build Coastguard Worker // |MULTIPROCESS_TEST_MAIN()| or |MULTIPROCESS_TEST_MAIN_WITH_SETUP()|.
61*635a8641SAndroid Build Coastguard Worker // |command_line| should be as provided by
62*635a8641SAndroid Build Coastguard Worker // |GetMultiProcessTestChildBaseCommandLine()| (below), possibly with arguments
63*635a8641SAndroid Build Coastguard Worker // added. Note: On Windows, you probably want to set |options.start_hidden|.
64*635a8641SAndroid Build Coastguard Worker Process SpawnMultiProcessTestChild(const std::string& procname,
65*635a8641SAndroid Build Coastguard Worker                                    const CommandLine& command_line,
66*635a8641SAndroid Build Coastguard Worker                                    const LaunchOptions& options);
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker // Gets the base command line for |SpawnMultiProcessTestChild()|. To this, you
69*635a8641SAndroid Build Coastguard Worker // may add any flags needed for your child process.
70*635a8641SAndroid Build Coastguard Worker CommandLine GetMultiProcessTestChildBaseCommandLine();
71*635a8641SAndroid Build Coastguard Worker 
72*635a8641SAndroid Build Coastguard Worker // Waits for the child process to exit. Returns true if the process exited
73*635a8641SAndroid Build Coastguard Worker // within |timeout| and sets |exit_code| if non null.
74*635a8641SAndroid Build Coastguard Worker bool WaitForMultiprocessTestChildExit(const Process& process,
75*635a8641SAndroid Build Coastguard Worker                                       TimeDelta timeout,
76*635a8641SAndroid Build Coastguard Worker                                       int* exit_code);
77*635a8641SAndroid Build Coastguard Worker 
78*635a8641SAndroid Build Coastguard Worker // Terminates |process| with |exit_code|. If |wait| is true, this call blocks
79*635a8641SAndroid Build Coastguard Worker // until the process actually terminates.
80*635a8641SAndroid Build Coastguard Worker bool TerminateMultiProcessTestChild(const Process& process,
81*635a8641SAndroid Build Coastguard Worker                                     int exit_code,
82*635a8641SAndroid Build Coastguard Worker                                     bool wait);
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker // MultiProcessTest ------------------------------------------------------------
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker // A MultiProcessTest is a test class which makes it easier to
87*635a8641SAndroid Build Coastguard Worker // write a test which requires code running out of process.
88*635a8641SAndroid Build Coastguard Worker //
89*635a8641SAndroid Build Coastguard Worker // To create a multiprocess test simply follow these steps:
90*635a8641SAndroid Build Coastguard Worker //
91*635a8641SAndroid Build Coastguard Worker // 1) Derive your test from MultiProcessTest. Example:
92*635a8641SAndroid Build Coastguard Worker //
93*635a8641SAndroid Build Coastguard Worker //    class MyTest : public MultiProcessTest {
94*635a8641SAndroid Build Coastguard Worker //    };
95*635a8641SAndroid Build Coastguard Worker //
96*635a8641SAndroid Build Coastguard Worker //    TEST_F(MyTest, TestCaseName) {
97*635a8641SAndroid Build Coastguard Worker //      ...
98*635a8641SAndroid Build Coastguard Worker //    }
99*635a8641SAndroid Build Coastguard Worker //
100*635a8641SAndroid Build Coastguard Worker // 2) Create a mainline function for the child processes and include
101*635a8641SAndroid Build Coastguard Worker //    testing/multiprocess_func_list.h.
102*635a8641SAndroid Build Coastguard Worker //    See the declaration of the MULTIPROCESS_TEST_MAIN macro
103*635a8641SAndroid Build Coastguard Worker //    in that file for an example.
104*635a8641SAndroid Build Coastguard Worker // 3) Call SpawnChild("foo"), where "foo" is the name of
105*635a8641SAndroid Build Coastguard Worker //    the function you wish to run in the child processes.
106*635a8641SAndroid Build Coastguard Worker // That's it!
107*635a8641SAndroid Build Coastguard Worker class MultiProcessTest : public PlatformTest {
108*635a8641SAndroid Build Coastguard Worker  public:
109*635a8641SAndroid Build Coastguard Worker   MultiProcessTest();
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker  protected:
112*635a8641SAndroid Build Coastguard Worker   // Run a child process.
113*635a8641SAndroid Build Coastguard Worker   // 'procname' is the name of a function which the child will
114*635a8641SAndroid Build Coastguard Worker   // execute.  It must be exported from this library in order to
115*635a8641SAndroid Build Coastguard Worker   // run.
116*635a8641SAndroid Build Coastguard Worker   //
117*635a8641SAndroid Build Coastguard Worker   // Example signature:
118*635a8641SAndroid Build Coastguard Worker   //    extern "C" int __declspec(dllexport) FooBar() {
119*635a8641SAndroid Build Coastguard Worker   //         // do client work here
120*635a8641SAndroid Build Coastguard Worker   //    }
121*635a8641SAndroid Build Coastguard Worker   //
122*635a8641SAndroid Build Coastguard Worker   // Returns the child process.
123*635a8641SAndroid Build Coastguard Worker   Process SpawnChild(const std::string& procname);
124*635a8641SAndroid Build Coastguard Worker 
125*635a8641SAndroid Build Coastguard Worker   // Run a child process using the given launch options.
126*635a8641SAndroid Build Coastguard Worker   //
127*635a8641SAndroid Build Coastguard Worker   // Note: On Windows, you probably want to set |options.start_hidden|.
128*635a8641SAndroid Build Coastguard Worker   Process SpawnChildWithOptions(const std::string& procname,
129*635a8641SAndroid Build Coastguard Worker                                 const LaunchOptions& options);
130*635a8641SAndroid Build Coastguard Worker 
131*635a8641SAndroid Build Coastguard Worker   // Set up the command line used to spawn the child process.
132*635a8641SAndroid Build Coastguard Worker   // Override this to add things to the command line (calling this first in the
133*635a8641SAndroid Build Coastguard Worker   // override).
134*635a8641SAndroid Build Coastguard Worker   // Note that currently some tests rely on this providing a full command line,
135*635a8641SAndroid Build Coastguard Worker   // which they then use directly with |LaunchProcess()|.
136*635a8641SAndroid Build Coastguard Worker   // TODO(viettrungluu): Remove this and add a virtual
137*635a8641SAndroid Build Coastguard Worker   // |ModifyChildCommandLine()|; make the two divergent uses more sane.
138*635a8641SAndroid Build Coastguard Worker   virtual CommandLine MakeCmdLine(const std::string& procname);
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker  private:
141*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(MultiProcessTest);
142*635a8641SAndroid Build Coastguard Worker };
143*635a8641SAndroid Build Coastguard Worker 
144*635a8641SAndroid Build Coastguard Worker }  // namespace base
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker #endif  // BASE_TEST_MULTIPROCESS_TEST_H_
147