Skip to content

Commit 5956899

Browse files
committed
Add a failing test for child thread
1 parent 9e9c40e commit 5956899

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/test_kernel.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ def thread_target():
9090
received += 1
9191

9292

93+
def test_print_to_correct_cell_from_child_thread():
94+
"""should print to the cell that spawned the thread, not a subsequently run cell"""
95+
iterations = 5
96+
interval = 0.25
97+
code = f"""\
98+
from threading import Thread
99+
from time import sleep
100+
101+
def child_target():
102+
for i in range({iterations}):
103+
print(i, end='', flush=True)
104+
sleep({interval})
105+
106+
def parent_target():
107+
sleep({interval})
108+
Thread(target=child_target).start()
109+
110+
Thread(target=parent_target).start()
111+
"""
112+
with kernel() as kc:
113+
thread_msg_id = kc.execute(code)
114+
_ = kc.execute("pass")
115+
116+
received = 0
117+
while received < iterations:
118+
msg = kc.get_iopub_msg(timeout=interval * 2)
119+
if msg["msg_type"] != "stream":
120+
continue
121+
content = msg["content"]
122+
assert content["name"] == "stdout"
123+
assert content["text"] == str(received)
124+
# this is crucial as the parent header decides to which cell the output goes
125+
assert msg["parent_header"]["msg_id"] == thread_msg_id
126+
received += 1
127+
128+
93129
def test_print_to_correct_cell_from_asyncio():
94130
"""should print to the cell that scheduled the task, not a subsequently run cell"""
95131
iterations = 5

0 commit comments

Comments
 (0)