multiprocess_func_list.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2012 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. #ifndef TESTING_MULTIPROCESS_FUNC_LIST_H_
  5. #define TESTING_MULTIPROCESS_FUNC_LIST_H_
  6. #include <string>
  7. // This file provides the plumbing to register functions to be executed
  8. // as the main function of a child process in a multi-process test.
  9. // This complements the MultiProcessTest class which provides facilities
  10. // for launching such tests.
  11. //
  12. // The MULTIPROCESS_TEST_MAIN() macro registers a string -> func_ptr mapping
  13. // by creating a new global instance of the AppendMultiProcessTest() class
  14. // this means that by the time that we reach our main() function the mapping
  15. // is already in place.
  16. //
  17. // Example usage:
  18. // MULTIPROCESS_TEST_MAIN(a_test_func) {
  19. // // Code here runs in a child process.
  20. // return 0;
  21. // }
  22. //
  23. // The prototype of a_test_func is implicitly
  24. // int test_main_func_name();
  25. namespace multi_process_function_list {
  26. // Type for child process main functions.
  27. typedef int (*TestMainFunctionPtr)();
  28. // Type for child setup functions.
  29. typedef void (*SetupFunctionPtr)();
  30. // Helper class to append a test function to the global mapping.
  31. // Used by the MULTIPROCESS_TEST_MAIN macro.
  32. class AppendMultiProcessTest {
  33. public:
  34. // |main_func_ptr| is the main function that is run in the child process.
  35. // |setup_func_ptr| is a function run when the global mapping is added.
  36. AppendMultiProcessTest(std::string test_name,
  37. TestMainFunctionPtr main_func_ptr,
  38. SetupFunctionPtr setup_func_ptr);
  39. };
  40. // Invoke the main function of a test previously registered with
  41. // MULTIPROCESS_TEST_MAIN()
  42. int InvokeChildProcessTest(const std::string& test_name);
  43. // This macro creates a global MultiProcessTest::AppendMultiProcessTest object
  44. // whose constructor does the work of adding the global mapping.
  45. #define MULTIPROCESS_TEST_MAIN(test_main) \
  46. MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, NULL)
  47. // Same as above but lets callers specify a setup method that is run in the
  48. // child process, just before the main function is run. This facilitates
  49. // adding a generic one-time setup function for multiple tests.
  50. #define MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, test_setup) \
  51. int test_main(); \
  52. namespace { \
  53. multi_process_function_list::AppendMultiProcessTest \
  54. AddMultiProcessTest##_##test_main(#test_main, (test_main), (test_setup)); \
  55. } \
  56. int test_main()
  57. } // namespace multi_process_function_list
  58. #endif // TESTING_MULTIPROCESS_FUNC_LIST_H_