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