Skip to content

Commit

Permalink
Try to avoid port collisions
Browse files Browse the repository at this point in the history
On our CI environment it seems that every now and then these tests fail
because the port it tries to bind to is already in use,
see #16
  • Loading branch information
gforcada committed Oct 8, 2018
1 parent 03ac926 commit 239844d
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions plone/cachepurging/tests/test_purger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
import unittest


# Define a test HTTP server that returns canned responses

SERVER_PORT = int(os.environ.get("ZSERVER_PORT", 8765))


class TestHandler(BaseHTTPRequestHandler):
def do_PURGE(self):
# Get the pre-defined response from the server's queue.
Expand Down Expand Up @@ -75,7 +70,7 @@ def queue_response(self, **kw):
class TestCase(unittest.TestCase):
def setUp(self):
self.purger = DefaultPurger()
self.httpd, self.httpt = self.startServer()
self.httpd, self.httpt, self.port = self.startServer()

def tearDown(self):
try:
Expand All @@ -101,16 +96,18 @@ def tearDown(self):
self.purger = None
self.httpd, self.httpt = None, None

def startServer(self, port=SERVER_PORT, start=True):
def startServer(self, start=True):
"""Start a TestHTTPServer in a separate thread, returning a tuple
(server, thread). If start is true, the thread is started.
"""
server_address = ("localhost", port)
environment_port = int(os.environ.get("ZSERVER_PORT", 0))
server_address = ("localhost", environment_port)
httpd = TestHTTPServer(server_address, TestHandler)
_, actual_port = httpd.socket.getsockname()
t = threading.Thread(target=httpd.serve_forever)
if start:
t.start()
return httpd, t
return httpd, t, actual_port


class TestSync(TestCase):
Expand All @@ -121,8 +118,8 @@ def setUp(self):
def tearDown(self):
super(TestSync, self).tearDown()

def dispatchURL(self, path, method="PURGE", port=SERVER_PORT):
url = "http://localhost:%s%s" % (port, path)
def dispatchURL(self, path, method="PURGE"):
url = "http://localhost:%s%s" % (self.port, path)
return self.purger.purgeSync(url, method)

def testSimpleSync(self):
Expand All @@ -145,8 +142,8 @@ def testError(self):


class TestAsync(TestCase):
def dispatchURL(self, path, method="PURGE", port=SERVER_PORT):
url = "http://localhost:%s%s" % (port, path)
def dispatchURL(self, path, method="PURGE"):
url = "http://localhost:%s%s" % (self.port, path)
self.purger.purgeAsync(url, method)

# Item should now be in the queue!
Expand Down Expand Up @@ -197,10 +194,10 @@ class TestAsyncConnectionFailure(TestCase):
def setUp(self):
# Override setup to not start the server immediately
self.purger = DefaultPurger()
self.httpd, self.httpt = self.startServer(start=False)
self.httpd, self.httpt, self.port = self.startServer(start=False)

def dispatchURL(self, path, method="PURGE", port=SERVER_PORT):
url = "http://localhost:%s%s" % (port, path)
def dispatchURL(self, path, method="PURGE"):
url = "http://localhost:%s%s" % (self.port, path)
self.purger.purgeAsync(url, method)

# Item should now be in the queue!
Expand Down

0 comments on commit 239844d

Please sign in to comment.