diff --git a/setup.py b/setup.py index cdd445ef..cea61f20 100644 --- a/setup.py +++ b/setup.py @@ -64,8 +64,9 @@ def read(path): ] }, extras_require=dict( - test=['zope.testing', - 'zc.customdoctests>=1.0.1'], + test=['zope.testing>=4,<5', + 'zc.customdoctests>=1.0.1,<2', + 'stopit>=1.1.2,<2'], sqlalchemy=['sqlalchemy>=1.0,<1.4', 'geojson>=2.5.0'] ), python_requires='>=3.4', diff --git a/src/crate/client/tests.py b/src/crate/client/tests.py index a0203e7c..58db6cb7 100644 --- a/src/crate/client/tests.py +++ b/src/crate/client/tests.py @@ -23,6 +23,7 @@ import json import os +import socket import unittest import doctest from pprint import pprint @@ -33,6 +34,8 @@ import threading import logging +import stopit + from crate.testing.layer import CrateLayer from crate.testing.tests import crate_path, docs_path from crate.client import connect @@ -258,7 +261,7 @@ def setUp(self): thread = threading.Thread(target=self.serve_forever) thread.daemon = True # quit interpreter when only thread exists thread.start() - time.sleep(0.5) + self.waitForServer() def serve_forever(self): print("listening on", self.HOST, self.PORT) @@ -268,6 +271,29 @@ def serve_forever(self): def tearDown(self): self.server.shutdown() + def isUp(self): + """ + Test if a host is up. + """ + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + ex = s.connect_ex((self.HOST, self.PORT)) + s.close() + return ex == 0 + + def waitForServer(self, timeout=5): + """ + Wait for the host to be available. + """ + with stopit.ThreadingTimeout(timeout) as to_ctx_mgr: + while True: + if self.isUp(): + break + time.sleep(0.001) + + if not to_ctx_mgr: + raise TimeoutError("Could not properly start embedded webserver " + "within {} seconds".format(timeout)) + def setUpWithHttps(test): test.globs['HttpClient'] = http.Client diff --git a/versions.cfg b/versions.cfg index 1ff59bf6..cd8759f6 100644 --- a/versions.cfg +++ b/versions.cfg @@ -24,6 +24,7 @@ zc.customdoctests = 1.0.1 zc.recipe.egg = 2.0.7 zc.recipe.testrunner = 2.2 zope.testing = 4.9 +stopit = 1.1.2 # Required by: # clint==0.5.1