Skip to content

Commit 6472212

Browse files
committed
Tests: Remove arbitrary sleeping for waiting for the webserver to start
1 parent 0df889f commit 6472212

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

setup.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def read(path):
6464
]
6565
},
6666
extras_require=dict(
67-
test=['zope.testing',
68-
'zc.customdoctests>=1.0.1'],
67+
test=['zope.testing>=4,<5',
68+
'zc.customdoctests>=1.0.1,<2',
69+
'stopit>=1.1.2,<2'],
6970
sqlalchemy=['sqlalchemy>=1.0,<1.4', 'geojson>=2.5.0']
7071
),
7172
python_requires='>=3.4',

src/crate/client/tests.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import json
2525
import os
26+
import socket
2627
import unittest
2728
import doctest
2829
from pprint import pprint
@@ -33,6 +34,8 @@
3334
import threading
3435
import logging
3536

37+
import stopit
38+
3639
from crate.testing.layer import CrateLayer
3740
from crate.testing.tests import crate_path, docs_path
3841
from crate.client import connect
@@ -258,7 +261,7 @@ def setUp(self):
258261
thread = threading.Thread(target=self.serve_forever)
259262
thread.daemon = True # quit interpreter when only thread exists
260263
thread.start()
261-
time.sleep(0.5)
264+
self.waitForServer()
262265

263266
def serve_forever(self):
264267
print("listening on", self.HOST, self.PORT)
@@ -268,6 +271,29 @@ def serve_forever(self):
268271
def tearDown(self):
269272
self.server.shutdown()
270273

274+
def isUp(self):
275+
"""
276+
Test if a host is up.
277+
"""
278+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
279+
ex = s.connect_ex((self.HOST, self.PORT))
280+
s.close()
281+
return ex == 0
282+
283+
def waitForServer(self, timeout=5):
284+
"""
285+
Wait for the host to be available.
286+
"""
287+
with stopit.ThreadingTimeout(timeout) as to_ctx_mgr:
288+
while True:
289+
if self.isUp():
290+
break
291+
time.sleep(0.001)
292+
293+
if not to_ctx_mgr:
294+
raise TimeoutError("Could not properly start embedded webserver "
295+
"within {} seconds".format(timeout))
296+
271297

272298
def setUpWithHttps(test):
273299
test.globs['HttpClient'] = http.Client

versions.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ zc.customdoctests = 1.0.1
2424
zc.recipe.egg = 2.0.7
2525
zc.recipe.testrunner = 2.2
2626
zope.testing = 4.9
27+
stopit = 1.1.2
2728

2829
# Required by:
2930
# clint==0.5.1

0 commit comments

Comments
 (0)