|
7 | 7 | import platform
|
8 | 8 | import time
|
9 | 9 | from collections import Counter
|
| 10 | +from queue import Empty |
10 | 11 |
|
11 | 12 | import pytest
|
12 | 13 | from jupyter_client.blocking.client import BlockingKernelClient
|
13 | 14 |
|
14 |
| -from .utils import TIMEOUT, assemble_output, get_replies, get_reply, new_kernel, wait_for_idle |
| 15 | +from .utils import ( |
| 16 | + TIMEOUT, |
| 17 | + assemble_output, |
| 18 | + flush_channels, |
| 19 | + get_replies, |
| 20 | + get_reply, |
| 21 | + new_kernel, |
| 22 | + wait_for_idle, |
| 23 | +) |
15 | 24 |
|
16 | 25 | # Helpers
|
17 | 26 |
|
@@ -40,8 +49,10 @@ def list_subshell_helper(kc: BlockingKernelClient):
|
40 | 49 | return reply["content"]
|
41 | 50 |
|
42 | 51 |
|
43 |
| -def execute_request(kc: BlockingKernelClient, code: str, subshell_id: str | None): |
44 |
| - msg = kc.session.msg("execute_request", {"code": code}) |
| 52 | +def execute_request( |
| 53 | + kc: BlockingKernelClient, code: str, subshell_id: str | None, silent: bool = False |
| 54 | +): |
| 55 | + msg = kc.session.msg("execute_request", {"code": code, "silent": silent}) |
45 | 56 | msg["header"]["subshell_id"] = subshell_id
|
46 | 57 | kc.shell_channel.send(msg)
|
47 | 58 | return msg
|
@@ -224,16 +235,16 @@ def test_execute_stop_on_error(are_subshells):
|
224 | 235 | msg = execute_request(
|
225 | 236 | kc, "import asyncio; await asyncio.sleep(1); raise ValueError()", subshell_ids[0]
|
226 | 237 | )
|
227 |
| - msg_ids.append(msg["msg_id"]) |
| 238 | + msg_ids.append(msg["header"]["msg_id"]) |
228 | 239 | msg = execute_request(kc, "print('hello')", subshell_ids[0])
|
229 |
| - msg_ids.append(msg["msg_id"]) |
| 240 | + msg_ids.append(msg["header"]["msg_id"]) |
230 | 241 | msg = execute_request(kc, "print('goodbye')", subshell_ids[0])
|
231 |
| - msg_ids.append(msg["msg_id"]) |
| 242 | + msg_ids.append(msg["header"]["msg_id"]) |
232 | 243 |
|
233 | 244 | msg = execute_request(kc, "import time; time.sleep(1.5)", subshell_ids[1])
|
234 |
| - msg_ids.append(msg["msg_id"]) |
| 245 | + msg_ids.append(msg["header"]["msg_id"]) |
235 | 246 | msg = execute_request(kc, "print('other')", subshell_ids[1])
|
236 |
| - msg_ids.append(msg["msg_id"]) |
| 247 | + msg_ids.append(msg["header"]["msg_id"]) |
237 | 248 |
|
238 | 249 | replies = get_replies(kc, msg_ids)
|
239 | 250 |
|
@@ -313,3 +324,70 @@ def test_idle_message_parent_headers(are_subshells):
|
313 | 324 | for subshell_id in subshell_ids:
|
314 | 325 | if subshell_id:
|
315 | 326 | delete_subshell_helper(kc, subshell_id)
|
| 327 | + |
| 328 | + |
| 329 | +def test_silent_flag_in_subshells(): |
| 330 | + """Verifies that the 'silent' flag suppresses output in main and subshell contexts.""" |
| 331 | + with new_kernel() as kc: |
| 332 | + subshell_id = None |
| 333 | + try: |
| 334 | + flush_channels(kc) |
| 335 | + # Test silent execution in main shell |
| 336 | + msg_main_silent = execute_request(kc, "a=1", None, silent=True) |
| 337 | + reply_main_silent = get_reply(kc, msg_main_silent["header"]["msg_id"]) |
| 338 | + assert reply_main_silent["content"]["status"] == "ok" |
| 339 | + |
| 340 | + # Test silent execution in subshell |
| 341 | + subshell_id = create_subshell_helper(kc)["subshell_id"] |
| 342 | + msg_sub_silent = execute_request(kc, "b=2", subshell_id, silent=True) |
| 343 | + reply_sub_silent = get_reply(kc, msg_sub_silent["header"]["msg_id"]) |
| 344 | + assert reply_sub_silent["content"]["status"] == "ok" |
| 345 | + |
| 346 | + # Check for no iopub messages (other than status) from the silent requests |
| 347 | + for msg_id in [msg_main_silent["header"]["msg_id"], msg_sub_silent["header"]["msg_id"]]: |
| 348 | + while True: |
| 349 | + try: |
| 350 | + msg = kc.get_iopub_msg(timeout=0.2) |
| 351 | + if msg["header"]["msg_type"] == "status": |
| 352 | + continue |
| 353 | + pytest.fail( |
| 354 | + f"Silent execution produced unexpected IOPub message: {msg['header']['msg_type']}" |
| 355 | + ) |
| 356 | + except Empty: |
| 357 | + break |
| 358 | + |
| 359 | + # Test concurrent silent and non-silent execution |
| 360 | + msg_silent = execute_request( |
| 361 | + kc, "import time; time.sleep(0.5); c=3", subshell_id, silent=True |
| 362 | + ) |
| 363 | + msg_noisy = execute_request(kc, "print('noisy')", None, silent=False) |
| 364 | + |
| 365 | + # Wait for both replies |
| 366 | + get_replies(kc, [msg_silent["header"]["msg_id"], msg_noisy["header"]["msg_id"]]) |
| 367 | + |
| 368 | + # Verify that we only receive stream output from the noisy message |
| 369 | + stdout, stderr = assemble_output( |
| 370 | + kc.get_iopub_msg, parent_msg_id=msg_noisy["header"]["msg_id"] |
| 371 | + ) |
| 372 | + assert "noisy" in stdout |
| 373 | + assert not stderr |
| 374 | + |
| 375 | + # Verify there is no output from the concurrent silent message |
| 376 | + while True: |
| 377 | + try: |
| 378 | + msg = kc.get_iopub_msg(timeout=0.2) |
| 379 | + if ( |
| 380 | + msg["header"]["msg_type"] == "status" |
| 381 | + and msg["parent_header"].get("msg_id") == msg_silent["header"]["msg_id"] |
| 382 | + ): |
| 383 | + continue |
| 384 | + if msg["parent_header"].get("msg_id") == msg_silent["header"]["msg_id"]: |
| 385 | + pytest.fail( |
| 386 | + "Silent execution in concurrent setting produced unexpected IOPub message" |
| 387 | + ) |
| 388 | + except Empty: |
| 389 | + break |
| 390 | + finally: |
| 391 | + # Ensure subshell is always deleted |
| 392 | + if subshell_id: |
| 393 | + delete_subshell_helper(kc, subshell_id) |
0 commit comments