|
1 | 1 | import requests
|
2 | 2 | import pytest
|
| 3 | +import subprocess |
| 4 | +import socket |
3 | 5 | from urllib.parse import urljoin
|
4 | 6 |
|
5 | 7 |
|
| 8 | +@pytest.fixture(scope='session') |
| 9 | +def server_port(): |
| 10 | + sock = socket.socket() |
| 11 | + sock.bind(('', 0)) |
| 12 | + return sock.getsockname()[1] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope='session') |
| 16 | +def server(server_port, tmp_path_factory): |
| 17 | + tmp_dir = tmp_path_factory.mktemp("server") |
| 18 | + data_dir = tmp_dir / 'data' |
| 19 | + stderr = tmp_dir / 'server.stderr.log' |
| 20 | + command = [ |
| 21 | + 'zig-out/bin/fpindex', |
| 22 | + '--dir', str(data_dir), |
| 23 | + '--port', str(server_port), |
| 24 | + '--log-level', 'debug', |
| 25 | + ] |
| 26 | + process = subprocess.Popen( |
| 27 | + command, |
| 28 | + stdin=subprocess.DEVNULL, |
| 29 | + stdout=subprocess.DEVNULL, |
| 30 | + stderr=stderr.open('w'), |
| 31 | + ) |
| 32 | + yield |
| 33 | + process.terminate() |
| 34 | + retcode = process.wait() |
| 35 | + if retcode != 0: |
| 36 | + for line in stderr.read_text().splitlines(): |
| 37 | + print(line) |
| 38 | + |
| 39 | + |
6 | 40 | @pytest.fixture
|
7 | 41 | def index_name():
|
8 | 42 | return 'testidx'
|
@@ -36,8 +70,8 @@ def session():
|
36 | 70 |
|
37 | 71 |
|
38 | 72 | @pytest.fixture
|
39 |
| -def client(session): |
40 |
| - return Client(session, 'http://localhost:8080') |
| 73 | +def client(session, server_port, server): |
| 74 | + return Client(session, f'http://localhost:{server_port}') |
41 | 75 |
|
42 | 76 |
|
43 | 77 | @pytest.fixture(autouse=True)
|
|
0 commit comments