|
| 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_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( |
| 20 | + format: str, type: str, path: str, doc: YUnicode, content: int | None = None |
| 21 | + ) -> None: |
| 22 | + ydoc = doc.ydoc |
| 23 | + test_array = ydoc.get_array("test") |
| 24 | + |
| 25 | + async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider( |
| 26 | + ydoc, ws |
| 27 | + ): |
| 28 | + if content is not None: |
| 29 | + with ydoc.begin_transaction() as txn: |
| 30 | + test_array.extend(txn, [content]) |
| 31 | + await sleep(0.2) |
| 32 | + |
| 33 | + clients = [] |
| 34 | + n = 10 |
| 35 | + for _ in range(n): |
| 36 | + doc = YUnicode() |
| 37 | + clients.append(create_task(fn(file_format, file_type, file_path, doc, 1))) |
| 38 | + |
| 39 | + doc = YUnicode() |
| 40 | + test_array = doc.ydoc.get_array("test") |
| 41 | + clients.append(create_task(fn(file_format, file_type, file_path, doc))) |
| 42 | + |
| 43 | + await wait(clients) |
| 44 | + |
| 45 | + assert sum(list(test_array)) == n |
| 46 | + |
| 47 | + |
| 48 | +async def test_clients_insert_text(rtc_create_file, rtc_connect_doc_client): |
| 49 | + file_path = "test.txt" |
| 50 | + file_format = "text" |
| 51 | + file_type = "file" |
| 52 | + await rtc_create_file(file_path) |
| 53 | + |
| 54 | + async def fn( |
| 55 | + format: str, type: str, path: str, doc: YUnicode, content: str | None = None |
| 56 | + ) -> None: |
| 57 | + async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider( |
| 58 | + doc.ydoc, ws |
| 59 | + ): |
| 60 | + if content is not None: |
| 61 | + with doc.ydoc.begin_transaction() as txn: |
| 62 | + doc._ysource.extend(txn, content) |
| 63 | + |
| 64 | + await sleep(0.2) |
| 65 | + |
| 66 | + n = 10 |
| 67 | + content = "test" |
| 68 | + res = len(content) * n |
| 69 | + |
| 70 | + clients = [] |
| 71 | + for _ in range(n): |
| 72 | + doc = YUnicode() |
| 73 | + clients.append(create_task(fn(file_format, file_type, file_path, doc, content))) |
| 74 | + |
| 75 | + doc = YUnicode() |
| 76 | + clients.append(create_task(fn(file_format, file_type, file_path, doc))) |
| 77 | + |
| 78 | + await wait(clients) |
| 79 | + |
| 80 | + assert len(doc._ysource) == res |
| 81 | + |
| 82 | + |
| 83 | +async def test_clients_insert_cell(rtc_create_notebook, rtc_connect_doc_client): |
| 84 | + file_path = "test.ipynb" |
| 85 | + file_format = "json" |
| 86 | + file_type = "notebook" |
| 87 | + await rtc_create_notebook(file_path) |
| 88 | + |
| 89 | + async def fn( |
| 90 | + format: str, type: str, path: str, doc: YNotebook, content: str | None = "" |
| 91 | + ) -> None: |
| 92 | + async with await rtc_connect_doc_client(format, type, path) as ws, WebsocketProvider( |
| 93 | + doc.ydoc, ws |
| 94 | + ): |
| 95 | + if content is not None: |
| 96 | + doc.append_cell(nbformat.v4.new_code_cell(content)) |
| 97 | + await sleep(0.2) |
| 98 | + |
| 99 | + n = 10 |
| 100 | + clients = [] |
| 101 | + for _ in range(n): |
| 102 | + doc = YNotebook() |
| 103 | + clients.append(create_task(fn(file_format, file_type, file_path, doc, "test"))) |
| 104 | + |
| 105 | + doc = YNotebook() |
| 106 | + clients.append(create_task(fn(file_format, file_type, file_path, doc, None))) |
| 107 | + |
| 108 | + await wait(clients) |
| 109 | + |
| 110 | + # +1 For the initial cell :( |
| 111 | + assert doc.cell_number == n + 1 |
0 commit comments