|
4 | 4 |
|
5 | 5 | import pytest
|
6 | 6 | from anyio import Event, fail_after
|
| 7 | +from httpx_ws import aconnect_ws |
7 | 8 | from pycrdt import Array, Doc, Map
|
8 |
| -from websockets import connect |
| 9 | +from utils import Websocket, yjs_client |
9 | 10 |
|
10 | 11 | from pycrdt_websocket import WebsocketProvider
|
11 | 12 |
|
| 13 | +pytestmark = pytest.mark.anyio |
| 14 | + |
12 | 15 |
|
13 | 16 | class Change:
|
14 | 17 | def __init__(self, event, timeout, ydata, sid, key):
|
@@ -38,32 +41,32 @@ def watch(ydata, key: str | None = None, timeout: float = 1.0):
|
38 | 41 | return Change(change_event, timeout, ydata, sid, key)
|
39 | 42 |
|
40 | 43 |
|
41 |
| -@pytest.mark.anyio |
42 |
| -@pytest.mark.parametrize("yjs_client", "0", indirect=True) |
43 |
| -async def test_pycrdt_yjs_0(yws_server, yjs_client): |
44 |
| - ydoc = Doc() |
45 |
| - async with connect("ws://127.0.0.1:1234/my-roomname") as websocket, WebsocketProvider( |
46 |
| - ydoc, websocket |
47 |
| - ): |
48 |
| - ydoc["map"] = ymap = Map() |
49 |
| - for v_in in range(10): |
50 |
| - ymap["in"] = float(v_in) |
51 |
| - v_out = await watch(ymap, "out").wait() |
52 |
| - assert v_out == v_in + 1.0 |
| 44 | +async def test_pycrdt_yjs_0(yws_server): |
| 45 | + port = yws_server |
| 46 | + with yjs_client(0, port): |
| 47 | + ydoc = Doc() |
| 48 | + async with aconnect_ws( |
| 49 | + f"http://localhost:{port}/my-roomname" |
| 50 | + ) as websocket, WebsocketProvider(ydoc, Websocket(websocket, "my-roomname")): |
| 51 | + ydoc["map"] = ymap = Map() |
| 52 | + for v_in in range(10): |
| 53 | + ymap["in"] = float(v_in) |
| 54 | + v_out = await watch(ymap, "out").wait() |
| 55 | + assert v_out == v_in + 1.0 |
53 | 56 |
|
54 | 57 |
|
55 |
| -@pytest.mark.anyio |
56 |
| -@pytest.mark.parametrize("yjs_client", "1", indirect=True) |
57 |
| -async def test_pycrdt_yjs_1(yws_server, yjs_client): |
58 |
| - ydoc = Doc() |
59 |
| - ydoc["cells"] = ycells = Array() |
60 |
| - ydoc["state"] = ystate = Map() |
61 |
| - ycells_change = watch(ycells) |
62 |
| - ystate_change = watch(ystate) |
63 |
| - async with connect("ws://127.0.0.1:1234/my-roomname") as websocket, WebsocketProvider( |
64 |
| - ydoc, websocket |
65 |
| - ): |
66 |
| - await ycells_change.wait() |
67 |
| - await ystate_change.wait() |
68 |
| - assert ycells.to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}] |
69 |
| - assert ystate.to_py() == {"state": {"dirty": False}} |
| 58 | +async def test_pycrdt_yjs_1(yws_server): |
| 59 | + port = yws_server |
| 60 | + with yjs_client(1, port): |
| 61 | + ydoc = Doc() |
| 62 | + ydoc["cells"] = ycells = Array() |
| 63 | + ydoc["state"] = ystate = Map() |
| 64 | + ycells_change = watch(ycells) |
| 65 | + ystate_change = watch(ystate) |
| 66 | + async with aconnect_ws( |
| 67 | + f"http://localhost:{port}/my-roomname" |
| 68 | + ) as websocket, WebsocketProvider(ydoc, Websocket(websocket, "my-roomname")): |
| 69 | + await ycells_change.wait() |
| 70 | + await ystate_change.wait() |
| 71 | + assert ycells.to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}] |
| 72 | + assert ystate.to_py() == {"state": {"dirty": False}} |
0 commit comments