@@ -113,6 +113,24 @@ async def cleanup(self):
113113 time .sleep (0.1 )
114114
115115
116+ @pytest .fixture
117+ def session_is_ready (jp_serverapp ):
118+ """Wait for the kernel started by a session to be ready.
119+
120+ This is useful when working with pending kernels.
121+ """
122+
123+ async def _ (session_id ):
124+ sm = jp_serverapp .session_manager
125+ mkm = jp_serverapp .kernel_manager
126+ session = await sm .get_session (session_id = session_id )
127+ kernel_id = session ["kernel" ]["id" ]
128+ kernel = mkm .get_kernel (kernel_id )
129+ await kernel .ready
130+
131+ return _
132+
133+
116134@pytest .fixture
117135def session_client (jp_root_dir , jp_fetch ):
118136 subdir = jp_root_dir .joinpath ("foo" )
@@ -156,7 +174,9 @@ def assert_session_equality(actual, expected):
156174 assert_kernel_equality (actual ["kernel" ], expected ["kernel" ])
157175
158176
159- async def test_create (session_client , jp_base_url , jp_cleanup_subprocesses , jp_serverapp ):
177+ async def test_create (
178+ session_client , jp_base_url , jp_cleanup_subprocesses , jp_serverapp , session_is_ready
179+ ):
160180 # Make sure no sessions exist.
161181 resp = await session_client .list ()
162182 sessions = j (resp )
@@ -197,7 +217,6 @@ async def test_create(session_client, jp_base_url, jp_cleanup_subprocesses, jp_s
197217 assert_session_equality (got , new_session )
198218
199219 # Need to find a better solution to this.
200- await session_client .cleanup ()
201220 await jp_cleanup_subprocesses ()
202221
203222
@@ -218,7 +237,6 @@ async def test_create_bad(
218237 await session_client .create ("foo/nb1.ipynb" )
219238
220239 # Need to find a better solution to this.
221- await session_client .cleanup ()
222240 await jp_cleanup_subprocesses ()
223241
224242
@@ -251,7 +269,6 @@ async def test_create_bad_pending(
251269 assert "non_existent_path" in session ["kernel" ]["reason" ]
252270
253271 # Need to find a better solution to this.
254- await session_client .cleanup ()
255272 await jp_cleanup_subprocesses ()
256273
257274
@@ -261,7 +278,6 @@ async def test_create_file_session(session_client, jp_cleanup_subprocesses, jp_s
261278 newsession = j (resp )
262279 assert newsession ["path" ] == "foo/nb1.py"
263280 assert newsession ["type" ] == "file"
264- await session_client .cleanup ()
265281 await jp_cleanup_subprocesses ()
266282
267283
@@ -272,7 +288,6 @@ async def test_create_console_session(session_client, jp_cleanup_subprocesses, j
272288 assert newsession ["path" ] == "foo/abc123"
273289 assert newsession ["type" ] == "console"
274290 # Need to find a better solution to this.
275- await session_client .cleanup ()
276291 await jp_cleanup_subprocesses ()
277292
278293
@@ -284,7 +299,6 @@ async def test_create_deprecated(session_client, jp_cleanup_subprocesses, jp_ser
284299 assert newsession ["type" ] == "notebook"
285300 assert newsession ["notebook" ]["path" ] == "foo/nb1.ipynb"
286301 # Need to find a better solution to this.
287- await session_client .cleanup ()
288302 await jp_cleanup_subprocesses ()
289303
290304
@@ -316,7 +330,6 @@ async def test_create_with_kernel_id(
316330 got = j (resp )
317331 assert_session_equality (got , new_session )
318332 # Need to find a better solution to this.
319- await session_client .cleanup ()
320333 await jp_cleanup_subprocesses ()
321334
322335
@@ -327,14 +340,15 @@ async def test_create_with_bad_kernel_id(session_client, jp_cleanup_subprocesses
327340 # TODO
328341 assert newsession ["path" ] == "foo/nb1.py"
329342 assert newsession ["type" ] == "file"
330- await session_client .cleanup ()
331343 await jp_cleanup_subprocesses ()
332344
333345
334- async def test_delete (session_client , jp_cleanup_subprocesses , jp_serverapp ):
346+ async def test_delete (session_client , jp_cleanup_subprocesses , jp_serverapp , session_is_ready ):
335347 resp = await session_client .create ("foo/nb1.ipynb" )
348+
336349 newsession = j (resp )
337350 sid = newsession ["id" ]
351+ await session_is_ready (sid )
338352
339353 resp = await session_client .delete (sid )
340354 assert resp .code == 204
@@ -347,7 +361,6 @@ async def test_delete(session_client, jp_cleanup_subprocesses, jp_serverapp):
347361 await session_client .get (sid )
348362 assert expected_http_error (e , 404 )
349363 # Need to find a better solution to this.
350- await session_client .cleanup ()
351364 await jp_cleanup_subprocesses ()
352365
353366
@@ -361,7 +374,6 @@ async def test_modify_path(session_client, jp_cleanup_subprocesses, jp_serverapp
361374 assert changed ["id" ] == sid
362375 assert changed ["path" ] == "nb2.ipynb"
363376 # Need to find a better solution to this.
364- await session_client .cleanup ()
365377 await jp_cleanup_subprocesses ()
366378
367379
@@ -375,7 +387,6 @@ async def test_modify_path_deprecated(session_client, jp_cleanup_subprocesses, j
375387 assert changed ["id" ] == sid
376388 assert changed ["notebook" ]["path" ] == "nb2.ipynb"
377389 # Need to find a better solution to this.
378- await session_client .cleanup ()
379390 await jp_cleanup_subprocesses ()
380391
381392
@@ -389,7 +400,6 @@ async def test_modify_type(session_client, jp_cleanup_subprocesses, jp_serverapp
389400 assert changed ["id" ] == sid
390401 assert changed ["type" ] == "console"
391402 # Need to find a better solution to this.
392- await session_client .cleanup ()
393403 await jp_cleanup_subprocesses ()
394404
395405
@@ -414,7 +424,6 @@ async def test_modify_kernel_name(session_client, jp_fetch, jp_cleanup_subproces
414424 assert kernel_list == [after ["kernel" ]]
415425
416426 # Need to find a better solution to this.
417- await session_client .cleanup ()
418427 await jp_cleanup_subprocesses ()
419428
420429
@@ -446,7 +455,6 @@ async def test_modify_kernel_id(session_client, jp_fetch, jp_cleanup_subprocesse
446455 assert kernel_list == [kernel ]
447456
448457 # Need to find a better solution to this.
449- await session_client .cleanup ()
450458 await jp_cleanup_subprocesses ()
451459
452460
@@ -509,5 +517,4 @@ async def test_restart_kernel(
509517 assert model ["connections" ] == 1
510518
511519 # Need to find a better solution to this.
512- await session_client .cleanup ()
513520 await jp_cleanup_subprocesses ()
0 commit comments