Skip to content

Commit

Permalink
Make Python sync server stoppable
Browse files Browse the repository at this point in the history
The testserver attempts to inherit from StoppableHTTPServer, but then
overrides the stoppable functionality. Instead of overriding serve_forever, override handle_request, to maintain stoppability.

BUG=

Review URL: https://codereview.chromium.org/797253003

Cr-Commit-Position: refs/heads/master@{#310732}
  • Loading branch information
sigbjornOpera authored and Commit bot committed Jan 9, 2015
1 parent 66c9d8a commit 1e63012
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions sync/tools/testserver/sync_testserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ def SetAuthenticated(self, auth_valid):
def GetAuthenticated(self):
return self.authenticated

def serve_forever(self):
"""This is a merge of asyncore.loop() and SocketServer.serve_forever().
"""

def handle_request(self):
"""Adaptation of asyncore.loop"""
def HandleXmppSocket(fd, socket_map, handler):
"""Runs the handler for the xmpp connection for fd.
Expand All @@ -92,44 +90,43 @@ def HandleXmppSocket(fd, socket_map, handler):
except:
xmpp_connection.handle_error()

while True:
read_fds = [ self.fileno() ]
write_fds = []
exceptional_fds = []

for fd, xmpp_connection in self._xmpp_socket_map.items():
is_r = xmpp_connection.readable()
is_w = xmpp_connection.writable()
if is_r:
read_fds.append(fd)
if is_w:
write_fds.append(fd)
if is_r or is_w:
exceptional_fds.append(fd)
read_fds = [ self.fileno() ]
write_fds = []
exceptional_fds = []

try:
read_fds, write_fds, exceptional_fds = (
select.select(read_fds, write_fds, exceptional_fds))
except select.error, err:
if err.args[0] != errno.EINTR:
raise
else:
continue

for fd in read_fds:
if fd == self.fileno():
self.HandleRequestNoBlock()
continue
HandleXmppSocket(fd, self._xmpp_socket_map,
asyncore.dispatcher.handle_read_event)

for fd in write_fds:
HandleXmppSocket(fd, self._xmpp_socket_map,
asyncore.dispatcher.handle_write_event)

for fd in exceptional_fds:
HandleXmppSocket(fd, self._xmpp_socket_map,
asyncore.dispatcher.handle_expt_event)
for fd, xmpp_connection in self._xmpp_socket_map.items():
is_r = xmpp_connection.readable()
is_w = xmpp_connection.writable()
if is_r:
read_fds.append(fd)
if is_w:
write_fds.append(fd)
if is_r or is_w:
exceptional_fds.append(fd)

try:
read_fds, write_fds, exceptional_fds = (
select.select(read_fds, write_fds, exceptional_fds))
except select.error, err:
if err.args[0] != errno.EINTR:
raise
else:
return

for fd in read_fds:
if fd == self.fileno():
self.HandleRequestNoBlock()
return
HandleXmppSocket(fd, self._xmpp_socket_map,
asyncore.dispatcher.handle_read_event)

for fd in write_fds:
HandleXmppSocket(fd, self._xmpp_socket_map,
asyncore.dispatcher.handle_write_event)

for fd in exceptional_fds:
HandleXmppSocket(fd, self._xmpp_socket_map,
asyncore.dispatcher.handle_expt_event)


class SyncPageHandler(testserver_base.BasePageHandler):
Expand Down

0 comments on commit 1e63012

Please sign in to comment.