brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 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. |
| 4 | |
| 5 | #ifndef BASE_TEST_MULTIPROCESS_TEST_H_ |
| 6 | #define BASE_TEST_MULTIPROCESS_TEST_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "base/process.h" |
| 13 | #include "base/process_util.h" |
| 14 | #include "build/build_config.h" |
| 15 | #include "testing/platform_test.h" |
| 16 | |
| 17 | class CommandLine; |
| 18 | |
| 19 | namespace base { |
| 20 | |
| 21 | // A MultiProcessTest is a test class which makes it easier to |
| 22 | // write a test which requires code running out of process. |
| 23 | // |
| 24 | // To create a multiprocess test simply follow these steps: |
| 25 | // |
| 26 | // 1) Derive your test from MultiProcessTest. Example: |
| 27 | // |
| 28 | // class MyTest : public MultiProcessTest { |
| 29 | // }; |
| 30 | // |
| 31 | // TEST_F(MyTest, TestCaseName) { |
| 32 | // ... |
| 33 | // } |
| 34 | // |
| 35 | // 2) Create a mainline function for the child processes and include |
| 36 | // testing/multiprocess_func_list.h. |
| 37 | // See the declaration of the MULTIPROCESS_TEST_MAIN macro |
| 38 | // in that file for an example. |
| 39 | // 3) Call SpawnChild("foo"), where "foo" is the name of |
| 40 | // the function you wish to run in the child processes. |
| 41 | // That's it! |
| 42 | class MultiProcessTest : public PlatformTest { |
| 43 | public: |
| 44 | MultiProcessTest(); |
| 45 | |
| 46 | protected: |
| 47 | // Run a child process. |
| 48 | // 'procname' is the name of a function which the child will |
| 49 | // execute. It must be exported from this library in order to |
| 50 | // run. |
| 51 | // |
| 52 | // Example signature: |
| 53 | // extern "C" int __declspec(dllexport) FooBar() { |
| 54 | // // do client work here |
| 55 | // } |
| 56 | // |
| 57 | // Returns the handle to the child, or NULL on failure |
| 58 | ProcessHandle SpawnChild(const std::string& procname, bool debug_on_start); |
| 59 | |
| 60 | #if defined(OS_POSIX) |
| 61 | ProcessHandle SpawnChild(const std::string& procname, |
| 62 | const file_handle_mapping_vector& fds_to_map, |
| 63 | bool debug_on_start); |
| 64 | #endif |
| 65 | |
evan@chromium.org | 87b15df | 2010-10-16 04:51:20 +0900 | [diff] [blame^] | 66 | // Set up the command line used to spawn the child process. |
| 67 | virtual CommandLine MakeCmdLine(const std::string& procname, |
| 68 | bool debug_on_start); |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | #if defined(OS_WIN) |
| 72 | ProcessHandle SpawnChildImpl(const std::string& procname, |
| 73 | bool debug_on_start); |
| 74 | |
| 75 | #elif defined(OS_POSIX) |
| 76 | // TODO(port): with the CommandLine refactoring, this code is very similar |
| 77 | // to the Windows code. Investigate whether this can be made shorter. |
| 78 | ProcessHandle SpawnChildImpl(const std::string& procname, |
| 79 | const file_handle_mapping_vector& fds_to_map, |
| 80 | bool debug_on_start); |
| 81 | #endif |
| 82 | |
evan@chromium.org | 87b15df | 2010-10-16 04:51:20 +0900 | [diff] [blame^] | 83 | DISALLOW_COPY_AND_ASSIGN(MultiProcessTest); |
brettw@chromium.org | 1c8c3be | 2010-08-18 04:40:11 +0900 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace base |
| 87 | |
| 88 | #endif // BASE_TEST_MULTIPROCESS_TEST_H_ |