Skip to content

Commit d53a3bd

Browse files
committed
add tests for granian lock on windows platform using multiple workers
1 parent 8a0224d commit d53a3bd

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@asynccontextmanager
13-
async def _server(interface, port, threading_mode, tls=False):
13+
async def _server(interface, port, threading_mode, tls=False, workers=1):
1414
certs_path = Path.cwd() / "tests" / "fixtures" / "tls"
1515
tls_opts = (
1616
f"--ssl-certificate {certs_path / 'cert.pem'} "
@@ -20,6 +20,7 @@ async def _server(interface, port, threading_mode, tls=False):
2020
"".join([
2121
f"granian --interface {interface} --port {port} ",
2222
f"--threads 1 --threading-mode {threading_mode} ",
23+
f"--workers {workers} ",
2324
tls_opts,
2425
f"tests.apps.{interface}:app"
2526
]),

tests/test_lock.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import httpx
2+
import pytest
3+
from concurrent.futures import ProcessPoolExecutor
4+
import asyncio
5+
6+
def make_req(port):
7+
return httpx.post(f"http://localhost:{port}/echo", data="test")
8+
9+
@pytest.mark.asyncio
10+
@pytest.mark.parametrize("threading_mode", ["runtime", "workers"])
11+
@pytest.mark.parametrize("server_mode", ["wsgi", "asgi", "rsgi"])
12+
async def test_lock(wsgi_server, asgi_server, rsgi_server, threading_mode, server_mode):
13+
server = {"wsgi": wsgi_server,
14+
"asgi": asgi_server,
15+
"rsgi": rsgi_server,
16+
}[server_mode]
17+
async with server(threading_mode, workers=2) as port:
18+
with ProcessPoolExecutor() as executor:
19+
for _ in range(100):
20+
futures = [executor.submit(make_req, port) for _ in range(3)]
21+
ok = 0
22+
timeout = 0
23+
for future in futures:
24+
try:
25+
res = await asyncio.wrap_future(future)
26+
assert res.status_code == 200
27+
ok += 1
28+
except httpx.ReadTimeout:
29+
timeout += 1
30+
assert (timeout, ok) == (0, 3)

0 commit comments

Comments
 (0)