This source file includes following definitions.
- user_data_dir_
- Reset
- StartChrome
- SetUp
- TearDown
- KillProcessTree
- Includes
- IN_PROC_BROWSER_TEST_F
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process_iterator.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_launcher_utils.h"
namespace {
class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> {
public:
ChromeStarter(base::TimeDelta timeout, const base::FilePath& user_data_dir)
: ready_event_(false , false ),
done_event_(false , false ),
process_handle_(base::kNullProcessHandle),
process_terminated_(false),
timeout_(timeout),
user_data_dir_(user_data_dir) {
}
void Reset() {
ready_event_.Reset();
done_event_.Reset();
if (process_handle_ != base::kNullProcessHandle)
base::CloseProcessHandle(process_handle_);
process_handle_ = base::kNullProcessHandle;
process_terminated_ = false;
}
void StartChrome(base::WaitableEvent* start_event, bool first_run) {
base::FilePath program;
ASSERT_TRUE(PathService::Get(base::FILE_EXE, &program));
CommandLine command_line(program);
command_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_);
if (first_run)
command_line.AppendSwitch(switches::kForceFirstRun);
else
command_line.AppendSwitch(switches::kNoFirstRun);
CommandLine standard_switches(CommandLine::NO_PROGRAM);
test_launcher_utils::PrepareBrowserCommandLineForTests(&standard_switches);
const CommandLine::SwitchMap& switch_map = standard_switches.GetSwitches();
for (CommandLine::SwitchMap::const_iterator i = switch_map.begin();
i != switch_map.end(); ++i) {
const std::string& switch_name = i->first;
if (switch_name == switches::kUserDataDir ||
switch_name == switches::kForceFirstRun ||
switch_name == switches::kNoFirstRun)
continue;
command_line.AppendSwitchNative(switch_name, i->second);
}
ready_event_.Signal();
ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event);
start_event->Wait();
base::LaunchProcess(command_line, base::LaunchOptions(), &process_handle_);
ASSERT_NE(base::kNullProcessHandle, process_handle_);
process_terminated_ = base::WaitForSingleProcess(process_handle_,
timeout_);
done_event_.Signal();
}
base::WaitableEvent ready_event_;
base::WaitableEvent done_event_;
base::ProcessHandle process_handle_;
bool process_terminated_;
private:
friend class base::RefCountedThreadSafe<ChromeStarter>;
~ChromeStarter() {
if (process_handle_ != base::kNullProcessHandle)
base::CloseProcessHandle(process_handle_);
}
base::TimeDelta timeout_;
base::FilePath user_data_dir_;
DISALLOW_COPY_AND_ASSIGN(ChromeStarter);
};
}
class ProcessSingletonTest : public InProcessBrowserTest {
public:
ProcessSingletonTest()
: threads_waker_(true , false ) {
EXPECT_TRUE(temp_profile_dir_.CreateUniqueTempDir());
}
virtual void SetUp() {
for (size_t i = 0; i < kNbThreads; ++i) {
chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter"));
ASSERT_TRUE(chrome_starter_threads_[i]->Start());
chrome_starters_[i] = new ChromeStarter(
TestTimeouts::action_max_timeout(), temp_profile_dir_.path());
}
}
virtual void TearDown() {
for (size_t i = 0; i < kNbThreads; ++i)
chrome_starter_threads_[i]->Stop();
}
void KillProcessTree(base::ProcessHandle process_handle) {
class ProcessTreeFilter : public base::ProcessFilter {
public:
explicit ProcessTreeFilter(base::ProcessId parent_pid) {
ancestor_pids_.insert(parent_pid);
}
virtual bool Includes(const base::ProcessEntry & entry) const OVERRIDE {
if (ancestor_pids_.find(entry.parent_pid()) != ancestor_pids_.end()) {
ancestor_pids_.insert(entry.pid());
return true;
} else {
return false;
}
}
private:
mutable std::set<base::ProcessId> ancestor_pids_;
} process_tree_filter(base::GetProcId(process_handle));
static const int kExitCode = 42;
EXPECT_TRUE(base::KillProcess(process_handle, kExitCode, true ));
static const int kNbTries = 10;
int num_tries = 0;
base::FilePath program;
ASSERT_TRUE(PathService::Get(base::FILE_EXE, &program));
base::FilePath::StringType exe_name = program.BaseName().value();
while (base::GetProcessCount(exe_name, &process_tree_filter) > 0 &&
num_tries++ < kNbTries) {
base::KillProcesses(exe_name, kExitCode, &process_tree_filter);
}
DLOG_IF(ERROR, num_tries >= kNbTries) << "Failed to kill all processes!";
}
static const size_t kNbAttempts = 5;
static const size_t kNbThreads = 5;
scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads];
scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads];
base::WaitableEvent threads_waker_;
base::ScopedTempDir temp_profile_dir_;
};
#if defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
#define MAYBE_StartupRaceCondition DISABLED_StartupRaceCondition
#else
#define MAYBE_StartupRaceCondition StartupRaceCondition
#endif
IN_PROC_BROWSER_TEST_F(ProcessSingletonTest, MAYBE_StartupRaceCondition) {
bool failed = false;
for (size_t attempt = 0; attempt < kNbAttempts && !failed; ++attempt) {
SCOPED_TRACE(testing::Message() << "Attempt: " << attempt << ".");
threads_waker_.Reset();
#if defined(OS_POSIX)
bool first_run = false;
#else
bool first_run = attempt % 2;
#endif
for (size_t i = 0; i < kNbThreads; ++i) {
ASSERT_NE(static_cast<ChromeStarter*>(NULL), chrome_starters_[i].get());
chrome_starters_[i]->Reset();
ASSERT_TRUE(chrome_starter_threads_[i]->IsRunning());
ASSERT_NE(static_cast<base::MessageLoop*>(NULL),
chrome_starter_threads_[i]->message_loop());
chrome_starter_threads_[i]->message_loop()->PostTask(
FROM_HERE, base::Bind(&ChromeStarter::StartChrome,
chrome_starters_[i].get(),
&threads_waker_,
first_run));
}
for (size_t i = 0; i < kNbThreads; ++i) {
SCOPED_TRACE(testing::Message() << "Waiting on thread: " << i << ".");
chrome_starters_[i]->ready_event_.Wait();
}
threads_waker_.Signal();
std::vector<size_t> pending_starters(kNbThreads);
for (size_t i = 0; i < kNbThreads; ++i)
pending_starters[i] = i;
base::WaitableEvent* starters_done_events[kNbThreads];
while (pending_starters.size() > 1) {
SCOPED_TRACE(testing::Message() << pending_starters.size() <<
" starters left.");
for (size_t i = 0; i < pending_starters.size(); ++i) {
starters_done_events[i] =
&chrome_starters_[pending_starters[i]]->done_event_;
}
size_t done_index = base::WaitableEvent::WaitMany(
starters_done_events, pending_starters.size());
size_t starter_index = pending_starters[done_index];
EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ ||
failed) << "There is more than one main process.";
if (!chrome_starters_[starter_index]->process_terminated_) {
failed = true;
if (chrome_starters_[starter_index]->process_handle_ !=
base::kNullProcessHandle) {
KillProcessTree(chrome_starters_[starter_index]->process_handle_);
}
}
pending_starters.erase(pending_starters.begin() + done_index);
}
ASSERT_EQ(static_cast<size_t>(1), pending_starters.size());
size_t last_index = pending_starters.front();
pending_starters.clear();
if (chrome_starters_[last_index]->process_handle_ !=
base::kNullProcessHandle) {
KillProcessTree(chrome_starters_[last_index]->process_handle_);
chrome_starters_[last_index]->done_event_.Wait();
}
}
}