Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow datetime or str in test_sequential_control_messages #1219

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ docs = [
"trio"
]
test = [
"pytest>=7.0",
"pytest>=7.0,<8",
"pytest-cov",
"flaky",
"ipyparallel",
Expand Down
23 changes: 14 additions & 9 deletions tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,20 +620,25 @@ def test_sequential_control_messages():
# Get replies
replies = [get_reply(kc, msg_id, channel="control") for msg_id in msg_ids]

def ensure_datetime(arg):
# Support arg which is a datetime or str.
if isinstance(arg, str):
if sys.version_info[:2] < (3, 11) and arg.endswith("Z"):
# Python < 3.11 doesn't support "Z" suffix in datetime.fromisoformat,
# so use alternative timezone format.
# https://github.com/python/cpython/issues/80010
arg = arg[:-1] + "+00:00"
return datetime.fromisoformat(arg)
return arg

# Check messages are processed in order, one at a time, and of a sensible duration.
previous_end = None
for reply, sleep in zip(replies, sleeps):
start_str = reply["metadata"]["started"]
if sys.version_info[:2] < (3, 11) and start_str.endswith("Z"):
# Python < 3.11 doesn't support "Z" suffix in datetime.fromisoformat,
# so use alternative timezone format.
# https://github.com/python/cpython/issues/80010
start_str = start_str[:-1] + "+00:00"
start = datetime.fromisoformat(start_str)
end = reply["header"]["date"] # Already a datetime
start = ensure_datetime(reply["metadata"]["started"])
end = ensure_datetime(reply["header"]["date"])

if previous_end is not None:
assert start > previous_end
assert start >= previous_end
previous_end = end

assert end >= start + timedelta(seconds=sleep)
Expand Down
Loading