Issue #27614: Avoid race in test_docxmlrpc server setup

This commit is contained in:
Martin Panter 2016-08-18 06:12:22 +00:00
parent 42e9d94bc5
commit 3911d8333c
1 changed files with 11 additions and 29 deletions

View File

@ -3,12 +3,8 @@ import httplib
import sys import sys
from test import test_support from test import test_support
threading = test_support.import_module('threading') threading = test_support.import_module('threading')
import time
import socket
import unittest import unittest
PORT = None
def make_request_and_skipIf(condition, reason): def make_request_and_skipIf(condition, reason):
# If we skip the test, we have to make a request because # If we skip the test, we have to make a request because
# the server created in setUp blocks expecting one to come in. # the server created in setUp blocks expecting one to come in.
@ -23,13 +19,10 @@ def make_request_and_skipIf(condition, reason):
return decorator return decorator
def server(evt, numrequests): def make_server():
serv = DocXMLRPCServer(("localhost", 0), logRequests=False) serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
try: try:
global PORT
PORT = serv.socket.getsockname()[1]
# Add some documentation # Add some documentation
serv.set_server_title("DocXMLRPCServer Test Documentation") serv.set_server_title("DocXMLRPCServer Test Documentation")
serv.set_server_name("DocXMLRPCServer Test Docs") serv.set_server_name("DocXMLRPCServer Test Docs")
@ -56,42 +49,31 @@ def server(evt, numrequests):
serv.register_function(add) serv.register_function(add)
serv.register_function(lambda x, y: x-y) serv.register_function(lambda x, y: x-y)
return serv
while numrequests > 0: except:
serv.handle_request()
numrequests -= 1
except socket.timeout:
pass
finally:
serv.server_close() serv.server_close()
PORT = None raise
evt.set()
class DocXMLRPCHTTPGETServer(unittest.TestCase): class DocXMLRPCHTTPGETServer(unittest.TestCase):
def setUp(self): def setUp(self):
self._threads = test_support.threading_setup()
# Enable server feedback # Enable server feedback
DocXMLRPCServer._send_traceback_header = True DocXMLRPCServer._send_traceback_header = True
self.evt = threading.Event() self.serv = make_server()
threading.Thread(target=server, args=(self.evt, 1)).start() self.thread = threading.Thread(target=self.serv.serve_forever)
self.thread.start()
# wait for port to be assigned
n = 1000
while n > 0 and PORT is None:
time.sleep(0.001)
n -= 1
PORT = self.serv.server_address[1]
self.client = httplib.HTTPConnection("localhost:%d" % PORT) self.client = httplib.HTTPConnection("localhost:%d" % PORT)
def tearDown(self): def tearDown(self):
self.client.close() self.client.close()
self.evt.wait()
# Disable server feedback # Disable server feedback
DocXMLRPCServer._send_traceback_header = False DocXMLRPCServer._send_traceback_header = False
test_support.threading_cleanup(*self._threads) self.serv.shutdown()
self.thread.join()
self.serv.server_close()
def test_valid_get_response(self): def test_valid_get_response(self):
self.client.request("GET", "/") self.client.request("GET", "/")