diff options
Diffstat (limited to 'net/test')
-rw-r--r-- | net/test/python_utils_unittest.cc | 25 | ||||
-rw-r--r-- | net/test/test_server.cc | 43 | ||||
-rw-r--r-- | net/test/test_server.h | 6 | ||||
-rw-r--r-- | net/test/test_server_posix.cc | 51 |
4 files changed, 93 insertions, 32 deletions
diff --git a/net/test/python_utils_unittest.cc b/net/test/python_utils_unittest.cc index f0060fef..d0e568dc 100644 --- a/net/test/python_utils_unittest.cc +++ b/net/test/python_utils_unittest.cc @@ -2,9 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <string> + +#include "base/command_line.h" #include "base/environment.h" #include "base/file_path.h" +#include "base/process_util.h" #include "base/scoped_ptr.h" +#include "base/stringprintf.h" +#include "base/string_util.h" #include "net/test/python_utils.h" #include "testing/gtest/include/gtest/gtest.h" @@ -41,11 +47,16 @@ TEST(PythonUtils, Append) { TEST(PythonUtils, PythonRunTime) { FilePath dir; EXPECT_TRUE(GetPythonRunTime(&dir)); -#if defined(OS_WIN) - EXPECT_NE(std::wstring::npos, - dir.value().find(L"\\third_party\\python_24\\python.exe")); -#elif defined(OS_POSIX) - EXPECT_NE(std::string::npos, dir.value().find("python")); -#endif -} + // Run a python command to print a string and make sure the output is what + // we want. + CommandLine cmd_line(dir); + cmd_line.AppendArg("-c"); + std::string input("PythonUtilsTest"); + std::string python_cmd = StringPrintf("print '%s';", input.c_str()); + cmd_line.AppendArg(python_cmd); + std::string output; + EXPECT_TRUE(base::GetAppOutput(cmd_line, &output)); + TrimWhitespace(output, TRIM_TRAILING, &output); + EXPECT_EQ(input, output); +} diff --git a/net/test/test_server.cc b/net/test/test_server.cc index f4eb64fd..c3359a42 100644 --- a/net/test/test_server.cc +++ b/net/test/test_server.cc @@ -164,26 +164,12 @@ bool TestServer::Stop() { base::CloseProcessHandle(process_handle_); process_handle_ = base::kNullProcessHandle; } else { - LOG(INFO) << "Kill failed?"; + VLOG(1) << "Kill failed?"; } return ret; } -bool TestServer::WaitToFinish(int timeout_ms) { - if (!process_handle_) - return true; - - bool ret = base::WaitForSingleProcess(process_handle_, timeout_ms); - if (ret) { - base::CloseProcessHandle(process_handle_); - process_handle_ = base::kNullProcessHandle; - } else { - LOG(ERROR) << "Timed out."; - } - return ret; -} - std::string TestServer::GetScheme() const { switch (type_) { case TYPE_FTP: @@ -204,7 +190,7 @@ std::string TestServer::GetScheme() const { bool TestServer::GetAddressList(AddressList* address_list) const { DCHECK(address_list); - scoped_refptr<HostResolver> resolver( + scoped_ptr<HostResolver> resolver( CreateSystemHostResolver(HostResolver::kDefaultParallelism, NULL)); HostResolver::RequestInfo info(host_port_pair_); int rv = resolver->Resolve(info, address_list, NULL, NULL, BoundNetLog()); @@ -252,9 +238,28 @@ bool TestServer::SetPythonPath() { LOG(ERROR) << "Failed to get DIR_EXE"; return false; } - generated_code_dir = generated_code_dir.Append(FILE_PATH_LITERAL("pyproto")); - AppendToPythonPath(generated_code_dir); - AppendToPythonPath(generated_code_dir.Append(FILE_PATH_LITERAL("sync_pb"))); + + static const FilePath kPyProto(FILE_PATH_LITERAL("pyproto")); + +#if defined(OS_MACOSX) + // On Mac, DIR_EXE might be pointing deep into the Release/ (or Debug/) + // directory and we can't depend on how far down it goes. So we walk upwards + // from DIR_EXE until we find a likely looking spot. + while (!file_util::DirectoryExists(generated_code_dir.Append(kPyProto))) { + FilePath parent = generated_code_dir.DirName(); + if (parent == generated_code_dir) { + // We hit the root directory. Maybe we didn't build any targets which + // produced Python protocol buffers. + PathService::Get(base::DIR_EXE, &generated_code_dir); + break; + } + generated_code_dir = parent; + } +#endif + + AppendToPythonPath(generated_code_dir.Append(kPyProto)); + AppendToPythonPath(generated_code_dir.Append(kPyProto). + Append(FILE_PATH_LITERAL("sync_pb"))); return true; } diff --git a/net/test/test_server.h b/net/test/test_server.h index 6baaaeca..4e68fd93 100644 --- a/net/test/test_server.h +++ b/net/test/test_server.h @@ -52,12 +52,6 @@ class TestServer { // Stop the server started by Start(). bool Stop(); - // If you access the server's Kill url, it will exit by itself - // without a call to Stop(). - // WaitToFinish is handy in that case. - // It returns true if the server exited cleanly. - bool WaitToFinish(int milliseconds) WARN_UNUSED_RESULT; - const FilePath& document_root() const { return document_root_; } const HostPortPair& host_port_pair() const { return host_port_pair_; } std::string GetScheme() const; diff --git a/net/test/test_server_posix.cc b/net/test/test_server_posix.cc index 262845c1..6e65bcf9 100644 --- a/net/test/test_server_posix.cc +++ b/net/test/test_server_posix.cc @@ -4,9 +4,52 @@ #include "net/test/test_server.h" +#include <vector> + #include "base/file_util.h" #include "base/logging.h" +#include "base/process_util.h" #include "base/string_number_conversions.h" +#include "base/string_util.h" + +namespace { + +// Helper class used to detect and kill orphaned python test server processes. +// Checks if the command line of a process contains |path_string| (the path +// from which the test server was launched) and |port_string| (the port used by +// the test server), and if the parent pid of the process is 1 (indicating that +// it is an orphaned process). +class OrphanedTestServerFilter : public base::ProcessFilter { + public: + OrphanedTestServerFilter( + const std::string& path_string, const std::string& port_string) + : path_string_(path_string), + port_string_(port_string) {} + + virtual bool Includes(const base::ProcessEntry& entry) const { + if (entry.parent_pid() != 1) + return false; + bool found_path_string = false; + bool found_port_string = false; + for (std::vector<std::string>::const_iterator it = + entry.cmd_line_args().begin(); + it != entry.cmd_line_args().end(); + ++it) { + if (it->find(path_string_) != std::string::npos) + found_path_string = true; + if (it->find(port_string_) != std::string::npos) + found_port_string = true; + } + return found_path_string && found_port_string; + } + + private: + std::string path_string_; + std::string port_string_; + DISALLOW_COPY_AND_ASSIGN(OrphanedTestServerFilter); +}; + +} // namespace namespace net { bool TestServer::LaunchPython(const FilePath& testserver_path) { @@ -47,6 +90,14 @@ bool TestServer::LaunchPython(const FilePath& testserver_path) { command_line.push_back("--startup-pipe=" + base::IntToString(pipefd[1])); + // Try to kill any orphaned testserver processes that may be running. + OrphanedTestServerFilter filter(testserver_path.value(), + base::IntToString(host_port_pair_.port())); + if (!base::KillProcesses(L"python", -1, &filter)) { + LOG(WARNING) << "Failed to clean up older orphaned testserver instances."; + } + + // Launch a new testserver process. if (!base::LaunchApp(command_line, map_write_fd, false, &process_handle_)) { LOG(ERROR) << "Failed to launch " << command_line[0] << " ..."; return false; |