blob: f806d53c935b467944ebf216067e3376208eed0d [file] [log] [blame]
jcivelli@chromium.orgc30c76f2012-06-27 10:12:24 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
jeremy@chromium.org012e9992008-10-30 05:46:04 +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 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.
maruel@chromium.orge5743442009-03-05 21:46:38 +090014//
jeremy@chromium.org012e9992008-10-30 05:46:04 +090015// 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.
jcivelli@chromium.orgc30c76f2012-06-27 10:12:24 +090032typedef int (*TestMainFunctionPtr)();
33
34// Type for child setup functions.
35typedef void (*SetupFunctionPtr)();
jeremy@chromium.org012e9992008-10-30 05:46:04 +090036
37// Helper class to append a test function to the global mapping.
maruel@chromium.orge5743442009-03-05 21:46:38 +090038// Used by the MULTIPROCESS_TEST_MAIN macro.
jeremy@chromium.org012e9992008-10-30 05:46:04 +090039class AppendMultiProcessTest {
40 public:
jcivelli@chromium.orgc30c76f2012-06-27 10:12:24 +090041 // |main_func_ptr| is the main function that is run in the child process.
42 // |setup_func_ptr| is a function run when the global mapping is added.
43 AppendMultiProcessTest(std::string test_name,
44 TestMainFunctionPtr main_func_ptr,
45 SetupFunctionPtr setup_func_ptr);
jeremy@chromium.org012e9992008-10-30 05:46:04 +090046};
47
maruel@chromium.orge5743442009-03-05 21:46:38 +090048// Invoke the main function of a test previously registered with
jeremy@chromium.org012e9992008-10-30 05:46:04 +090049// MULTIPROCESS_TEST_MAIN()
50int InvokeChildProcessTest(std::string test_name);
51
52// This macro creates a global MultiProcessTest::AppendMultiProcessTest object
53// whose constructor does the work of adding the global mapping.
54#define MULTIPROCESS_TEST_MAIN(test_main) \
jcivelli@chromium.orgc30c76f2012-06-27 10:12:24 +090055 MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, NULL)
56
57// Same as above but lets callers specify a setup method that is run in the
58// child process, just before the main function is run. This facilitates
59// adding a generic one-time setup function for multiple tests.
60#define MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, test_setup) \
jeremy@chromium.org012e9992008-10-30 05:46:04 +090061 int test_main(); \
62 namespace { \
63 multi_process_function_list::AppendMultiProcessTest \
jcivelli@chromium.orgc30c76f2012-06-27 10:12:24 +090064 AddMultiProcessTest##_##test_main(#test_main, (test_main), (test_setup)); \
jeremy@chromium.org012e9992008-10-30 05:46:04 +090065 } \
66 int test_main()
67
68} // namespace multi_process_function_list
69
70#endif // TESTING_MULTIPROCESS_FUNC_LIST_H_