@@ -113,6 +113,24 @@ async def cleanup(self):
113
113
time .sleep (0.1 )
114
114
115
115
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
+
116
134
@pytest .fixture
117
135
def session_client (jp_root_dir , jp_fetch ):
118
136
subdir = jp_root_dir .joinpath ("foo" )
@@ -156,7 +174,9 @@ def assert_session_equality(actual, expected):
156
174
assert_kernel_equality (actual ["kernel" ], expected ["kernel" ])
157
175
158
176
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
+ ):
160
180
# Make sure no sessions exist.
161
181
resp = await session_client .list ()
162
182
sessions = j (resp )
@@ -197,7 +217,6 @@ async def test_create(session_client, jp_base_url, jp_cleanup_subprocesses, jp_s
197
217
assert_session_equality (got , new_session )
198
218
199
219
# Need to find a better solution to this.
200
- await session_client .cleanup ()
201
220
await jp_cleanup_subprocesses ()
202
221
203
222
@@ -218,7 +237,6 @@ async def test_create_bad(
218
237
await session_client .create ("foo/nb1.ipynb" )
219
238
220
239
# Need to find a better solution to this.
221
- await session_client .cleanup ()
222
240
await jp_cleanup_subprocesses ()
223
241
224
242
@@ -251,7 +269,6 @@ async def test_create_bad_pending(
251
269
assert "non_existent_path" in session ["kernel" ]["reason" ]
252
270
253
271
# Need to find a better solution to this.
254
- await session_client .cleanup ()
255
272
await jp_cleanup_subprocesses ()
256
273
257
274
@@ -261,7 +278,6 @@ async def test_create_file_session(session_client, jp_cleanup_subprocesses, jp_s
261
278
newsession = j (resp )
262
279
assert newsession ["path" ] == "foo/nb1.py"
263
280
assert newsession ["type" ] == "file"
264
- await session_client .cleanup ()
265
281
await jp_cleanup_subprocesses ()
266
282
267
283
@@ -272,7 +288,6 @@ async def test_create_console_session(session_client, jp_cleanup_subprocesses, j
272
288
assert newsession ["path" ] == "foo/abc123"
273
289
assert newsession ["type" ] == "console"
274
290
# Need to find a better solution to this.
275
- await session_client .cleanup ()
276
291
await jp_cleanup_subprocesses ()
277
292
278
293
@@ -284,7 +299,6 @@ async def test_create_deprecated(session_client, jp_cleanup_subprocesses, jp_ser
284
299
assert newsession ["type" ] == "notebook"
285
300
assert newsession ["notebook" ]["path" ] == "foo/nb1.ipynb"
286
301
# Need to find a better solution to this.
287
- await session_client .cleanup ()
288
302
await jp_cleanup_subprocesses ()
289
303
290
304
@@ -316,7 +330,6 @@ async def test_create_with_kernel_id(
316
330
got = j (resp )
317
331
assert_session_equality (got , new_session )
318
332
# Need to find a better solution to this.
319
- await session_client .cleanup ()
320
333
await jp_cleanup_subprocesses ()
321
334
322
335
@@ -327,14 +340,15 @@ async def test_create_with_bad_kernel_id(session_client, jp_cleanup_subprocesses
327
340
# TODO
328
341
assert newsession ["path" ] == "foo/nb1.py"
329
342
assert newsession ["type" ] == "file"
330
- await session_client .cleanup ()
331
343
await jp_cleanup_subprocesses ()
332
344
333
345
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 ):
335
347
resp = await session_client .create ("foo/nb1.ipynb" )
348
+
336
349
newsession = j (resp )
337
350
sid = newsession ["id" ]
351
+ await session_is_ready (sid )
338
352
339
353
resp = await session_client .delete (sid )
340
354
assert resp .code == 204
@@ -347,7 +361,6 @@ async def test_delete(session_client, jp_cleanup_subprocesses, jp_serverapp):
347
361
await session_client .get (sid )
348
362
assert expected_http_error (e , 404 )
349
363
# Need to find a better solution to this.
350
- await session_client .cleanup ()
351
364
await jp_cleanup_subprocesses ()
352
365
353
366
@@ -361,7 +374,6 @@ async def test_modify_path(session_client, jp_cleanup_subprocesses, jp_serverapp
361
374
assert changed ["id" ] == sid
362
375
assert changed ["path" ] == "nb2.ipynb"
363
376
# Need to find a better solution to this.
364
- await session_client .cleanup ()
365
377
await jp_cleanup_subprocesses ()
366
378
367
379
@@ -375,7 +387,6 @@ async def test_modify_path_deprecated(session_client, jp_cleanup_subprocesses, j
375
387
assert changed ["id" ] == sid
376
388
assert changed ["notebook" ]["path" ] == "nb2.ipynb"
377
389
# Need to find a better solution to this.
378
- await session_client .cleanup ()
379
390
await jp_cleanup_subprocesses ()
380
391
381
392
@@ -389,7 +400,6 @@ async def test_modify_type(session_client, jp_cleanup_subprocesses, jp_serverapp
389
400
assert changed ["id" ] == sid
390
401
assert changed ["type" ] == "console"
391
402
# Need to find a better solution to this.
392
- await session_client .cleanup ()
393
403
await jp_cleanup_subprocesses ()
394
404
395
405
@@ -414,7 +424,6 @@ async def test_modify_kernel_name(session_client, jp_fetch, jp_cleanup_subproces
414
424
assert kernel_list == [after ["kernel" ]]
415
425
416
426
# Need to find a better solution to this.
417
- await session_client .cleanup ()
418
427
await jp_cleanup_subprocesses ()
419
428
420
429
@@ -446,7 +455,6 @@ async def test_modify_kernel_id(session_client, jp_fetch, jp_cleanup_subprocesse
446
455
assert kernel_list == [kernel ]
447
456
448
457
# Need to find a better solution to this.
449
- await session_client .cleanup ()
450
458
await jp_cleanup_subprocesses ()
451
459
452
460
@@ -509,5 +517,4 @@ async def test_restart_kernel(
509
517
assert model ["connections" ] == 1
510
518
511
519
# Need to find a better solution to this.
512
- await session_client .cleanup ()
513
520
await jp_cleanup_subprocesses ()
0 commit comments