@@ -131,3 +131,87 @@ async def my_listener(logger: EventLogger, schema_id: str, data: dict) -> None:
131131 assert collected_data [1 ]["action" ] == "leave"
132132 assert collected_data [1 ]["roomid" ] == "text:file:" + fim .get_id ("test.txt" )
133133 assert collected_data [1 ]["username" ] is not None
134+
135+
136+ async def test_room_handler_doc_client_should_cleanup_room_file (
137+ rtc_create_file , rtc_connect_doc_client , jp_serverapp
138+ ):
139+ path , _ = await rtc_create_file ("test.txt" , "test" )
140+
141+ event = Event ()
142+
143+ def _on_document_change (target : str , e : Any ) -> None :
144+ if target == "source" :
145+ event .set ()
146+
147+ doc = YUnicode ()
148+ doc .observe (_on_document_change )
149+
150+ async with await rtc_connect_doc_client ("text" , "file" , path ) as ws , WebsocketProvider (
151+ doc .ydoc , ws
152+ ):
153+ await event .wait ()
154+ await sleep (0.1 )
155+
156+ # kill websocketserver to mimic task group inactive failure
157+ await jp_serverapp .web_app .settings ["jupyter_server_ydoc" ].ywebsocket_server .stop ()
158+
159+ listener_was_called = False
160+ collected_data = []
161+
162+ async def my_listener (logger : EventLogger , schema_id : str , data : dict ) -> None :
163+ nonlocal listener_was_called
164+ collected_data .append (data )
165+ listener_was_called = True
166+
167+ event_logger = jp_serverapp .event_logger
168+ event_logger .add_listener (
169+ schema_id = "https://schema.jupyter.org/jupyter_collaboration/session/v1" ,
170+ listener = my_listener ,
171+ )
172+
173+ path2 , _ = await rtc_create_file ("test2.txt" , "test2" )
174+
175+ try :
176+ async with await rtc_connect_doc_client ("text2" , "file2" , path2 ) as ws , WebsocketProvider (
177+ doc .ydoc , ws
178+ ):
179+ await event .wait ()
180+ await sleep (0.1 )
181+ except Exception :
182+ pass
183+
184+ try :
185+ async with await rtc_connect_doc_client ("text2" , "file2" , path2 ) as ws , WebsocketProvider (
186+ doc .ydoc , ws
187+ ):
188+ await event .wait ()
189+ await sleep (0.1 )
190+ except Exception :
191+ pass
192+
193+ fim = jp_serverapp .web_app .settings ["file_id_manager" ]
194+
195+ assert listener_was_called is True
196+ assert len (collected_data ) == 4
197+ # no two collaboration events are emitted.
198+ # [{'level': 'WARNING', 'msg': 'There is another collaborative session accessing the same file.\nThe synchronization bet...ou might lose some of your changes.', 'path': 'test2.txt', 'room': 'text2:file2:51b7e24f-f534-47fb-882f-5cc45ba867fe'}]
199+ assert collected_data [0 ]["path" ] == "test2.txt"
200+ assert collected_data [0 ]["room" ] == "text2:file2:" + fim .get_id ("test2.txt" )
201+ assert collected_data [0 ]["action" ] == "clean"
202+ assert collected_data [0 ]["msg" ] == "Room deleted."
203+ assert collected_data [1 ]["path" ] == "test2.txt"
204+ assert collected_data [1 ]["room" ] == "text2:file2:" + fim .get_id ("test2.txt" )
205+ assert collected_data [1 ]["action" ] == "clean"
206+ assert collected_data [1 ]["msg" ] == "file loader removed."
207+ assert collected_data [2 ]["path" ] == "test2.txt"
208+ assert collected_data [2 ]["room" ] == "text2:file2:" + fim .get_id ("test2.txt" )
209+ assert collected_data [2 ]["action" ] == "clean"
210+ assert collected_data [2 ]["msg" ] == "Room deleted."
211+ assert collected_data [3 ]["path" ] == "test2.txt"
212+ assert collected_data [3 ]["room" ] == "text2:file2:" + fim .get_id ("test2.txt" )
213+ assert collected_data [3 ]["action" ] == "clean"
214+ assert collected_data [3 ]["msg" ] == "file loader removed."
215+
216+ await jp_serverapp .web_app .settings ["jupyter_server_ydoc" ].stop_extension ()
217+ del jp_serverapp .web_app .settings ["file_id_manager" ]
0 commit comments