Skip to content

Commit

Permalink
Fix Android multi-process tests on M.
Browse files Browse the repository at this point in the history
On M, there are two issues:
1. Fd 4 is important, although I don't know what it is.
2. Some test environment setup can't be done in the child process.

BUG=608732,585849

Review-Url: https://codereview.chromium.org/1952513003
Cr-Commit-Position: refs/heads/master@{#391692}
  • Loading branch information
akmistry authored and Commit bot committed May 4, 2016
1 parent 08d81e2 commit 931b0d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion base/posix/global_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class BASE_EXPORT GlobalDescriptors {
#if !defined(OS_ANDROID)
static const int kBaseDescriptor = 3; // 0, 1, 2 are already taken.
#else
static const int kBaseDescriptor = 4; // 3 used by __android_log_write().
// 3 used by __android_log_write().
// 4 used by... something important on Android M.
static const int kBaseDescriptor = 5;
#endif

// Return the singleton instance of GlobalDescriptors.
Expand Down
3 changes: 3 additions & 0 deletions base/test/multiprocess_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ CommandLine GetMultiProcessTestChildBaseCommandLine();
// component builds.
void InitAndroidMultiProcessTestHelper(int (*main)(int, char**));

// Returns true if the current process is a test child.
bool AndroidIsChildProcess();

// Wait for a test child to exit if the alternate test child implementation is
// being used.
bool AndroidWaitForChildExitWithTimeout(
Expand Down
18 changes: 14 additions & 4 deletions base/test/multiprocess_test_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class LaunchHelper {
int* exit_code);

bool IsReady() const { return child_fd_ != -1; }
bool IsChild() const { return parent_fd_ != -1; }
bool IsChild() const { return is_child_; }

private:
// Wrappers around sendmsg/recvmsg that supports message fragmentation.
Expand All @@ -119,6 +119,8 @@ class LaunchHelper {
std::vector<ScopedFD> fds);
void WaitForChildInHelper(const WaitProcessRequest* request);

bool is_child_ = false;

// Parent vars.
int child_fd_ = -1;

Expand Down Expand Up @@ -204,6 +206,7 @@ void LaunchHelper::DoParent(int fd) {

void LaunchHelper::DoHelper(int fd) {
parent_fd_ = fd;
is_child_ = true;
std::unique_ptr<char[]> buf(new char[kMaxMessageSize]);
while (true) {
// Wait for a message from the parent.
Expand Down Expand Up @@ -247,6 +250,7 @@ void LaunchHelper::StartProcessInHelper(const StartProcessRequest* request,
} else {
// Child.
PCHECK(close(parent_fd_) == 0);
parent_fd_ = -1;
CommandLine::Reset();

Pickle serialised_extra(reinterpret_cast<const char*>(request + 1),
Expand All @@ -264,10 +268,12 @@ void LaunchHelper::StartProcessInHelper(const StartProcessRequest* request,
int new_fd;
CHECK(iter.ReadInt(&new_fd));
int old_fd = fds[i].release();
if (dup2(old_fd, new_fd) < 0) {
PLOG(FATAL) << "dup2";
if (new_fd != old_fd) {
if (dup2(old_fd, new_fd) < 0) {
PLOG(FATAL) << "dup2";
}
PCHECK(close(old_fd) == 0);
}
PCHECK(close(old_fd) == 0);
}

std::unique_ptr<char*[]> argv(new char*[args.size()]);
Expand Down Expand Up @@ -370,6 +376,10 @@ void InitAndroidMultiProcessTestHelper(int (*main)(int, char**)) {
g_launch_helper.Get().Init(main);
}

bool AndroidIsChildProcess() {
return g_launch_helper.Get().IsChild();
}

bool AndroidWaitForChildExitWithTimeout(
const Process& process, TimeDelta timeout, int* exit_code) {
CHECK(g_launch_helper.Get().IsReady());
Expand Down
7 changes: 5 additions & 2 deletions base/test/test_support_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/message_loop/message_pump_android.h"
#include "base/path_service.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/multiprocess_test.h"

namespace {

Expand Down Expand Up @@ -176,8 +177,10 @@ void InitAndroidTestMessageLoop() {
}

void InitAndroidTest() {
InitAndroidTestLogging();
InitAndroidTestPaths();
if (!base::AndroidIsChildProcess()) {
InitAndroidTestLogging();
InitAndroidTestPaths();
}
InitAndroidTestMessageLoop();
}
} // namespace base

0 comments on commit 931b0d6

Please sign in to comment.