thestig@chromium.org | 33d5657 | 2012-02-24 13:40:20 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_TEST_MULTIPROCESS_TEST_H_ |
| 6 | #define BASE_TEST_MULTIPROCESS_TEST_H_ |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/basictypes.h" |
rsesek@chromium.org | 687756f | 2013-07-26 06:38:23 +0900 | [diff] [blame] | 11 | #include "base/process/launch.h" |
| 12 | #include "base/process/process_handle.h" |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 13 | #include "build/build_config.h" |
| 14 | #include "testing/platform_test.h" |
| 15 | |
| 16 | class CommandLine; |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | // A MultiProcessTest is a test class which makes it easier to |
| 21 | // write a test which requires code running out of process. |
| 22 | // |
| 23 | // To create a multiprocess test simply follow these steps: |
| 24 | // |
| 25 | // 1) Derive your test from MultiProcessTest. Example: |
| 26 | // |
| 27 | // class MyTest : public MultiProcessTest { |
| 28 | // }; |
| 29 | // |
| 30 | // TEST_F(MyTest, TestCaseName) { |
| 31 | // ... |
| 32 | // } |
| 33 | // |
| 34 | // 2) Create a mainline function for the child processes and include |
| 35 | // testing/multiprocess_func_list.h. |
| 36 | // See the declaration of the MULTIPROCESS_TEST_MAIN macro |
| 37 | // in that file for an example. |
| 38 | // 3) Call SpawnChild("foo"), where "foo" is the name of |
| 39 | // the function you wish to run in the child processes. |
| 40 | // That's it! |
| 41 | class MultiProcessTest : public PlatformTest { |
| 42 | public: |
| 43 | MultiProcessTest(); |
| 44 | |
| 45 | protected: |
| 46 | // Run a child process. |
| 47 | // 'procname' is the name of a function which the child will |
| 48 | // execute. It must be exported from this library in order to |
| 49 | // run. |
| 50 | // |
| 51 | // Example signature: |
| 52 | // extern "C" int __declspec(dllexport) FooBar() { |
| 53 | // // do client work here |
| 54 | // } |
| 55 | // |
| 56 | // Returns the handle to the child, or NULL on failure |
| 57 | ProcessHandle SpawnChild(const std::string& procname, bool debug_on_start); |
| 58 | |
viettrungluu@chromium.org | b1b55ff | 2013-12-07 06:23:35 +0900 | [diff] [blame^] | 59 | // Run a child process using the given launch options. |
| 60 | // |
| 61 | // Note: On Windows, you probably want to set |options.start_hidden|. |
| 62 | ProcessHandle SpawnChildWithOptions(const std::string& procname, |
| 63 | const LaunchOptions& options, |
| 64 | bool debug_on_start); |
| 65 | |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 66 | #if defined(OS_POSIX) |
viettrungluu@chromium.org | b1b55ff | 2013-12-07 06:23:35 +0900 | [diff] [blame^] | 67 | // TODO(vtl): Remove this in favor of |SpawnChildWithOptions()|. Probably keep |
| 68 | // the no-options |SpawnChild()| around for ease-of-use. |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 69 | ProcessHandle SpawnChild(const std::string& procname, |
thestig@chromium.org | 33d5657 | 2012-02-24 13:40:20 +0900 | [diff] [blame] | 70 | const FileHandleMappingVector& fds_to_map, |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 71 | bool debug_on_start); |
| 72 | #endif |
| 73 | |
evan@chromium.org | 87b15df | 2010-10-16 04:51:20 +0900 | [diff] [blame] | 74 | // Set up the command line used to spawn the child process. |
| 75 | virtual CommandLine MakeCmdLine(const std::string& procname, |
| 76 | bool debug_on_start); |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 77 | |
| 78 | private: |
evan@chromium.org | 87b15df | 2010-10-16 04:51:20 +0900 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(MultiProcessTest); |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace base |
| 83 | |
| 84 | #endif // BASE_TEST_MULTIPROCESS_TEST_H_ |