1
1
import asyncio
2
+ import multiprocessing as mp
2
3
import os
3
4
import socket
4
5
from contextlib import asynccontextmanager , closing
7
8
8
9
import pytest
9
10
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
+
10
18
11
19
@asynccontextmanager
12
20
async def _server (interface , port , threading_mode , tls = False ):
13
21
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'
26
31
27
32
succeeded , spawn_failures = False , 0
28
33
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
+
30
37
conn_failures = 0
31
38
while conn_failures < 3 :
32
39
try :
@@ -41,7 +48,7 @@ async def _server(interface, port, threading_mode, tls=False):
41
48
break
42
49
43
50
proc .terminate ()
44
- await proc .wait ()
51
+ proc .join ()
45
52
spawn_failures += 1
46
53
47
54
if not succeeded :
@@ -51,7 +58,7 @@ async def _server(interface, port, threading_mode, tls=False):
51
58
yield port
52
59
finally :
53
60
proc .terminate ()
54
- await proc .wait ()
61
+ proc .join ()
55
62
56
63
57
64
@pytest .fixture (scope = 'function' )
0 commit comments