Skip to content

Commit c353ddf

Browse files
authored
Improve robustness of subshell concurrency tests using Barrier (ipython#1288)
1 parent b7ad5c8 commit c353ddf

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

tests/test_subshells.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,22 @@ def test_run_concurrently_sequence(are_subshells, overlap):
134134
for is_subshell in are_subshells
135135
]
136136

137-
# Import time module before running time-sensitive subshell code.
138-
execute_request_subshell_id(kc, "import time; print('ok')", None)
137+
# Import time module before running time-sensitive subshell code
138+
# and use threading.Barrier to synchronise start of subshell code.
139+
execute_request_subshell_id(
140+
kc, "import threading as t, time; b=t.Barrier(2); print('ok')", None
141+
)
139142

140143
sleep = 0.2
141144
if overlap:
142145
codes = [
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",
146+
f"b.wait(); start0=True; end0=False; time.sleep({sleep}); end0=True",
147+
f"b.wait(); time.sleep({sleep/2}); assert start0; assert not end0; time.sleep({sleep}); assert end0",
145148
]
146149
else:
147150
codes = [
148-
f"start0=True; end0=False; time.sleep({sleep}); assert end1",
149-
f"time.sleep({sleep/2}); assert start0; assert not end0; end1=True",
151+
f"b.wait(); start0=True; end0=False; time.sleep({sleep}); assert end1",
152+
f"b.wait(); time.sleep({sleep/2}); assert start0; assert not end0; end1=True",
150153
]
151154

152155
msgs = []
@@ -174,16 +177,19 @@ def test_run_concurrently_timing(include_main_shell):
174177
create_subshell_helper(kc)["subshell_id"],
175178
]
176179

177-
# Import time module before running time-sensitive subshell code.
178-
execute_request_subshell_id(kc, "import time; print('ok')", None)
180+
# Import time module before running time-sensitive subshell code
181+
# and use threading.Barrier to synchronise start of subshell code.
182+
execute_request_subshell_id(
183+
kc, "import threading as t, time; b=t.Barrier(2); print('ok')", None
184+
)
179185

180186
times = (0.2, 0.2)
181187
# Prepare messages, times are sleep times in seconds.
182188
# Identical times for both subshells is a harder test as preparing and sending
183189
# the execute_reply messages may overlap.
184190
msgs = []
185191
for id, sleep in zip(subshell_ids, times):
186-
code = f"time.sleep({sleep})"
192+
code = f"b.wait(); time.sleep({sleep})"
187193
msg = kc.session.msg("execute_request", {"code": code})
188194
msg["header"]["subshell_id"] = id
189195
msgs.append(msg)
@@ -213,11 +219,17 @@ def test_execution_count():
213219
with new_kernel() as kc:
214220
subshell_id = create_subshell_helper(kc)["subshell_id"]
215221

222+
# Import time module before running time-sensitive subshell code
223+
# and use threading.Barrier to synchronise start of subshell code.
224+
execute_request_subshell_id(
225+
kc, "import threading as t, time; b=t.Barrier(2); print('ok')", None
226+
)
227+
216228
# Prepare messages
217229
times = (0.2, 0.1, 0.4, 0.15) # Sleep seconds
218230
msgs = []
219-
for id, sleep in zip((None, subshell_id, None, subshell_id), times):
220-
code = f"import time; time.sleep({sleep})"
231+
for i, (id, sleep) in enumerate(zip((None, subshell_id, None, subshell_id), times)):
232+
code = f"b.wait(); time.sleep({sleep})" if i < 2 else f"time.sleep({sleep})"
221233
msg = kc.session.msg("execute_request", {"code": code})
222234
msg["header"]["subshell_id"] = id
223235
msgs.append(msg)

0 commit comments

Comments
 (0)