Skip to content

Commit 1671747

Browse files
davidbrochartpre-commit-ci[bot]
andauthoredFeb 12, 2024
Drop YTest (#14)
* Drop YTest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 734f4d2 commit 1671747

File tree

3 files changed

+57
-85
lines changed

3 files changed

+57
-85
lines changed
 

‎tests/test_pycrdt_yjs.py

+36-42
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,69 @@
1+
from __future__ import annotations
2+
3+
from functools import partial
4+
15
import pytest
2-
from anyio import Event, create_task_group, move_on_after, sleep
6+
from anyio import Event, fail_after
37
from pycrdt import Array, Doc, Map
48
from websockets import connect
59

610
from pycrdt_websocket import WebsocketProvider
711

812

9-
class YTest:
10-
def __init__(self, ydoc: Doc, timeout: float = 1.0):
11-
self.ydoc = ydoc
13+
class Change:
14+
def __init__(self, event, timeout, ydata, sid, key):
15+
self.event = event
1216
self.timeout = timeout
13-
self.ydoc["_test"] = self.ytest = Map()
14-
self.clock = -1.0
17+
self.ydata = ydata
18+
self.sid = sid
19+
self.key = key
1520

16-
def run_clock(self):
17-
self.clock = max(self.clock, 0.0)
18-
self.ytest["clock"] = self.clock
21+
async def wait(self):
22+
with fail_after(self.timeout):
23+
await self.event.wait()
24+
self.ydata.unobserve(self.sid)
25+
if self.key is None:
26+
return
27+
return self.ydata[self.key]
1928

20-
async def clock_run(self):
21-
change = Event()
2229

23-
def callback(event):
24-
if "clock" in event.keys:
25-
clk = event.keys["clock"]["newValue"]
26-
if clk > self.clock:
27-
self.clock = clk + 1.0
28-
change.set()
30+
def callback(change_event, key, event):
31+
if key is None or key in event.keys:
32+
change_event.set()
2933

30-
subscription_id = self.ytest.observe(callback)
31-
async with create_task_group():
32-
with move_on_after(self.timeout):
33-
await change.wait()
3434

35-
self.ytest.unobserve(subscription_id)
35+
def watch(ydata, key: str | None = None, timeout: float = 1.0):
36+
change_event = Event()
37+
sid = ydata.observe(partial(callback, change_event, key))
38+
return Change(change_event, timeout, ydata, sid, key)
3639

3740

3841
@pytest.mark.anyio
3942
@pytest.mark.parametrize("yjs_client", "0", indirect=True)
4043
async def test_pycrdt_yjs_0(yws_server, yjs_client):
4144
ydoc = Doc()
42-
ytest = YTest(ydoc)
4345
async with connect("ws://127.0.0.1:1234/my-roomname") as websocket, WebsocketProvider(
4446
ydoc, websocket
4547
):
4648
ydoc["map"] = ymap = Map()
47-
# set a value in "in"
4849
for v_in in range(10):
4950
ymap["in"] = float(v_in)
50-
ytest.run_clock()
51-
await ytest.clock_run()
52-
v_out = ymap["out"]
51+
v_out = await watch(ymap, "out").wait()
5352
assert v_out == v_in + 1.0
5453

5554

5655
@pytest.mark.anyio
5756
@pytest.mark.parametrize("yjs_client", "1", indirect=True)
5857
async def test_pycrdt_yjs_1(yws_server, yjs_client):
59-
# wait for the JS client to connect
60-
tt, dt = 0, 0.1
61-
while True:
62-
await sleep(dt)
63-
if "/my-roomname" in yws_server.rooms:
64-
break
65-
tt += dt
66-
if tt >= 1:
67-
raise RuntimeError("Timeout waiting for client to connect")
68-
ydoc = yws_server.rooms["/my-roomname"].ydoc
69-
ytest = YTest(ydoc)
70-
ytest.run_clock()
71-
await ytest.clock_run()
58+
ydoc = Doc()
7259
ydoc["cells"] = ycells = Array()
7360
ydoc["state"] = ystate = Map()
74-
assert ycells.to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}]
75-
assert ystate.to_py() == {"state": {"dirty": False}}
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}}

‎tests/yjs_client_0.js

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
const Y = require('yjs')
22
const WebsocketProvider = require('y-websocket').WebsocketProvider
3+
const ws = require('ws')
34

45
const ydoc = new Y.Doc()
5-
const ytest = ydoc.getMap('_test')
66
const ymap = ydoc.getMap('map')
7-
const ws = require('ws')
7+
8+
function increment(resolve) {
9+
ymap.set('out', ymap.get('in') + 1);
10+
resolve();
11+
}
12+
13+
ymap.observe(event => {
14+
if (event.transaction.local || !event.changes.keys.has('in')) {
15+
return
16+
}
17+
new Promise(increment);
18+
})
819

920
const wsProvider = new WebsocketProvider(
1021
'ws://127.0.0.1:1234', 'my-roomname',
1122
ydoc,
1223
{ WebSocketPolyfill: ws }
1324
)
14-
15-
wsProvider.on('status', event => {
16-
console.log(event.status)
17-
})
18-
19-
var clock = -1
20-
21-
ytest.observe(event => {
22-
event.changes.keys.forEach((change, key) => {
23-
if (key === 'clock') {
24-
const clk = ytest.get('clock')
25-
if (clk > clock) {
26-
ymap.set('out', ymap.get('in') + 1)
27-
clock = clk + 1
28-
ytest.set('clock', clock)
29-
}
30-
}
31-
})
32-
})

‎tests/yjs_client_1.js

+8-22
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
const Y = require('yjs')
22
const WebsocketProvider = require('y-websocket').WebsocketProvider
3+
const ws = require('ws')
34

45
const ydoc = new Y.Doc()
5-
const ytest = ydoc.getMap('_test')
6-
const ycells = ydoc.getArray("cells")
7-
const ystate = ydoc.getMap("state")
8-
const ws = require('ws')
96

107
const wsProvider = new WebsocketProvider(
118
'ws://127.0.0.1:1234', 'my-roomname',
129
ydoc,
1310
{ WebSocketPolyfill: ws }
1411
)
1512

16-
wsProvider.on('status', event => {
17-
console.log(event.status)
18-
})
19-
20-
var clock = -1
13+
wsProvider.on('sync', () => {
14+
const ycells = ydoc.getArray('cells')
15+
const ystate = ydoc.getMap('state')
2116

22-
ytest.observe(event => {
23-
event.changes.keys.forEach((change, key) => {
24-
if (key === 'clock') {
25-
const clk = ytest.get('clock')
26-
if (clk > clock) {
27-
const cells = [new Y.Map([['source', new Y.Text('1 + 2')], ['metadata', {'foo': 'bar'}]])]
28-
ycells.push(cells)
29-
ystate.set('state', {'dirty': false})
30-
clock = clk + 1
31-
ytest.set('clock', clock)
32-
}
33-
}
34-
})
17+
const cells = [new Y.Map([['source', new Y.Text('1 + 2')], ['metadata', {'foo': 'bar'}]])]
18+
const state = {'dirty': false}
19+
ycells.push(cells)
20+
ystate.set('state', state)
3521
})

0 commit comments

Comments
 (0)
Please sign in to comment.