Skip to content

Commit 5ce5bee

Browse files
authored
Improve robustness of subshell concurrency tests (#1285)
1 parent 48e39cf commit 5ce5bee

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tests/test_subshells.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,20 @@ def test_run_concurrently_sequence(are_subshells, overlap):
133133
create_subshell_helper(kc)["subshell_id"] if is_subshell else None
134134
for is_subshell in are_subshells
135135
]
136+
137+
# Import time module before running time-sensitive subshell code.
138+
execute_request_subshell_id(kc, "import time; print('ok')", None)
139+
140+
sleep = 0.2
136141
if overlap:
137142
codes = [
138-
"import time; start0=True; end0=False; time.sleep(0.2); end0=True",
139-
"assert start0; assert not end0; time.sleep(0.2); assert end0",
143+
f"start0=True; end0=False; time.sleep({sleep}); end0=True",
144+
f"time.sleep({sleep/2}); assert start0; assert not end0; time.sleep({sleep}); assert end0",
140145
]
141146
else:
142147
codes = [
143-
"import time; start0=True; end0=False; time.sleep(0.2); assert end1",
144-
"assert start0; assert not end0; end1=True",
148+
f"start0=True; end0=False; time.sleep({sleep}); assert end1",
149+
f"time.sleep({sleep/2}); assert start0; assert not end0; end1=True",
145150
]
146151

147152
msgs = []
@@ -150,8 +155,6 @@ def test_run_concurrently_sequence(are_subshells, overlap):
150155
msg["header"]["subshell_id"] = subshell_id
151156
kc.shell_channel.send(msg)
152157
msgs.append(msg)
153-
if len(msgs) == 1:
154-
time.sleep(0.1) # Wait for first execute_request to start.
155158

156159
replies = get_replies(kc, [msg["msg_id"] for msg in msgs])
157160

@@ -171,13 +174,16 @@ def test_run_concurrently_timing(include_main_shell):
171174
create_subshell_helper(kc)["subshell_id"],
172175
]
173176

177+
# Import time module before running time-sensitive subshell code.
178+
execute_request_subshell_id(kc, "import time; print('ok')", None)
179+
174180
times = (0.2, 0.2)
175181
# Prepare messages, times are sleep times in seconds.
176182
# Identical times for both subshells is a harder test as preparing and sending
177183
# the execute_reply messages may overlap.
178184
msgs = []
179185
for id, sleep in zip(subshell_ids, times):
180-
code = f"import time; time.sleep({sleep})"
186+
code = f"time.sleep({sleep})"
181187
msg = kc.session.msg("execute_request", {"code": code})
182188
msg["header"]["subshell_id"] = id
183189
msgs.append(msg)

0 commit comments

Comments
 (0)