Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Unified Diff: chrome/test/base/ui_test_utils.cc

Issue 8633004: Stop WebSocker server processes certainly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: again Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/test/base/ui_test_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/base/ui_test_utils.cc
diff --git a/chrome/test/base/ui_test_utils.cc b/chrome/test/base/ui_test_utils.cc
index 44aed98add084ddcb03d67a39670b0c2d9c6c975..da384c753c28b11d7ca734b4d6f3dd89c301a0ea 100644
--- a/chrome/test/base/ui_test_utils.cc
+++ b/chrome/test/base/ui_test_utils.cc
@@ -4,6 +4,10 @@
#include "chrome/test/base/ui_test_utils.h"
+#if defined(OS_WIN)
+#include <windows.h>
+#endif
+
#include <vector>
#include "base/bind.h"
@@ -16,6 +20,7 @@
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/process_util.h"
+#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/automation/ui_controls.h"
@@ -62,6 +67,8 @@
#include "ui/aura/desktop.h"
#endif
+static const int kDefaultWsPort = 8880;
+
namespace ui_test_utils {
namespace {
@@ -727,7 +734,53 @@ void AppendToPythonPath(const FilePath& dir) {
} // anonymous namespace
-TestWebSocketServer::TestWebSocketServer() : started_(false) {
+#if defined(OS_POSIX)
+class OrphanedWebSocketServerFilter : public base::ProcessFilter {
Paweł Hajdan Jr. 2011/11/23 17:50:43 Could you remove it? It shouldn't be necessary now
Takashi Toyoshima 2011/11/23 23:29:43 OK, I remove OrphanedWebSocketServerFilter related
+ public:
+ OrphanedWebSocketServerFilter(
+ const std::string& script_name, const std::string& port)
+ : script_name_(script_name),
+ port_(port) {}
+
+ virtual bool Includes(const base::ProcessEntry& entry) const {
+ // The parent process of orphaned WebSocket servers must be root process.
+ if (entry.parent_pid() != 1)
+ return false;
+
+ // Python script name must be ended with specified script name.
+ const std::string &script_name = entry.cmd_line_args()[2];
+ if (script_name.find_last_of(script_name_) != script_name.size() - 1)
+ return false;
+
+ // Check if an argument sequence like "--port 8880" exists.
+ bool found = false;
+ for (std::vector<std::string>::const_iterator it =
+ entry.cmd_line_args().begin();
+ it != entry.cmd_line_args().end();
+ ++it) {
+ if (*it == "--port") {
+ ++it;
+ if (it == entry.cmd_line_args().end() && *it == port_) {
+ found = true;
+ break;
+ }
+ }
+ }
+ return found;
+ }
+
+ private:
+ std::string script_name_;
+ std::string port_;
+ DISALLOW_COPY_AND_ASSIGN(OrphanedWebSocketServerFilter);
+};
+#endif
+
+TestWebSocketServer::TestWebSocketServer()
+ : started_(false) {
+#if defined(OS_POSIX)
+ process_handle_ = base::kNullProcessHandle;
+#endif
}
bool TestWebSocketServer::Start(const FilePath& root_directory) {
@@ -740,6 +793,7 @@ bool TestWebSocketServer::Start(const FilePath& root_directory) {
cmd_line->AppendArg("--register_cygwin");
cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") +
root_directory.value());
+ cmd_line->AppendArg("--port=" + base::IntToString(kDefaultWsPort));
if (!temp_dir_.CreateUniqueTempDir()) {
LOG(ERROR) << "Unable to create a temporary directory.";
return false;
@@ -748,9 +802,44 @@ bool TestWebSocketServer::Start(const FilePath& root_directory) {
cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") +
websocket_pid_file_.value());
SetPythonPath();
+
base::LaunchOptions options;
+ base::ProcessHandle* process_handle = NULL;
+
+#if defined(OS_POSIX)
+ // Try to kill any orphaned WebSocket server processes that may be running.
+ OrphanedWebSocketServerFilter filter("standalone.py",
+ base::IntToString(kDefaultWsPort));
+ if (!base::KillProcesses("python", -1, &filter)) {
+ LOG(WARNING) <<
+ "Failed to clean up older orphaned WebSocket server instance.";
+ }
+
+ options.new_process_group = true;
+ process_handle = &process_handle_;
+#elif defined(OS_WIN)
+ job_handle_.Set(CreateJobObject(NULL, NULL));
+ if (!job_handle_.IsValid()) {
+ LOG(ERROR) << "Could not create JobObject.";
+ return false;
+ }
+
+ JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info = {0};
+ limit_info.BasicLimitInformation.LimitFlags =
+ JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
+ if (0 == SetInformationJobObject(job_handle_.Get(),
+ JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info))) {
+ LOG(ERROR) << "Could not SetInformationJobObject.";
+ return false;
+ }
+
+ options.inherit_handles = true;
+ options.job_handle = job_handle_.Get();
+#endif
+
+ // Launch a new WebSocket server process.
options.wait = true;
- if (!base::LaunchProcess(*cmd_line.get(), options, NULL)) {
+ if (!base::LaunchProcess(*cmd_line.get(), options, process_handle)) {
LOG(ERROR) << "Unable to launch websocket server.";
return false;
}
@@ -805,6 +894,11 @@ TestWebSocketServer::~TestWebSocketServer() {
base::LaunchOptions options;
options.wait = true;
base::LaunchProcess(*cmd_line.get(), options, NULL);
+
+#if defined(PS_POSIX)
Paweł Hajdan Jr. 2011/11/23 17:50:43 Ooops, this should be OS_POSIX.
Takashi Toyoshima 2011/11/23 23:29:43 Done.
+ // Just to make sure that the server process terminates certainly.
+ base::KillProcessGroup(process_handle_);
+#endif
}
TestNotificationObserver::TestNotificationObserver()
« no previous file with comments | « chrome/test/base/ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698