blob: 6ba84954feec0ef31ba6d767e062ae92c4bb2f5f [file] [log] [blame]
[email protected]307f5362008-10-29 20:46:041// Copyright (c) 2008 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 TESTING_MULTIPROCESS_FUNC_LIST_H_
6#define TESTING_MULTIPROCESS_FUNC_LIST_H_
7
8#include <string>
9
10// This file provides the plumbing to register functions to be executed
11// as the main function of a child process in a multi-process test.
12// This complements the MultiProcessTest class which provides facilities
13// for launching such tests.
[email protected]f0a51fb52009-03-05 12:46:3814//
[email protected]307f5362008-10-29 20:46:0415// The MULTIPROCESS_TEST_MAIN() macro registers a string -> func_ptr mapping
16// by creating a new global instance of the AppendMultiProcessTest() class
17// this means that by the time that we reach our main() function the mapping
18// is already in place.
19//
20// Example usage:
21// MULTIPROCESS_TEST_MAIN(a_test_func) {
22// // Code here runs in a child process.
23// return 0;
24// }
25//
26// The prototype of a_test_func is implicitly
27// int test_main_func_name();
28
29namespace multi_process_function_list {
30
31// Type for child process main functions.
32typedef int (*ChildFunctionPtr)();
33
34// Helper class to append a test function to the global mapping.
[email protected]f0a51fb52009-03-05 12:46:3835// Used by the MULTIPROCESS_TEST_MAIN macro.
[email protected]307f5362008-10-29 20:46:0436class AppendMultiProcessTest {
37 public:
38 AppendMultiProcessTest(std::string test_name, ChildFunctionPtr func_ptr);
39};
40
[email protected]f0a51fb52009-03-05 12:46:3841// Invoke the main function of a test previously registered with
[email protected]307f5362008-10-29 20:46:0442// MULTIPROCESS_TEST_MAIN()
43int InvokeChildProcessTest(std::string test_name);
44
45// This macro creates a global MultiProcessTest::AppendMultiProcessTest object
46// whose constructor does the work of adding the global mapping.
47#define MULTIPROCESS_TEST_MAIN(test_main) \
48 int test_main(); \
49 namespace { \
50 multi_process_function_list::AppendMultiProcessTest \
51 AddMultiProcessTest##_##test_main(#test_main, (test_main)); \
52 } \
53 int test_main()
54
55} // namespace multi_process_function_list
56
57#endif // TESTING_MULTIPROCESS_FUNC_LIST_H_