| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TEST_MULTIPROCESS_TEST_H_ | 5 #ifndef BASE_TEST_MULTIPROCESS_TEST_H_ |
| 6 #define BASE_TEST_MULTIPROCESS_TEST_H_ | 6 #define BASE_TEST_MULTIPROCESS_TEST_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/process/launch.h" | 11 #include "base/process/launch.h" |
| 12 #include "base/process/process.h" | 12 #include "base/process/process.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 | 17 |
| 18 class CommandLine; | 18 class CommandLine; |
| 19 | 19 |
| 20 struct SpawnChildResult { |
| 21 SpawnChildResult() {} |
| 22 SpawnChildResult(SpawnChildResult&& other) = default; |
| 23 |
| 24 SpawnChildResult& operator=(SpawnChildResult&& other) = default; |
| 25 |
| 26 Process process; |
| 27 |
| 28 DISALLOW_COPY_AND_ASSIGN(SpawnChildResult); |
| 29 }; |
| 30 |
| 20 // Helpers to spawn a child for a multiprocess test and execute a designated | 31 // Helpers to spawn a child for a multiprocess test and execute a designated |
| 21 // function. Use these when you already have another base class for your test | 32 // function. Use these when you already have another base class for your test |
| 22 // fixture, but you want (some) of your tests to be multiprocess (otherwise you | 33 // fixture, but you want (some) of your tests to be multiprocess (otherwise you |
| 23 // may just want to derive your fixture from |MultiProcessTest|, below). | 34 // may just want to derive your fixture from |MultiProcessTest|, below). |
| 24 // | 35 // |
| 25 // Use these helpers as follows: | 36 // Use these helpers as follows: |
| 26 // | 37 // |
| 27 // TEST_F(MyTest, ATest) { | 38 // TEST_F(MyTest, ATest) { |
| 28 // CommandLine command_line( | 39 // CommandLine command_line( |
| 29 // base::GetMultiProcessTestChildBaseCommandLine()); | 40 // base::GetMultiProcessTestChildBaseCommandLine()); |
| 30 // // Maybe add our own switches to |command_line|.... | 41 // // Maybe add our own switches to |command_line|.... |
| 31 // | 42 // |
| 32 // LaunchOptions options; | 43 // LaunchOptions options; |
| 33 // // Maybe set some options (e.g., |start_hidden| on Windows).... | 44 // // Maybe set some options (e.g., |start_hidden| on Windows).... |
| 34 // | 45 // |
| 35 // // Start a child process and run |a_test_func|. | 46 // // Start a child process and run |a_test_func|. |
| 36 // base::Process test_child_process = | 47 // SpawnChildResult result = |
| 37 // base::SpawnMultiProcessTestChild("a_test_func", command_line, | 48 // base::SpawnMultiProcessTestChild("a_test_func", command_line, |
| 38 // options); | 49 // options); |
| 50 // base::Process test_child_process = std::move(result.process); |
| 39 // | 51 // |
| 40 // // Do stuff involving |test_child_process| and the child process.... | 52 // // Do stuff involving |test_child_process| and the child process.... |
| 41 // | 53 // |
| 42 // int rv = -1; | 54 // int rv = -1; |
| 43 // ASSERT_TRUE(base::WaitForMultiprocessTestChildExit(test_child_process, | 55 // ASSERT_TRUE(base::WaitForMultiprocessTestChildExit(test_child_process, |
| 44 // TestTimeouts::action_timeout(), &rv)); | 56 // TestTimeouts::action_timeout(), &rv)); |
| 45 // EXPECT_EQ(0, rv); | 57 // EXPECT_EQ(0, rv); |
| 46 // } | 58 // } |
| 47 // | 59 // |
| 48 // // Note: |MULTIPROCESS_TEST_MAIN()| is defined in | 60 // // Note: |MULTIPROCESS_TEST_MAIN()| is defined in |
| 49 // // testing/multi_process_function_list.h. | 61 // // testing/multi_process_function_list.h. |
| 50 // MULTIPROCESS_TEST_MAIN(a_test_func) { | 62 // MULTIPROCESS_TEST_MAIN(a_test_func) { |
| 51 // // Code here runs in a child process.... | 63 // // Code here runs in a child process.... |
| 52 // return 0; | 64 // return 0; |
| 53 // } | 65 // } |
| 54 // | 66 // |
| 55 // If you need to terminate the child process, use the | 67 // If you need to terminate the child process, use the |
| 56 // TerminateMultiProcessTestChild method to ensure that test will work on | 68 // TerminateMultiProcessTestChild method to ensure that test will work on |
| 57 // Android. | 69 // Android. |
| 58 | 70 |
| 59 // Spawns a child process and executes the function |procname| declared using | 71 // Spawns a child process and executes the function |procname| declared using |
| 60 // |MULTIPROCESS_TEST_MAIN()| or |MULTIPROCESS_TEST_MAIN_WITH_SETUP()|. | 72 // |MULTIPROCESS_TEST_MAIN()| or |MULTIPROCESS_TEST_MAIN_WITH_SETUP()|. |
| 61 // |command_line| should be as provided by | 73 // |command_line| should be as provided by |
| 62 // |GetMultiProcessTestChildBaseCommandLine()| (below), possibly with arguments | 74 // |GetMultiProcessTestChildBaseCommandLine()| (below), possibly with arguments |
| 63 // added. Note: On Windows, you probably want to set |options.start_hidden|. | 75 // added. Note: On Windows, you probably want to set |options.start_hidden|. |
| 64 Process SpawnMultiProcessTestChild( | 76 SpawnChildResult SpawnMultiProcessTestChild(const std::string& procname, |
| 65 const std::string& procname, | 77 const CommandLine& command_line, |
| 66 const CommandLine& command_line, | 78 const LaunchOptions& options); |
| 67 const LaunchOptions& options); | |
| 68 | 79 |
| 69 // Gets the base command line for |SpawnMultiProcessTestChild()|. To this, you | 80 // Gets the base command line for |SpawnMultiProcessTestChild()|. To this, you |
| 70 // may add any flags needed for your child process. | 81 // may add any flags needed for your child process. |
| 71 CommandLine GetMultiProcessTestChildBaseCommandLine(); | 82 CommandLine GetMultiProcessTestChildBaseCommandLine(); |
| 72 | 83 |
| 73 // Waits for the child process to exit. Returns true if the process exited | 84 // Waits for the child process to exit. Returns true if the process exited |
| 74 // within |timeout| and sets |exit_code| if non null. | 85 // within |timeout| and sets |exit_code| if non null. |
| 75 bool WaitForMultiprocessTestChildExit(const Process& process, | 86 bool WaitForMultiprocessTestChildExit(const Process& process, |
| 76 TimeDelta timeout, | 87 TimeDelta timeout, |
| 77 int* exit_code); | 88 int* exit_code); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // 'procname' is the name of a function which the child will | 125 // 'procname' is the name of a function which the child will |
| 115 // execute. It must be exported from this library in order to | 126 // execute. It must be exported from this library in order to |
| 116 // run. | 127 // run. |
| 117 // | 128 // |
| 118 // Example signature: | 129 // Example signature: |
| 119 // extern "C" int __declspec(dllexport) FooBar() { | 130 // extern "C" int __declspec(dllexport) FooBar() { |
| 120 // // do client work here | 131 // // do client work here |
| 121 // } | 132 // } |
| 122 // | 133 // |
| 123 // Returns the child process. | 134 // Returns the child process. |
| 124 Process SpawnChild(const std::string& procname); | 135 SpawnChildResult SpawnChild(const std::string& procname); |
| 125 | 136 |
| 126 // Run a child process using the given launch options. | 137 // Run a child process using the given launch options. |
| 127 // | 138 // |
| 128 // Note: On Windows, you probably want to set |options.start_hidden|. | 139 // Note: On Windows, you probably want to set |options.start_hidden|. |
| 129 Process SpawnChildWithOptions(const std::string& procname, | 140 SpawnChildResult SpawnChildWithOptions(const std::string& procname, |
| 130 const LaunchOptions& options); | 141 const LaunchOptions& options); |
| 131 | 142 |
| 132 // Set up the command line used to spawn the child process. | 143 // Set up the command line used to spawn the child process. |
| 133 // Override this to add things to the command line (calling this first in the | 144 // Override this to add things to the command line (calling this first in the |
| 134 // override). | 145 // override). |
| 135 // Note that currently some tests rely on this providing a full command line, | 146 // Note that currently some tests rely on this providing a full command line, |
| 136 // which they then use directly with |LaunchProcess()|. | 147 // which they then use directly with |LaunchProcess()|. |
| 137 // TODO(viettrungluu): Remove this and add a virtual | 148 // TODO(viettrungluu): Remove this and add a virtual |
| 138 // |ModifyChildCommandLine()|; make the two divergent uses more sane. | 149 // |ModifyChildCommandLine()|; make the two divergent uses more sane. |
| 139 virtual CommandLine MakeCmdLine(const std::string& procname); | 150 virtual CommandLine MakeCmdLine(const std::string& procname); |
| 140 | 151 |
| 141 private: | 152 private: |
| 142 DISALLOW_COPY_AND_ASSIGN(MultiProcessTest); | 153 DISALLOW_COPY_AND_ASSIGN(MultiProcessTest); |
| 143 }; | 154 }; |
| 144 | 155 |
| 145 } // namespace base | 156 } // namespace base |
| 146 | 157 |
| 147 #endif // BASE_TEST_MULTIPROCESS_TEST_H_ | 158 #endif // BASE_TEST_MULTIPROCESS_TEST_H_ |
| OLD | NEW |