-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathserverfixtures.py
More file actions
52 lines (34 loc) · 1.18 KB
/
serverfixtures.py
File metadata and controls
52 lines (34 loc) · 1.18 KB
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
import socket
from multiprocessing import Process
from time import sleep
import pytest
from flask import Flask
SERVER_PORT = 44777
BASE_URL = f"http://127.0.0.1:{SERVER_PORT}"
app = Flask(__name__, static_url_path="", static_folder="res")
def start_server():
app.run(host="127.0.0.1", port=44777)
def _wait_for_server(host, port, timeout=10.0):
import time
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
with socket.create_connection((host, port), timeout=0.1):
return
except OSError:
time.sleep(0.05)
raise RuntimeError(f"Server on {host}:{port} did not start within {timeout}s")
def _start_server_process(target):
server_process = Process(target=target)
server_process.start()
_wait_for_server("127.0.0.1", SERVER_PORT)
if not server_process.is_alive():
raise TypeError("The server process did not start!")
yield 1
sleep(1.2)
server_process.terminate()
@pytest.fixture(scope="module", autouse=True)
def test_server():
yield from _start_server_process(start_server)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=44778, debug=True)