Skip to content

Commit e8cd596

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

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import httpx
2+
import pytest
3+
import asyncio
4+
5+
async def make_req(port):
6+
async with httpx.AsyncClient() as client:
7+
return await client.post(f"http://localhost:{port}/echo", content="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+
for _ in range(100):
19+
ok = 0
20+
timeout = 0
21+
for res in await asyncio.gather(*[make_req(port) for _ in range(3)], return_exceptions=True):
22+
if isinstance(res, Exception):
23+
timeout += 1
24+
else:
25+
assert res.status_code == 200
26+
ok += 1
27+
assert (timeout, ok) == (0, 3)

0 commit comments

Comments
 (0)