|
17 | 17 | from IPython.paths import locate_profile |
18 | 18 | from ipython_genutils.tempdir import TemporaryDirectory |
19 | 19 |
|
| 20 | +from ipykernel.tests.test_message_spec import validate_message |
20 | 21 | from .utils import ( |
21 | 22 | new_kernel, kernel, TIMEOUT, assemble_output, execute, |
22 | 23 | flush_channels, wait_for_idle, |
23 | | -) |
| 24 | + connect_to_kernel) |
24 | 25 |
|
25 | 26 |
|
26 | 27 | def _check_master(kc, expected=True, stream="stdout"): |
@@ -326,3 +327,43 @@ def test_shutdown(): |
326 | 327 | else: |
327 | 328 | break |
328 | 329 | assert not km.is_alive() |
| 330 | + |
| 331 | +def test_fork_metadata(): |
| 332 | + with kernel() as kc: |
| 333 | + km = kc.parent |
| 334 | + fork_msg_id = kc.fork() |
| 335 | + fork_reply = kc.get_shell_msg(block=True, timeout=TIMEOUT) |
| 336 | + # validate_message(fork_reply, "execute_reply", fork_msg_id) # TODO: Make it work (need the `fork_reply`) |
| 337 | + assert fork_msg_id == fork_reply['parent_header']['msg_id'] == fork_msg_id |
| 338 | + assert fork_reply['content']['conn']['key'] != kc.session.key.decode() |
| 339 | + fork_pid = fork_reply['content']['pid'] |
| 340 | + _check_status(fork_reply['content']) |
| 341 | + wait_for_idle(kc) |
| 342 | + |
| 343 | + assert fork_pid != km.kernel.pid |
| 344 | + #TODO: Inspect if `fork_pid` is running? Might need to use `psutil` for this in order to be cross platform |
| 345 | + |
| 346 | + with connect_to_kernel(fork_reply['content']['conn'], TIMEOUT) as kc_fork: |
| 347 | + assert fork_reply['content']['conn']['key'] == kc_fork.session.key.decode() |
| 348 | + kc_fork.shutdown() |
| 349 | + |
| 350 | +def test_fork(): |
| 351 | + def execute_with_user_expression(kc, code, user_expression): |
| 352 | + _, reply = execute(code, kc=kc, user_expressions={"my-user-expression": user_expression}) |
| 353 | + content = reply["user_expressions"]["my-user-expression"]["data"]["text/plain"] |
| 354 | + wait_for_idle(kc) |
| 355 | + return content |
| 356 | + |
| 357 | + """Kernel forks after fork_request""" |
| 358 | + with kernel() as kc: |
| 359 | + assert execute_with_user_expression(kc, u'a = 1', "a") == "1" |
| 360 | + assert execute_with_user_expression(kc, u'b = 2', "b") == "2" |
| 361 | + kc.fork() |
| 362 | + fork_reply = kc.get_shell_msg(block=True, timeout=TIMEOUT) |
| 363 | + wait_for_idle(kc) |
| 364 | + |
| 365 | + with connect_to_kernel(fork_reply['content']['conn'], TIMEOUT) as kc_fork: |
| 366 | + assert execute_with_user_expression(kc_fork, 'a = 11', "a, b") == str((11, 2)) |
| 367 | + assert execute_with_user_expression(kc_fork, 'b = 12', "a, b") == str((11, 12)) |
| 368 | + assert execute_with_user_expression(kc, 'z = 20', "a, b") == str((1, 2)) |
| 369 | + kc_fork.shutdown() |
0 commit comments