1 // Copyright 2012 The Chromium Authors 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. 14 // 15 // 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 29 namespace multi_process_function_list { 30 31 // Type for child process main functions. 32 typedef int (*TestMainFunctionPtr)(); 33 34 // Type for child setup functions. 35 typedef void (*SetupFunctionPtr)(); 36 37 // Helper class to append a test function to the global mapping. 38 // Used by the MULTIPROCESS_TEST_MAIN macro. 39 class AppendMultiProcessTest { 40 public: 41 // |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); 46 }; 47 48 using ChildProcessTestRunner = int (*)(const std::string&); 49 void SetChildProcessTestRunner(ChildProcessTestRunner runner); 50 51 // Invokes the ChildProcessTestRunner callback with `test_name` if the callback 52 // is not null. Otherwise invokes test associated main function previously 53 // registered with MULTIPROCESS_TEST_MAIN(). 54 int InvokeChildProcessTest(const std::string& test_name); 55 56 // Invokes a the main function of a test previously registered with 57 // MULTIPROCESS_TEST_MAIN(). 58 int InvokeChildProcessTestMain(const std::string& test_name); 59 60 // This macro creates a global MultiProcessTest::AppendMultiProcessTest object 61 // whose constructor does the work of adding the global mapping. 62 #define MULTIPROCESS_TEST_MAIN(test_main) \ 63 MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, NULL) 64 65 // Same as above but lets callers specify a setup method that is run in the 66 // child process, just before the main function is run. This facilitates 67 // adding a generic one-time setup function for multiple tests. 68 #define MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, test_setup) \ 69 int test_main(); \ 70 namespace { \ 71 multi_process_function_list::AppendMultiProcessTest \ 72 AddMultiProcessTest##_##test_main(#test_main, (test_main), (test_setup)); \ 73 } \ 74 int test_main() 75 76 } // namespace multi_process_function_list 77 78 #endif // TESTING_MULTIPROCESS_FUNC_LIST_H_ 79