Skip to content

Commit

Permalink
Improve robustness of subshell concurrency tests (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 authored Oct 25, 2024
1 parent 48e39cf commit 5ce5bee
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions tests/test_subshells.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,20 @@ def test_run_concurrently_sequence(are_subshells, overlap):
create_subshell_helper(kc)["subshell_id"] if is_subshell else None
for is_subshell in are_subshells
]

# Import time module before running time-sensitive subshell code.
execute_request_subshell_id(kc, "import time; print('ok')", None)

sleep = 0.2
if overlap:
codes = [
"import time; start0=True; end0=False; time.sleep(0.2); end0=True",
"assert start0; assert not end0; time.sleep(0.2); assert end0",
f"start0=True; end0=False; time.sleep({sleep}); end0=True",
f"time.sleep({sleep/2}); assert start0; assert not end0; time.sleep({sleep}); assert end0",
]
else:
codes = [
"import time; start0=True; end0=False; time.sleep(0.2); assert end1",
"assert start0; assert not end0; end1=True",
f"start0=True; end0=False; time.sleep({sleep}); assert end1",
f"time.sleep({sleep/2}); assert start0; assert not end0; end1=True",
]

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

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

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

# Import time module before running time-sensitive subshell code.
execute_request_subshell_id(kc, "import time; print('ok')", None)

times = (0.2, 0.2)
# Prepare messages, times are sleep times in seconds.
# Identical times for both subshells is a harder test as preparing and sending
# the execute_reply messages may overlap.
msgs = []
for id, sleep in zip(subshell_ids, times):
code = f"import time; time.sleep({sleep})"
code = f"time.sleep({sleep})"
msg = kc.session.msg("execute_request", {"code": code})
msg["header"]["subshell_id"] = id
msgs.append(msg)
Expand Down

0 comments on commit 5ce5bee

Please sign in to comment.