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

Side by Side Diff: net/test/spawned_test_server/local_test_server_posix.cc

Issue 191673003: Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/test/spawned_test_server/local_test_server.h" 5 #include "net/test/spawned_test_server/local_test_server.h"
6 6
7 #include <poll.h> 7 #include <poll.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/files/scoped_file.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/process/kill.h" 15 #include "base/process/kill.h"
15 #include "base/process/launch.h" 16 #include "base/process/launch.h"
16 #include "base/process/process_iterator.h" 17 #include "base/process/process_iterator.h"
17 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
19 #include "base/test/test_timeouts.h" 20 #include "base/test/test_timeouts.h"
20 #include "net/test/python_utils.h" 21 #include "net/test/python_utils.h"
21 22
22 namespace { 23 namespace {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 if (!AddCommandLineArguments(&python_command)) 114 if (!AddCommandLineArguments(&python_command))
114 return false; 115 return false;
115 116
116 int pipefd[2]; 117 int pipefd[2];
117 if (pipe(pipefd) != 0) { 118 if (pipe(pipefd) != 0) {
118 PLOG(ERROR) << "Could not create pipe."; 119 PLOG(ERROR) << "Could not create pipe.";
119 return false; 120 return false;
120 } 121 }
121 122
122 // Save the read half. The write half is sent to the child. 123 // Save the read half. The write half is sent to the child.
123 child_fd_ = pipefd[0]; 124 child_fd_.reset(pipefd[0]);
124 child_fd_closer_.reset(&child_fd_); 125 base::ScopedFD write_closer(pipefd[1]);
125 file_util::ScopedFD write_closer(&pipefd[1]);
126 base::FileHandleMappingVector map_write_fd; 126 base::FileHandleMappingVector map_write_fd;
127 map_write_fd.push_back(std::make_pair(pipefd[1], pipefd[1])); 127 map_write_fd.push_back(std::make_pair(pipefd[1], pipefd[1]));
128 128
129 python_command.AppendArg("--startup-pipe=" + base::IntToString(pipefd[1])); 129 python_command.AppendArg("--startup-pipe=" + base::IntToString(pipefd[1]));
130 130
131 // Try to kill any orphaned testserver processes that may be running. 131 // Try to kill any orphaned testserver processes that may be running.
132 OrphanedTestServerFilter filter(testserver_path.value(), 132 OrphanedTestServerFilter filter(testserver_path.value(),
133 base::IntToString(GetPort())); 133 base::IntToString(GetPort()));
134 if (!base::KillProcesses("python", -1, &filter)) { 134 if (!base::KillProcesses("python", -1, &filter)) {
135 LOG(WARNING) << "Failed to clean up older orphaned testserver instances."; 135 LOG(WARNING) << "Failed to clean up older orphaned testserver instances.";
136 } 136 }
137 137
138 // Launch a new testserver process. 138 // Launch a new testserver process.
139 base::LaunchOptions options; 139 base::LaunchOptions options;
140 140
141 options.fds_to_remap = &map_write_fd; 141 options.fds_to_remap = &map_write_fd;
142 if (!base::LaunchProcess(python_command, options, &process_handle_)) { 142 if (!base::LaunchProcess(python_command, options, &process_handle_)) {
143 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); 143 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString();
144 return false; 144 return false;
145 } 145 }
146 146
147 return true; 147 return true;
148 } 148 }
149 149
150 bool LocalTestServer::WaitToStart() { 150 bool LocalTestServer::WaitToStart() {
151 file_util::ScopedFD child_fd_closer(child_fd_closer_.release()); 151 base::ScopedFD our_fd(child_fd_.release());
152 152
153 base::TimeDelta remaining_time = TestTimeouts::action_timeout(); 153 base::TimeDelta remaining_time = TestTimeouts::action_timeout();
154 154
155 uint32 server_data_len = 0; 155 uint32 server_data_len = 0;
156 if (!ReadData(child_fd_, sizeof(server_data_len), 156 if (!ReadData(our_fd.get(), sizeof(server_data_len),
157 reinterpret_cast<uint8*>(&server_data_len), 157 reinterpret_cast<uint8*>(&server_data_len),
158 &remaining_time)) { 158 &remaining_time)) {
159 LOG(ERROR) << "Could not read server_data_len"; 159 LOG(ERROR) << "Could not read server_data_len";
160 return false; 160 return false;
161 } 161 }
162 std::string server_data(server_data_len, '\0'); 162 std::string server_data(server_data_len, '\0');
163 if (!ReadData(child_fd_, server_data_len, 163 if (!ReadData(our_fd.get(), server_data_len,
164 reinterpret_cast<uint8*>(&server_data[0]), 164 reinterpret_cast<uint8*>(&server_data[0]),
165 &remaining_time)) { 165 &remaining_time)) {
166 LOG(ERROR) << "Could not read server_data (" << server_data_len 166 LOG(ERROR) << "Could not read server_data (" << server_data_len
167 << " bytes)"; 167 << " bytes)";
168 return false; 168 return false;
169 } 169 }
170 170
171 if (!ParseServerData(server_data)) { 171 if (!ParseServerData(server_data)) {
172 LOG(ERROR) << "Could not parse server_data: " << server_data; 172 LOG(ERROR) << "Could not parse server_data: " << server_data;
173 return false; 173 return false;
174 } 174 }
175 175
176 return true; 176 return true;
177 } 177 }
178 178
179 } // namespace net 179 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698