multiprocess_test.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 BASE_TEST_MULTIPROCESS_TEST_H_
  5. #define BASE_TEST_MULTIPROCESS_TEST_H_
  6. #include <string>
  7. #include "base/macros.h"
  8. #include "base/process/launch.h"
  9. #include "base/process/process.h"
  10. #include "build/build_config.h"
  11. #include "testing/platform_test.h"
  12. namespace base {
  13. class CommandLine;
  14. // Helpers to spawn a child for a multiprocess test and execute a designated
  15. // function. Use these when you already have another base class for your test
  16. // fixture, but you want (some) of your tests to be multiprocess (otherwise you
  17. // may just want to derive your fixture from |MultiProcessTest|, below).
  18. //
  19. // Use these helpers as follows:
  20. //
  21. // TEST_F(MyTest, ATest) {
  22. // CommandLine command_line(
  23. // base::GetMultiProcessTestChildBaseCommandLine());
  24. // // Maybe add our own switches to |command_line|....
  25. //
  26. // LaunchOptions options;
  27. // // Maybe set some options (e.g., |start_hidden| on Windows)....
  28. //
  29. // // Start a child process and run |a_test_func|.
  30. // base::Process test_child_process =
  31. // base::SpawnMultiProcessTestChild("a_test_func", command_line,
  32. // options);
  33. //
  34. // // Do stuff involving |test_child_process| and the child process....
  35. //
  36. // int rv = -1;
  37. // ASSERT_TRUE(base::WaitForMultiprocessTestChildExit(test_child_process,
  38. // TestTimeouts::action_timeout(), &rv));
  39. // EXPECT_EQ(0, rv);
  40. // }
  41. //
  42. // // Note: |MULTIPROCESS_TEST_MAIN()| is defined in
  43. // // testing/multiprocess_func_list.h.
  44. // MULTIPROCESS_TEST_MAIN(a_test_func) {
  45. // // Code here runs in a child process....
  46. // return 0;
  47. // }
  48. //
  49. // If you need to terminate the child process, use the
  50. // TerminateMultiProcessTestChild method to ensure that test will work on
  51. // Android.
  52. // Spawns a child process and executes the function |procname| declared using
  53. // |MULTIPROCESS_TEST_MAIN()| or |MULTIPROCESS_TEST_MAIN_WITH_SETUP()|.
  54. // |command_line| should be as provided by
  55. // |GetMultiProcessTestChildBaseCommandLine()| (below), possibly with arguments
  56. // added. Note: On Windows, you probably want to set |options.start_hidden|.
  57. Process SpawnMultiProcessTestChild(const std::string& procname,
  58. const CommandLine& command_line,
  59. const LaunchOptions& options);
  60. // Gets the base command line for |SpawnMultiProcessTestChild()|. To this, you
  61. // may add any flags needed for your child process.
  62. CommandLine GetMultiProcessTestChildBaseCommandLine();
  63. // Waits for the child process to exit. Returns true if the process exited
  64. // within |timeout| and sets |exit_code| if non null.
  65. bool WaitForMultiprocessTestChildExit(const Process& process,
  66. TimeDelta timeout,
  67. int* exit_code);
  68. // Terminates |process| with |exit_code|. If |wait| is true, this call blocks
  69. // until the process actually terminates.
  70. bool TerminateMultiProcessTestChild(const Process& process,
  71. int exit_code,
  72. bool wait);
  73. #if defined(OS_ANDROID)
  74. // Returns whether the child process exited cleanly from the main runloop.
  75. bool MultiProcessTestChildHasCleanExit(const Process& process);
  76. #endif
  77. // MultiProcessTest ------------------------------------------------------------
  78. // A MultiProcessTest is a test class which makes it easier to
  79. // write a test which requires code running out of process.
  80. //
  81. // To create a multiprocess test simply follow these steps:
  82. //
  83. // 1) Derive your test from MultiProcessTest. Example:
  84. //
  85. // class MyTest : public MultiProcessTest {
  86. // };
  87. //
  88. // TEST_F(MyTest, TestCaseName) {
  89. // ...
  90. // }
  91. //
  92. // 2) Create a mainline function for the child processes and include
  93. // testing/multiprocess_func_list.h.
  94. // See the declaration of the MULTIPROCESS_TEST_MAIN macro
  95. // in that file for an example.
  96. // 3) Call SpawnChild("foo"), where "foo" is the name of
  97. // the function you wish to run in the child processes.
  98. // That's it!
  99. class MultiProcessTest : public PlatformTest {
  100. public:
  101. MultiProcessTest();
  102. protected:
  103. // Run a child process.
  104. // 'procname' is the name of a function which the child will
  105. // execute. It must be exported from this library in order to
  106. // run.
  107. //
  108. // Example signature:
  109. // extern "C" int __declspec(dllexport) FooBar() {
  110. // // do client work here
  111. // }
  112. //
  113. // Returns the child process.
  114. Process SpawnChild(const std::string& procname);
  115. // Run a child process using the given launch options.
  116. //
  117. // Note: On Windows, you probably want to set |options.start_hidden|.
  118. Process SpawnChildWithOptions(const std::string& procname,
  119. const LaunchOptions& options);
  120. // Set up the command line used to spawn the child process.
  121. // Override this to add things to the command line (calling this first in the
  122. // override).
  123. // Note that currently some tests rely on this providing a full command line,
  124. // which they then use directly with |LaunchProcess()|.
  125. // TODO(viettrungluu): Remove this and add a virtual
  126. // |ModifyChildCommandLine()|; make the two divergent uses more sane.
  127. virtual CommandLine MakeCmdLine(const std::string& procname);
  128. private:
  129. DISALLOW_COPY_AND_ASSIGN(MultiProcessTest);
  130. };
  131. } // namespace base
  132. #endif // BASE_TEST_MULTIPROCESS_TEST_H_