OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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_MULTIPROCESS_TEST_H_ | |
6 #define BASE_MULTIPROCESS_TEST_H_ | |
7 #pragma once | |
8 | |
9 #include "base/base_switches.h" | |
10 #include "base/command_line.h" | |
11 #include "base/process_util.h" | |
12 #include "base/string_util.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "testing/multiprocess_func_list.h" | |
15 #include "testing/platform_test.h" | |
16 | |
17 #if defined(OS_POSIX) | |
18 #include <sys/types.h> | |
19 #include <unistd.h> | |
20 #endif | |
21 | |
22 // Command line switch to invoke a child process rather than | |
23 // to run the normal test suite. | |
24 static const char kRunClientProcess[] = "client"; | |
25 | |
26 // A MultiProcessTest is a test class which makes it easier to | |
27 // write a test which requires code running out of process. | |
28 // | |
29 // To create a multiprocess test simply follow these steps: | |
30 // | |
31 // 1) Derive your test from MultiProcessTest. Example: | |
32 // | |
33 // class MyTest : public MultiProcessTest { | |
34 // }; | |
35 // | |
36 // TEST_F(MyTest, TestCaseName) { | |
37 // ... | |
38 // } | |
39 // | |
40 // 2) Create a mainline function for the child processes and include | |
41 // testing/multiprocess_func_list.h. | |
42 // See the declaration of the MULTIPROCESS_TEST_MAIN macro | |
43 // in that file for an example. | |
44 // 3) Call SpawnChild("foo"), where "foo" is the name of | |
45 // the function you wish to run in the child processes. | |
46 // That's it! | |
47 // | |
48 class MultiProcessTest : public PlatformTest { | |
49 protected: | |
50 // Run a child process. | |
51 // 'procname' is the name of a function which the child will | |
52 // execute. It must be exported from this library in order to | |
53 // run. | |
54 // | |
55 // Example signature: | |
56 // extern "C" int __declspec(dllexport) FooBar() { | |
57 // // do client work here | |
58 // } | |
59 // | |
60 // Returns the handle to the child, or NULL on failure | |
61 // | |
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 | |
78 #if defined(OS_POSIX) | |
79 base::ProcessHandle SpawnChild( | |
80 const std::string& procname, | |
81 const base::file_handle_mapping_vector& fds_to_map, | |
82 bool debug_on_start) { | |
83 return SpawnChildImpl(procname, fds_to_map, debug_on_start); | |
84 } | |
85 #endif | |
86 | |
87 protected: | |
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 | |
96 private: | |
97 #if defined(OS_WIN) | |
98 base::ProcessHandle SpawnChildImpl(const std::string& procname, | |
99 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 | |
106 #elif defined(OS_POSIX) | |
107 // TODO(port): with the CommandLine refactoring, this code is very similar | |
108 // to the Windows code. Investigate whether this can be made shorter. | |
109 base::ProcessHandle SpawnChildImpl( | |
110 const std::string& procname, | |
111 const base::file_handle_mapping_vector& fds_to_map, | |
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 | |
119 }; | |
120 | |
121 #endif // BASE_MULTIPROCESS_TEST_H_ | |
OLD | NEW |