-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathconftest.py
72 lines (58 loc) · 1.66 KB
/
conftest.py
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
69
70
71
72
from contextlib import contextmanager
import logging
import random
import threading
from wsgiref.simple_server import (
ServerHandler,
WSGIRequestHandler,
WSGIServer,
make_server,
)
import pytest
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 BaseException:
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.daemon = 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.is_alive():
log.warning("worker is hanged")
else:
log.debug("server stopped")
return _serve