summaryrefslogtreecommitdiffstats
path: root/mojo/common/test/multiprocess_test_helper.h
blob: 103dcf4b7009441efb84fcc8ef62f937a591819e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
#define MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_

#include <string>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/process/process_handle.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "mojo/system/embedder/scoped_platform_handle.h"
#include "testing/multiprocess_func_list.h"

namespace mojo {

namespace embedder {
class PlatformChannelPair;
}

namespace test {

class MultiprocessTestHelper {
 public:
  MultiprocessTestHelper();
  ~MultiprocessTestHelper();

  // Start a child process and run the "main" function "named" |test_child_name|
  // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| (below).
  void StartChild(const std::string& test_child_name);
  // Wait for the child process to terminate.
  // Returns the exit code of the child process. Note that, though it's declared
  // to be an |int|, the exit code is subject to mangling by the OS. E.g., we
  // usually return -1 on error in the child (e.g., if |test_child_name| was not
  // found), but this is mangled to 255 on Linux. You should only rely on codes
  // 0-127 being preserved, and -1 being outside the range 0-127.
  int WaitForChildShutdown();

  // For use by |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| only:
  static void ChildSetup();

  // For use in the main process:
  embedder::ScopedPlatformHandle server_platform_handle;

  // For use (and only valid) in the child process:
  static embedder::ScopedPlatformHandle client_platform_handle;

 private:
  scoped_ptr<embedder::PlatformChannelPair> platform_channel_pair_;

  // Valid after |StartChild()| and before |WaitForChildShutdown()|.
  base::ProcessHandle test_child_handle_;

  DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper);
};

// Use this to declare the child process's "main()" function for tests using
// |MultiprocessTestHelper|. It returns an |int|, which will be the process's
// exit code (but see the comment about |WaitForChildShutdown()|).
#define MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) \
    MULTIPROCESS_TEST_MAIN_WITH_SETUP( \
        test_child_name ## TestChildMain, \
        ::mojo::test::MultiprocessTestHelper::ChildSetup)

}  // namespace test
}  // namespace mojo

#endif  // MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_