blob: 88f6b2a86b464ebec437ca47069971b316a2d73c [file] [log] [blame]
thestig@chromium.org33d56572012-02-24 13:40:20 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +09002// 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
17class CommandLine;
18
19namespace 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!
42class 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)
evan@chromium.orgf8da1bb2011-07-14 03:51:47 +090061 // TODO(evan): see if we can delete this via more refactoring.
62 // SpawnChild() should just take a base::LaunchOptions so that we don't
63 // need multiple versions of it.
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090064 ProcessHandle SpawnChild(const std::string& procname,
thestig@chromium.org33d56572012-02-24 13:40:20 +090065 const FileHandleMappingVector& fds_to_map,
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090066 bool debug_on_start);
67#endif
68
evan@chromium.org87b15df2010-10-16 04:51:20 +090069 // Set up the command line used to spawn the child process.
70 virtual CommandLine MakeCmdLine(const std::string& procname,
71 bool debug_on_start);
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090072
73 private:
evan@chromium.orgf8da1bb2011-07-14 03:51:47 +090074 // Shared implementation of SpawnChild.
75 // TODO: |fds_to_map| is unused on Windows; see above TODO about
76 // further refactoring.
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090077 ProcessHandle SpawnChildImpl(const std::string& procname,
thestig@chromium.org33d56572012-02-24 13:40:20 +090078 const FileHandleMappingVector& fds_to_map,
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090079 bool debug_on_start);
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090080
evan@chromium.org87b15df2010-10-16 04:51:20 +090081 DISALLOW_COPY_AND_ASSIGN(MultiProcessTest);
brettw@chromium.org1c8c3be2010-08-18 04:40:11 +090082};
83
84} // namespace base
85
86#endif // BASE_TEST_MULTIPROCESS_TEST_H_