multiprocess_test.h revision 7f92e4ca7bcb209f5d9b38cacaf3a1c6cbe493aa
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
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)
61  ProcessHandle SpawnChild(const std::string& procname,
62                           const file_handle_mapping_vector& fds_to_map,
63                           bool debug_on_start);
64#endif
65
66  CommandLine MakeCmdLine(const std::string& procname, bool debug_on_start);
67
68 private:
69#if defined(OS_WIN)
70  ProcessHandle SpawnChildImpl(const std::string& procname,
71                               bool debug_on_start);
72
73#elif defined(OS_POSIX)
74  // TODO(port): with the CommandLine refactoring, this code is very similar
75  // to the Windows code.  Investigate whether this can be made shorter.
76  ProcessHandle SpawnChildImpl(const std::string& procname,
77                               const file_handle_mapping_vector& fds_to_map,
78                               bool debug_on_start);
79#endif
80
81 DISALLOW_COPY_AND_ASSIGN(MultiProcessTest);
82};
83
84}  // namespace base
85
86#endif  // BASE_TEST_MULTIPROCESS_TEST_H_
87