Skip to content

Commit b9aed05

Browse files
committed
Adds general tests
1 parent 3364a94 commit b9aed05

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ async def _inner(
9090
# Create a notebook string and write to file.
9191
if content is None:
9292
nb = nbformat.v4.new_notebook()
93+
notary = nbformat.sign.NotebookNotary()
94+
notary.sign(nb)
9395
content = nbformat.writes(nb, version=4)
9496

9597
nbpath.write_text(content)

tests/test_general.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
4+
from __future__ import annotations
5+
6+
from asyncio import create_task, sleep, wait
7+
8+
import nbformat
9+
from jupyter_ydoc import YNotebook, YUnicode
10+
from ypy_websocket import WebsocketProvider
11+
12+
13+
async def test_thousand_clients(rtc_create_file, rtc_connect_doc_client):
14+
file_path = "test.txt"
15+
file_format = "text"
16+
file_type = "file"
17+
await rtc_create_file(file_path)
18+
19+
async def fn(format: str, type: str, path: str, doc: YUnicode, content: int | None = None):
20+
ydoc = doc.ydoc
21+
test_array = ydoc.get_array("test")
22+
23+
async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider(
24+
ydoc, ws
25+
):
26+
if content is not None:
27+
with ydoc.begin_transaction() as txn:
28+
test_array.extend(txn, [content])
29+
await sleep(0.2)
30+
31+
clients = []
32+
for i in range(1000):
33+
doc = YUnicode()
34+
clients.append(create_task(fn(file_format, file_type, file_path, doc, 1)))
35+
36+
doc = YUnicode()
37+
test_array = doc.ydoc.get_array("test")
38+
clients.append(create_task(fn(file_format, file_type, file_path, doc)))
39+
40+
await wait(clients)
41+
42+
assert sum(list(test_array)) == 1000
43+
44+
45+
async def test_thousand_clients_insert_text(rtc_create_file, rtc_connect_doc_client):
46+
file_path = "test.txt"
47+
file_format = "text"
48+
file_type = "file"
49+
await rtc_create_file(file_path)
50+
51+
async def fn(format: str, type: str, path: str, doc: YUnicode, content: str | None = None):
52+
async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider(
53+
doc.ydoc, ws
54+
):
55+
if content is not None:
56+
with doc.ydoc.begin_transaction() as txn:
57+
doc._ysource.extend(txn, content)
58+
59+
await sleep(0.2)
60+
61+
n = 1000
62+
content = "test"
63+
res = len(content) * n
64+
65+
clients = []
66+
for i in range(n):
67+
doc = YUnicode()
68+
clients.append(create_task(fn(file_format, file_type, file_path, doc, content)))
69+
70+
doc = YUnicode()
71+
clients.append(create_task(fn(file_format, file_type, file_path, doc)))
72+
73+
await wait(clients)
74+
75+
assert len(doc._ysource) == res
76+
77+
78+
async def test_hundred_clients_insert_cell(rtc_create_notebook, rtc_connect_doc_client):
79+
file_path = "test.ipynb"
80+
file_format = "json"
81+
file_type = "notebook"
82+
await rtc_create_notebook(file_path)
83+
84+
async def fn(format: str, type: str, path: str, doc: YNotebook, content: str | None = ""):
85+
async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider(
86+
doc.ydoc, ws
87+
):
88+
if content is not None:
89+
doc.append_cell(nbformat.v4.new_code_cell(content))
90+
await sleep(0.2)
91+
92+
n = 100
93+
clients = []
94+
for i in range(n):
95+
doc = YNotebook()
96+
clients.append(create_task(fn(file_format, file_type, file_path, doc, "test")))
97+
98+
doc = YNotebook()
99+
clients.append(create_task(fn(file_format, file_type, file_path, doc, None)))
100+
101+
await wait(clients)
102+
103+
# +1 For the initial cell :(
104+
assert doc.cell_number == n + 1

0 commit comments

Comments
 (0)