Skip to content

Commit

Permalink
Adds general tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hbcarlos committed Oct 18, 2023
1 parent 3364a94 commit b9aed05
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ async def _inner(
# Create a notebook string and write to file.
if content is None:
nb = nbformat.v4.new_notebook()
notary = nbformat.sign.NotebookNotary()
notary.sign(nb)
content = nbformat.writes(nb, version=4)

nbpath.write_text(content)
Expand Down
104 changes: 104 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import annotations

from asyncio import create_task, sleep, wait

import nbformat
from jupyter_ydoc import YNotebook, YUnicode
from ypy_websocket import WebsocketProvider


async def test_thousand_clients(rtc_create_file, rtc_connect_doc_client):
file_path = "test.txt"
file_format = "text"
file_type = "file"
await rtc_create_file(file_path)

async def fn(format: str, type: str, path: str, doc: YUnicode, content: int | None = None):
ydoc = doc.ydoc
test_array = ydoc.get_array("test")

async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider(
ydoc, ws
):
if content is not None:
with ydoc.begin_transaction() as txn:
test_array.extend(txn, [content])
await sleep(0.2)

clients = []
for i in range(1000):
doc = YUnicode()
clients.append(create_task(fn(file_format, file_type, file_path, doc, 1)))

doc = YUnicode()
test_array = doc.ydoc.get_array("test")
clients.append(create_task(fn(file_format, file_type, file_path, doc)))

await wait(clients)

assert sum(list(test_array)) == 1000


async def test_thousand_clients_insert_text(rtc_create_file, rtc_connect_doc_client):
file_path = "test.txt"
file_format = "text"
file_type = "file"
await rtc_create_file(file_path)

async def fn(format: str, type: str, path: str, doc: YUnicode, content: str | None = None):
async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider(
doc.ydoc, ws
):
if content is not None:
with doc.ydoc.begin_transaction() as txn:
doc._ysource.extend(txn, content)

await sleep(0.2)

n = 1000
content = "test"
res = len(content) * n

clients = []
for i in range(n):
doc = YUnicode()
clients.append(create_task(fn(file_format, file_type, file_path, doc, content)))

doc = YUnicode()
clients.append(create_task(fn(file_format, file_type, file_path, doc)))

await wait(clients)

assert len(doc._ysource) == res


async def test_hundred_clients_insert_cell(rtc_create_notebook, rtc_connect_doc_client):
file_path = "test.ipynb"
file_format = "json"
file_type = "notebook"
await rtc_create_notebook(file_path)

async def fn(format: str, type: str, path: str, doc: YNotebook, content: str | None = ""):
async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider(
doc.ydoc, ws
):
if content is not None:
doc.append_cell(nbformat.v4.new_code_cell(content))
await sleep(0.2)

n = 100
clients = []
for i in range(n):
doc = YNotebook()
clients.append(create_task(fn(file_format, file_type, file_path, doc, "test")))

doc = YNotebook()
clients.append(create_task(fn(file_format, file_type, file_path, doc, None)))

await wait(clients)

# +1 For the initial cell :(
assert doc.cell_number == n + 1

0 comments on commit b9aed05

Please sign in to comment.