OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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_MULTIPROCESS_TEST_H_ | 5 #ifndef BASE_TEST_MULTIPROCESS_TEST_H_ |
6 #define BASE_MULTIPROCESS_TEST_H_ | 6 #define BASE_TEST_MULTIPROCESS_TEST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/base_switches.h" | 9 #include <string> |
10 #include "base/command_line.h" | 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/process.h" |
11 #include "base/process_util.h" | 13 #include "base/process_util.h" |
12 #include "base/string_util.h" | 14 #include "build/build_config.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "testing/multiprocess_func_list.h" | |
15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
16 | 16 |
17 #if defined(OS_POSIX) | 17 class CommandLine; |
18 #include <sys/types.h> | |
19 #include <unistd.h> | |
20 #endif | |
21 | 18 |
22 // Command line switch to invoke a child process rather than | 19 namespace base { |
23 // to run the normal test suite. | |
24 static const char kRunClientProcess[] = "client"; | |
25 | 20 |
26 // A MultiProcessTest is a test class which makes it easier to | 21 // A MultiProcessTest is a test class which makes it easier to |
27 // write a test which requires code running out of process. | 22 // write a test which requires code running out of process. |
28 // | 23 // |
29 // To create a multiprocess test simply follow these steps: | 24 // To create a multiprocess test simply follow these steps: |
30 // | 25 // |
31 // 1) Derive your test from MultiProcessTest. Example: | 26 // 1) Derive your test from MultiProcessTest. Example: |
32 // | 27 // |
33 // class MyTest : public MultiProcessTest { | 28 // class MyTest : public MultiProcessTest { |
34 // }; | 29 // }; |
35 // | 30 // |
36 // TEST_F(MyTest, TestCaseName) { | 31 // TEST_F(MyTest, TestCaseName) { |
37 // ... | 32 // ... |
38 // } | 33 // } |
39 // | 34 // |
40 // 2) Create a mainline function for the child processes and include | 35 // 2) Create a mainline function for the child processes and include |
41 // testing/multiprocess_func_list.h. | 36 // testing/multiprocess_func_list.h. |
42 // See the declaration of the MULTIPROCESS_TEST_MAIN macro | 37 // See the declaration of the MULTIPROCESS_TEST_MAIN macro |
43 // in that file for an example. | 38 // in that file for an example. |
44 // 3) Call SpawnChild("foo"), where "foo" is the name of | 39 // 3) Call SpawnChild("foo"), where "foo" is the name of |
45 // the function you wish to run in the child processes. | 40 // the function you wish to run in the child processes. |
46 // That's it! | 41 // That's it! |
47 // | |
48 class MultiProcessTest : public PlatformTest { | 42 class MultiProcessTest : public PlatformTest { |
| 43 public: |
| 44 MultiProcessTest(); |
| 45 |
49 protected: | 46 protected: |
50 // Run a child process. | 47 // Run a child process. |
51 // 'procname' is the name of a function which the child will | 48 // 'procname' is the name of a function which the child will |
52 // execute. It must be exported from this library in order to | 49 // execute. It must be exported from this library in order to |
53 // run. | 50 // run. |
54 // | 51 // |
55 // Example signature: | 52 // Example signature: |
56 // extern "C" int __declspec(dllexport) FooBar() { | 53 // extern "C" int __declspec(dllexport) FooBar() { |
57 // // do client work here | 54 // // do client work here |
58 // } | 55 // } |
59 // | 56 // |
60 // Returns the handle to the child, or NULL on failure | 57 // Returns the handle to the child, or NULL on failure |
61 // | 58 ProcessHandle SpawnChild(const std::string& procname, bool debug_on_start); |
62 // TODO(darin): re-enable this once we have base/debug_util.h | |
63 // ProcessDebugFlags(&cl, DebugUtil::UNKNOWN, false); | |
64 base::ProcessHandle SpawnChild(const std::string& procname) { | |
65 return SpawnChild(procname, false); | |
66 } | |
67 | |
68 base::ProcessHandle SpawnChild(const std::string& procname, | |
69 bool debug_on_start) { | |
70 #if defined(OS_WIN) | |
71 return SpawnChildImpl(procname, debug_on_start); | |
72 #elif defined(OS_POSIX) | |
73 base::file_handle_mapping_vector empty_file_list; | |
74 return SpawnChildImpl(procname, empty_file_list, debug_on_start); | |
75 #endif | |
76 } | |
77 | 59 |
78 #if defined(OS_POSIX) | 60 #if defined(OS_POSIX) |
79 base::ProcessHandle SpawnChild( | 61 ProcessHandle SpawnChild(const std::string& procname, |
80 const std::string& procname, | 62 const file_handle_mapping_vector& fds_to_map, |
81 const base::file_handle_mapping_vector& fds_to_map, | 63 bool debug_on_start); |
82 bool debug_on_start) { | |
83 return SpawnChildImpl(procname, fds_to_map, debug_on_start); | |
84 } | |
85 #endif | 64 #endif |
86 | 65 |
87 protected: | 66 CommandLine MakeCmdLine(const std::string& procname, bool debug_on_start); |
88 CommandLine MakeCmdLine(const std::string& procname, bool debug_on_start) { | |
89 CommandLine cl(*CommandLine::ForCurrentProcess()); | |
90 cl.AppendSwitchASCII(kRunClientProcess, procname); | |
91 if (debug_on_start) | |
92 cl.AppendSwitch(switches::kDebugOnStart); | |
93 return cl; | |
94 } | |
95 | 67 |
96 private: | 68 private: |
97 #if defined(OS_WIN) | 69 #if defined(OS_WIN) |
98 base::ProcessHandle SpawnChildImpl(const std::string& procname, | 70 ProcessHandle SpawnChildImpl(const std::string& procname, |
99 bool debug_on_start) { | 71 bool debug_on_start); |
100 base::ProcessHandle handle = static_cast<base::ProcessHandle>(NULL); | |
101 base::LaunchApp(MakeCmdLine(procname, debug_on_start), | |
102 false, true, &handle); | |
103 return handle; | |
104 } | |
105 | 72 |
106 #elif defined(OS_POSIX) | 73 #elif defined(OS_POSIX) |
107 // TODO(port): with the CommandLine refactoring, this code is very similar | 74 // TODO(port): with the CommandLine refactoring, this code is very similar |
108 // to the Windows code. Investigate whether this can be made shorter. | 75 // to the Windows code. Investigate whether this can be made shorter. |
109 base::ProcessHandle SpawnChildImpl( | 76 ProcessHandle SpawnChildImpl(const std::string& procname, |
110 const std::string& procname, | 77 const file_handle_mapping_vector& fds_to_map, |
111 const base::file_handle_mapping_vector& fds_to_map, | 78 bool debug_on_start); |
112 bool debug_on_start) { | |
113 base::ProcessHandle handle = base::kNullProcessHandle; | |
114 base::LaunchApp(MakeCmdLine(procname, debug_on_start).argv(), | |
115 fds_to_map, false, &handle); | |
116 return handle; | |
117 } | |
118 #endif | 79 #endif |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(MultiProcessTest); |
119 }; | 82 }; |
120 | 83 |
121 #endif // BASE_MULTIPROCESS_TEST_H_ | 84 } // namespace base |
| 85 |
| 86 #endif // BASE_TEST_MULTIPROCESS_TEST_H_ |
OLD | NEW |