1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
import pytest
import threading
import random
import logging
from contextlib import contextmanager
from wsgiref.simple_server import make_server
from wsgiref.simple_server import WSGIRequestHandler
from wsgiref.simple_server import WSGIServer
from wsgiref.simple_server import ServerHandler
log = logging.getLogger(__name__)
ServerHandler.handle_error = lambda: None
class QuietHandler(WSGIRequestHandler):
def log_request(self, *args):
pass
class QuietServer(WSGIServer):
def handle_error(self, req, addr):
pass
def _make_test_server(app):
maxport = ((1 << 16) - 1)
# we'll make 3 attempts to find a free port
for i in range(3, 0, -1):
try:
port = random.randint(maxport // 2, maxport)
server = make_server(
'localhost',
port,
app,
server_class=QuietServer,
handler_class=QuietHandler,
)
server.timeout = 5
return server
except:
if i == 1:
raise
@pytest.fixture
def serve():
@contextmanager
def _serve(app):
server = _make_test_server(app)
try:
worker = threading.Thread(target=server.serve_forever)
worker.setDaemon(True)
worker.start()
server.url = "http://localhost:%d" % server.server_port
log.debug("server started on %s", server.url)
yield server
finally:
log.debug("shutting server down")
server.shutdown()
worker.join(1)
if worker.isAlive():
log.warning('worker is hanged')
else:
log.debug("server stopped")
return _serve
|