Skip to content

Commit ee4fdcc

Browse files
committed
Review tests server ctxmanager
1 parent 738f02a commit ee4fdcc

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

granian/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def serve(
509509

510510
if setproctitle is not None:
511511
self.process_name = self.process_name or (
512-
f'granian {self.interface.value} {self.bind_addr}:{self.bind_port} {self.target}'
512+
f'granian {self.interface} {self.bind_addr}:{self.bind_port} {self.target}'
513513
)
514514
setproctitle.setproctitle(self.process_name)
515515
elif self.process_name is not None:

tests/conftest.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import multiprocessing as mp
23
import os
34
import socket
45
from contextlib import asynccontextmanager, closing
@@ -7,26 +8,32 @@
78

89
import pytest
910

11+
from granian import Granian
12+
13+
14+
def _serve(**kwargs):
15+
server = Granian(f'tests.apps.{kwargs["interface"]}:app', **kwargs)
16+
server.serve()
17+
1018

1119
@asynccontextmanager
1220
async def _server(interface, port, threading_mode, tls=False):
1321
certs_path = Path.cwd() / 'tests' / 'fixtures' / 'tls'
14-
tls_opts = (
15-
(f"--ssl-certificate {certs_path / 'cert.pem'} " f"--ssl-keyfile {certs_path / 'key.pem'} ") if tls else ''
16-
)
17-
cmd = ''.join(
18-
[
19-
f'granian --interface {interface} --port {port} ',
20-
f'--threads 1 --threading-mode {threading_mode} ',
21-
tls_opts,
22-
'--opt ' if os.getenv('LOOP_OPT') else '',
23-
f'tests.apps.{interface}:app',
24-
]
25-
)
22+
kwargs = {
23+
'interface': interface,
24+
'port': port,
25+
'threading_mode': threading_mode,
26+
'loop_opt': bool(os.getenv('LOOP_OPT')),
27+
}
28+
if tls:
29+
kwargs['ssl_cert'] = certs_path / 'cert.pem'
30+
kwargs['ssl_key'] = certs_path / 'key.pem'
2631

2732
succeeded, spawn_failures = False, 0
2833
while spawn_failures < 3:
29-
proc = await asyncio.create_subprocess_shell(cmd, env=dict(os.environ))
34+
proc = mp.get_context('spawn').Process(target=_serve, kwargs=kwargs)
35+
proc.start()
36+
3037
conn_failures = 0
3138
while conn_failures < 3:
3239
try:
@@ -41,7 +48,7 @@ async def _server(interface, port, threading_mode, tls=False):
4148
break
4249

4350
proc.terminate()
44-
await proc.wait()
51+
proc.join()
4552
spawn_failures += 1
4653

4754
if not succeeded:
@@ -51,7 +58,7 @@ async def _server(interface, port, threading_mode, tls=False):
5158
yield port
5259
finally:
5360
proc.terminate()
54-
await proc.wait()
61+
proc.join()
5562

5663

5764
@pytest.fixture(scope='function')

0 commit comments

Comments
 (0)