Skip to content

Commit 35f5836

Browse files
committed
Randomize test order each run
A couple of dependencies discovered so far, and an apparent cleanup bug that I don't know how to fix with the `caplog` fixture. Drop using our `mock_log` fixture. [sc-46860]
1 parent 2e83898 commit 35f5836

File tree

7 files changed

+24
-9
lines changed

7 files changed

+24
-9
lines changed

compute_endpoint/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"coverage>=5.2",
5252
"pytest-mock==3.2.0",
5353
"pyfakefs<5.9.2", # 5.9.2 (Jul 30, 2025), breaks us; retry after 6.0.0 lands?
54+
"pytest-random-order",
5455
]
5556

5657

compute_endpoint/tests/unit/test_execute_task.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
import random
4+
from itertools import chain
45
from unittest import mock
56

67
import pytest
@@ -69,14 +70,13 @@ def test_bad_run_dir(endpoint_uuid, task_uuid, run_dir):
6970
execute_task(task_uuid, b"", endpoint_uuid, run_dir=None)
7071

7172

72-
def test_happy_path(serde, caplog, task_uuid, ez_pack_task, execute_task_runner):
73+
def test_happy_path(serde, mock_log, task_uuid, ez_pack_task, execute_task_runner):
7374
out = random.randint(1, 100_000)
7475
divisor = random.randint(1, 100_000)
7576

7677
task_bytes = ez_pack_task(divide, divisor * out, divisor)
7778

78-
with caplog.at_level(logging.DEBUG):
79-
packed_result = execute_task_runner(task_bytes)
79+
packed_result = execute_task_runner(task_bytes)
8080
assert isinstance(packed_result, bytes)
8181

8282
result = messagepack.unpack(packed_result)
@@ -89,7 +89,12 @@ def test_happy_path(serde, caplog, task_uuid, ez_pack_task, execute_task_runner)
8989
assert "endpoint_id" in result.details
9090
assert serde.deserialize(result.data) == out
9191

92-
log_msgs = "\n".join(r.msg % r.args for r in caplog.records)
92+
log_recs = []
93+
for a, _ in chain(mock_log.info.call_args_list, mock_log.debug.call_args_list):
94+
fmt, *a = a
95+
log_recs.append(a and fmt % tuple(a) or fmt)
96+
log_msgs = "\n".join(log_recs)
97+
9398
assert "Preparing to execute" in log_msgs, "Expect log clue of progress"
9499
assert "Unpacking" in log_msgs, "Expect log clue of progress"
95100
assert "Deserializing function" in log_msgs, "Expect log to clue of progress"
@@ -98,8 +103,8 @@ def test_happy_path(serde, caplog, task_uuid, ez_pack_task, execute_task_runner)
98103
assert "Task function complete" in log_msgs, "Expect log clue of progress"
99104
assert "Execution completed" in log_msgs, "Expect log clue of progress"
100105
assert "Task processing completed in" in log_msgs, "Expect log clue of progress"
101-
assert len(caplog.records) == 7, "Time to update test?"
102-
assert log_msgs.count(str(task_uuid)) == len(caplog.records), "Expect id prefixed"
106+
assert len(log_recs) == 7, "Time to update test?"
107+
assert log_msgs.count(str(task_uuid)) == len(log_recs), "Expect id always prefixed"
103108

104109

105110
def test_sandbox(task_10_2, execute_task_runner, task_uuid, tmp_path):

compute_endpoint/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extras = test
1010
usedevelop = true
1111
commands =
1212
coverage erase
13-
coverage run -m pytest --durations 5 --log-cli-level=ERROR {posargs}
13+
coverage run -m pytest --random-order --durations 5 --log-cli-level=ERROR {posargs}
1414
coverage report
1515

1616
[testenv:mypy]

compute_sdk/globus_compute_sdk/sdk/executor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,10 +1447,12 @@ def run(self):
14471447
fut.set_exception(self._cancellation_reason)
14481448
log.debug("Cancelled: %s", fut.task_id)
14491449
self._open_futures_empty.set()
1450+
self.shutdown()
14501451
log.debug("%r AMQP thread complete.", self)
14511452

14521453
def shutdown(self, wait=True, *, cancel_futures=False):
14531454
if not self.is_alive():
1455+
_RESULT_WATCHERS.pop(self.task_group_id, None)
14541456
return
14551457

14561458
self._closed = True # No more futures will be accepted

compute_sdk/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"flake8==3.8.0",
3737
"pytest>=7.2",
3838
"pytest-mock",
39+
"pytest-random-order",
3940
"pyfakefs",
4041
"coverage",
4142
# easy mocking of the `requests` library

compute_sdk/tests/unit/test_executor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def __init__(self, *args, **kwargs):
6969
}
7070
kwargs.setdefault("client", mock_client)
7171
super().__init__(*args, **kwargs)
72-
Executor._default_task_group_id = None # Reset for each test
7372
self._test_task_submitter_exception: t.Type[Exception] | None = None
7473
self._test_task_submitter_done = False
7574

@@ -112,6 +111,11 @@ def join(self, timeout: float | None = None) -> None:
112111
super().join(timeout=timeout)
113112

114113

114+
@pytest.fixture(autouse=True)
115+
def reset_default_executor_id():
116+
Executor._default_task_group_id = None # Reset for each test
117+
118+
115119
@pytest.fixture
116120
def mock_result_watcher(mocker: MockerFixture):
117121
rw = mocker.patch(f"{_MOCK_BASE}_ResultWatcher", autospec=True)
@@ -1581,6 +1585,7 @@ def test_resultwatcher_onmessage_verifies_result_type(mocker, unpacked):
15811585
mrw._on_message(mock_channel, mock_deliver, mock_props, b"some_bytes")
15821586
mock_channel.basic_nack.assert_called()
15831587
assert not mrw._received_results
1588+
mrw.shutdown()
15841589

15851590

15861591
def test_resultwatcher_onmessage_sets_check_results_flag():
@@ -1594,6 +1599,7 @@ def test_resultwatcher_onmessage_sets_check_results_flag():
15941599
mock_channel.basic_nack.assert_not_called()
15951600
assert mrw._received_results
15961601
assert mrw._time_to_check_results.is_set()
1602+
mrw.shutdown()
15971603

15981604

15991605
@pytest.mark.parametrize("exc", (MemoryError("some description"), "some description"))

compute_sdk/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ usedevelop = true
99
extras = test
1010
commands =
1111
coverage erase
12-
coverage run -m pytest --durations 5 {posargs}
12+
coverage run -m pytest --durations 5 --random-order {posargs}
1313
coverage report --skip-covered
1414

1515
[testenv:mypy]

0 commit comments

Comments
 (0)