-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.py
More file actions
64 lines (47 loc) · 1.79 KB
/
Copy pathmodule.py
File metadata and controls
64 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import typing as t
from contextlib import asynccontextmanager
import socketio
from ellar.testing.module import Test, TestingModule
from ellar.testing.uvicorn_server import EllarUvicornServer
class RunWithServerContext:
__slots__ = ("sio", "base_url", "sio_s")
def __init__(self, sio: socketio.AsyncClient, base_url: str) -> None:
self.sio = sio
self.base_url: str = base_url
self.sio_s = [sio]
async def connect(
self,
path: str = "",
namespaces: str = "/",
socketio_path: str = "socket.io",
**kwargs: t.Any,
) -> None:
assert path == "" or path.startswith("/"), "Routed paths must start with '/'"
await self.sio.connect(
self.base_url + path,
namespaces=namespaces,
socketio_path=socketio_path,
**kwargs,
)
async def wait(self, seconds: float = 0.5) -> None:
await self.sio.sleep(seconds)
def new_socket_client_context(self) -> "RunWithServerContext":
sio = socketio.AsyncClient()
self.sio_s.append(sio)
return self.__class__(sio=sio, base_url=self.base_url)
class SocketIOTestingModule(TestingModule):
@asynccontextmanager
async def run_with_server(
self, host: str = "127.0.0.1", port: int = 4000
) -> t.AsyncIterator[RunWithServerContext]:
base_url = f"http://{host}:{port}"
server = EllarUvicornServer(app=self.create_application(), host=host, port=port)
await server.run_server()
sio = socketio.AsyncClient()
run_ctx = RunWithServerContext(sio=sio, base_url=base_url)
yield run_ctx
for item in run_ctx.sio_s:
await item.shutdown()
await server.tear_down()
class TestGateway(Test):
TESTING_MODULE = SocketIOTestingModule