Skip to content

Commit 736b73d

Browse files
committed
test(agent-rearrange): patch the executor the module actually uses
The three batch_run concurrency tests patch swarms.structs.agent_rearrange.ThreadPoolExecutor, a name the module stopped binding when kyegomez#2078 switched it to ContextThreadPoolExecutor. mock.patch raises AttributeError before the test body runs, so all three have been failing on master and the concurrency guarantee they exist to guard has had no working coverage since. Patch ContextThreadPoolExecutor instead, and drop the __import__ dance for a plain import. Verified with teeth: replacing batch_run's executor block with a sequential loop fails all three. Before this change they errored identically whatever the source did.
1 parent 6d9e61d commit 736b73d

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

tests/structs/test_agent_rearrange.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from swarms import Agent, AgentRearrange
9+
from swarms.telemetry.otel import ContextThreadPoolExecutor
910

1011

1112
def create_sample_agents():
@@ -1018,29 +1019,25 @@ def instrumented_run(task, img=None, *a, **kw):
10181019
), "Expected at least two batch_run tasks to overlap in execution"
10191020

10201021
def test_threadpoolexecutor_is_used(self):
1021-
"""Patch ThreadPoolExecutor to confirm it is invoked for each batch."""
1022+
"""Patch the executor to confirm it is invoked for each batch."""
10221023
pipeline = _make_pipeline("AgentA", "AgentB")
10231024
tasks = ["t1", "t2", "t3"]
10241025

10251026
with patch(
1026-
"swarms.structs.agent_rearrange.ThreadPoolExecutor",
1027-
wraps=__import__(
1028-
"concurrent.futures", fromlist=["ThreadPoolExecutor"]
1029-
).ThreadPoolExecutor,
1027+
"swarms.structs.agent_rearrange.ContextThreadPoolExecutor",
1028+
wraps=ContextThreadPoolExecutor,
10301029
) as mock_tpe:
10311030
pipeline.batch_run(tasks=tasks, batch_size=10)
10321031
assert mock_tpe.call_count == 1
10331032

10341033
def test_multiple_batches_uses_executor_per_batch(self):
1035-
"""One ThreadPoolExecutor context-manager per batch."""
1034+
"""One executor context-manager per batch."""
10361035
pipeline = _make_pipeline("AgentA", "AgentB")
10371036
tasks = [f"t{i}" for i in range(6)]
10381037

10391038
with patch(
1040-
"swarms.structs.agent_rearrange.ThreadPoolExecutor",
1041-
wraps=__import__(
1042-
"concurrent.futures", fromlist=["ThreadPoolExecutor"]
1043-
).ThreadPoolExecutor,
1039+
"swarms.structs.agent_rearrange.ContextThreadPoolExecutor",
1040+
wraps=ContextThreadPoolExecutor,
10441041
) as mock_tpe:
10451042
pipeline.batch_run(tasks=tasks, batch_size=2)
10461043
# 6 tasks / batch_size=2 -> 3 batches -> 3 executor instances
@@ -1204,13 +1201,11 @@ def test_various_batch_sizes_return_all_results(self, batch_size):
12041201
assert len(results) == len(tasks)
12051202

12061203
def test_batch_size_one_still_uses_executor(self):
1207-
"""Even batch_size=1 should go through ThreadPoolExecutor."""
1204+
"""Even batch_size=1 should go through the executor."""
12081205
pipeline = _make_pipeline("AgentA", "AgentB")
12091206
with patch(
1210-
"swarms.structs.agent_rearrange.ThreadPoolExecutor",
1211-
wraps=__import__(
1212-
"concurrent.futures", fromlist=["ThreadPoolExecutor"]
1213-
).ThreadPoolExecutor,
1207+
"swarms.structs.agent_rearrange.ContextThreadPoolExecutor",
1208+
wraps=ContextThreadPoolExecutor,
12141209
) as mock_tpe:
12151210
pipeline.batch_run(tasks=["only"], batch_size=1)
12161211
assert mock_tpe.call_count == 1

0 commit comments

Comments
 (0)