summaryrefslogtreecommitdiffstats
path: root/net/test/test_server_posix.cc
diff options
context:
space:
mode:
authorakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-22 22:52:26 +0000
committerakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-22 22:52:26 +0000
commitd5dc41eebc137ac693a6ee1e527e927edd02ed28 (patch)
tree0b21967493f05ed032a7b5603a23102b319f1dd0 /net/test/test_server_posix.cc
parent761862b098c1593e2d0397c6ab99004c7d488af4 (diff)
downloadchromium_src-d5dc41eebc137ac693a6ee1e527e927edd02ed28.zip
chromium_src-d5dc41eebc137ac693a6ee1e527e927edd02ed28.tar.gz
chromium_src-d5dc41eebc137ac693a6ee1e527e927edd02ed28.tar.bz2
Made testserver communicate to parent process with JSON
This is so that if the testserver needs to communicate anything more than the port in the future (e.g., xmpp port for the test sync server), it can do so in a flexible manner. BUG=53934 TEST=manually Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=66879 Review URL: http://codereview.chromium.org/5196001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67018 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/test/test_server_posix.cc')
-rw-r--r--net/test/test_server_posix.cc85
1 files changed, 49 insertions, 36 deletions
diff --git a/net/test/test_server_posix.cc b/net/test/test_server_posix.cc
index 9c0210b..74dbcd4 100644
--- a/net/test/test_server_posix.cc
+++ b/net/test/test_server_posix.cc
@@ -53,6 +53,42 @@ class OrphanedTestServerFilter : public base::ProcessFilter {
DISALLOW_COPY_AND_ASSIGN(OrphanedTestServerFilter);
};
+// Given a file descriptor, reads into |buffer| until |bytes_max|
+// bytes has been read or an error has been encountered. Returns true
+// if the read was successful. |remaining_time| is used as a timeout.
+bool ReadData(int fd, ssize_t bytes_max, uint8* buffer,
+ base::TimeDelta* remaining_time) {
+ ssize_t bytes_read = 0;
+ base::Time previous_time = base::Time::Now();
+ while (bytes_read < bytes_max) {
+ struct pollfd poll_fds[1];
+
+ poll_fds[0].fd = fd;
+ poll_fds[0].events = POLLIN | POLLPRI;
+ poll_fds[0].revents = 0;
+
+ int rv = HANDLE_EINTR(poll(poll_fds, 1,
+ remaining_time->InMilliseconds()));
+ if (rv != 1) {
+ LOG(ERROR) << "Failed to poll for the child file descriptor.";
+ return false;
+ }
+
+ base::Time current_time = base::Time::Now();
+ base::TimeDelta elapsed_time_cycle = current_time - previous_time;
+ DCHECK(elapsed_time_cycle.InMilliseconds() >= 0);
+ *remaining_time -= elapsed_time_cycle;
+ previous_time = current_time;
+
+ ssize_t num_bytes = HANDLE_EINTR(read(fd, buffer + bytes_read,
+ bytes_max - bytes_read));
+ if (num_bytes <= 0)
+ return false;
+ bytes_read += num_bytes;
+ }
+ return true;
+}
+
} // namespace
namespace net {
@@ -98,45 +134,22 @@ bool TestServer::LaunchPython(const FilePath& testserver_path) {
}
bool TestServer::WaitToStart() {
- uint16 port;
- uint8* buffer = reinterpret_cast<uint8*>(&port);
- ssize_t bytes_read = 0;
- ssize_t bytes_max = sizeof(port);
+ file_util::ScopedFD child_fd_closer(child_fd_closer_.release());
+
base::TimeDelta remaining_time = base::TimeDelta::FromMilliseconds(
TestTimeouts::action_max_timeout_ms());
- base::Time previous_time = base::Time::Now();
- while (bytes_read < bytes_max) {
- struct pollfd poll_fds[1];
-
- poll_fds[0].fd = child_fd_;
- poll_fds[0].events = POLLIN | POLLPRI;
- poll_fds[0].revents = 0;
-
- int rv = HANDLE_EINTR(poll(poll_fds, 1, remaining_time.InMilliseconds()));
- if (rv != 1) {
- LOG(ERROR) << "Failed to poll for the child file descriptor.";
- return false;
- }
-
- base::Time current_time = base::Time::Now();
- base::TimeDelta elapsed_time_cycle = current_time - previous_time;
- DCHECK(elapsed_time_cycle.InMilliseconds() >= 0);
- remaining_time -= elapsed_time_cycle;
- previous_time = current_time;
-
- ssize_t num_bytes = HANDLE_EINTR(read(child_fd_, buffer + bytes_read,
- bytes_max - bytes_read));
- if (num_bytes <= 0)
- break;
- bytes_read += num_bytes;
- }
-
- // We don't need the FD anymore.
- child_fd_closer_.reset(NULL);
- if (bytes_read < bytes_max)
+ uint32 server_data_len = 0;
+ if (!ReadData(child_fd_, sizeof(server_data_len),
+ reinterpret_cast<uint8*>(&server_data_len),
+ &remaining_time))
return false;
- host_port_pair_.set_port(port);
- return true;
+ std::string server_data(server_data_len, '\0');
+ if (!ReadData(child_fd_, server_data_len,
+ reinterpret_cast<uint8*>(&server_data[0]),
+ &remaining_time))
+ return false;
+
+ return ParseServerData(server_data);
}
bool TestServer::CheckCATrusted() {