From 7591bcab59dd244825404685560b38658a2c6713 Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:27:55 +0100 Subject: [PATCH 1/2] Fix use of traitlets to configure YStore class --- projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py b/projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py index 4527786e..e1df345d 100644 --- a/projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py +++ b/projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +from functools import partial from typing import Literal from jupyter_server.extension.application import ExtensionApp @@ -96,13 +97,12 @@ def initialize_handlers(self): page_config.setdefault("serverSideExecution", self.server_side_execution) # Set configurable parameters to YStore class - for k, v in self.config.get(self.ystore_class.__name__, {}).items(): - setattr(self.ystore_class, k, v) + ystore_class = partial(self.ystore_class, config=self.config) self.ywebsocket_server = JupyterWebsocketServer( rooms_ready=False, auto_clean_rooms=False, - ystore_class=self.ystore_class, + ystore_class=ystore_class, # Log exceptions, because we don't want the websocket server # to _ever_ crash permanently in a live jupyter_server. exception_handler=exception_logger, @@ -125,7 +125,7 @@ def initialize_handlers(self): "document_cleanup_delay": self.document_cleanup_delay, "document_save_delay": self.document_save_delay, "file_loaders": self.file_loaders, - "ystore_class": self.ystore_class, + "ystore_class": ystore_class, "ywebsocket_server": self.ywebsocket_server, }, ), From 5bb041f8b5aa5978ef5a421e338b21a0656c041c Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:47:24 +0100 Subject: [PATCH 2/2] Add test case, apply fix in the fixture too Co-authored-by: asteppke --- tests/conftest.py | 13 +++++++------ tests/test_app.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index f9df43c2..4c1e1821 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -169,13 +169,9 @@ async def _inner(format: str, type: str, path: str) -> None: return _inner -@pytest.fixture -def rtc_create_SQLite_store(jp_serverapp): - for k, v in jp_serverapp.config.get("SQLiteYStore").items(): - setattr(SQLiteYStore, k, v) - +def rtc_create_SQLite_store_factory(jp_serverapp): async def _inner(type: str, path: str, content: str) -> DocumentRoom: - db = SQLiteYStore(path=f"{type}:{path}") + db = SQLiteYStore(path=f"{type}:{path}", config=jp_serverapp.config) task = create_task(db.start()) await db.started.wait() @@ -192,6 +188,11 @@ async def _inner(type: str, path: str, content: str) -> DocumentRoom: return _inner +@pytest.fixture +def rtc_create_SQLite_store(jp_serverapp): + return rtc_create_SQLite_store_factory(jp_serverapp) + + @pytest.fixture def rtc_create_mock_document_room(): def _inner( diff --git a/tests/test_app.py b/tests/test_app.py index 3288311e..82067a1d 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -6,6 +6,8 @@ import pytest from jupyter_server_ydoc.stores import SQLiteYStore, TempFileYStore +from .conftest import rtc_create_SQLite_store_factory + def test_default_settings(jp_serverapp): settings = jp_serverapp.web_app.settings["jupyter_server_ydoc_config"] @@ -62,6 +64,19 @@ def test_settings_should_change_ystore_class(jp_configurable_serverapp): assert settings["ystore_class"] == TempFileYStore +async def test_document_ttl_from_settings(rtc_create_mock_document_room, jp_configurable_serverapp): + argv = ["--SQLiteYStore.document_ttl=3600"] + + app = jp_configurable_serverapp(argv=argv) + + id = "test-id" + content = "test_ttl" + rtc_create_SQLite_store = rtc_create_SQLite_store_factory(app) + store = await rtc_create_SQLite_store("file", id, content) + + assert store.document_ttl == 3600 + + @pytest.mark.parametrize("copy", [True, False]) async def test_get_document_file(rtc_create_file, jp_serverapp, copy): path, content = await rtc_create_file("test.txt", "test", store=True)