Skip to content

Commit

Permalink
Tests: Remove arbitrary sleeping for waiting for the webserver to start
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Mar 25, 2021
1 parent d646784 commit 03c050a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 27 additions & 1 deletion src/crate/client/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import json
import os
import socket
import unittest
import doctest
from pprint import pprint
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions versions.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 03c050a

Please sign in to comment.