|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | from swarms import Agent, AgentRearrange |
| 9 | +from swarms.telemetry.otel import ContextThreadPoolExecutor |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def create_sample_agents(): |
@@ -611,7 +612,9 @@ def bad_run(*args, **kwargs): |
611 | 612 | raise TypeError("unexpected error in agent") |
612 | 613 |
|
613 | 614 | next( |
614 | | - agent for agent in r.agents if agent.agent_name == "WriterAgent" |
| 615 | + agent |
| 616 | + for agent in r.agents |
| 617 | + if agent.agent_name == "WriterAgent" |
615 | 618 | ).run = bad_run |
616 | 619 |
|
617 | 620 | with pytest.raises(TypeError, match="unexpected error in agent"): |
@@ -1018,29 +1021,25 @@ def instrumented_run(task, img=None, *a, **kw): |
1018 | 1021 | ), "Expected at least two batch_run tasks to overlap in execution" |
1019 | 1022 |
|
1020 | 1023 | def test_threadpoolexecutor_is_used(self): |
1021 | | - """Patch ThreadPoolExecutor to confirm it is invoked for each batch.""" |
| 1024 | + """Patch the executor to confirm it is invoked for each batch.""" |
1022 | 1025 | pipeline = _make_pipeline("AgentA", "AgentB") |
1023 | 1026 | tasks = ["t1", "t2", "t3"] |
1024 | 1027 |
|
1025 | 1028 | with patch( |
1026 | | - "swarms.structs.agent_rearrange.ThreadPoolExecutor", |
1027 | | - wraps=__import__( |
1028 | | - "concurrent.futures", fromlist=["ThreadPoolExecutor"] |
1029 | | - ).ThreadPoolExecutor, |
| 1029 | + "swarms.structs.agent_rearrange.ContextThreadPoolExecutor", |
| 1030 | + wraps=ContextThreadPoolExecutor, |
1030 | 1031 | ) as mock_tpe: |
1031 | 1032 | pipeline.batch_run(tasks=tasks, batch_size=10) |
1032 | 1033 | assert mock_tpe.call_count == 1 |
1033 | 1034 |
|
1034 | 1035 | def test_multiple_batches_uses_executor_per_batch(self): |
1035 | | - """One ThreadPoolExecutor context-manager per batch.""" |
| 1036 | + """One executor context-manager per batch.""" |
1036 | 1037 | pipeline = _make_pipeline("AgentA", "AgentB") |
1037 | 1038 | tasks = [f"t{i}" for i in range(6)] |
1038 | 1039 |
|
1039 | 1040 | with patch( |
1040 | | - "swarms.structs.agent_rearrange.ThreadPoolExecutor", |
1041 | | - wraps=__import__( |
1042 | | - "concurrent.futures", fromlist=["ThreadPoolExecutor"] |
1043 | | - ).ThreadPoolExecutor, |
| 1041 | + "swarms.structs.agent_rearrange.ContextThreadPoolExecutor", |
| 1042 | + wraps=ContextThreadPoolExecutor, |
1044 | 1043 | ) as mock_tpe: |
1045 | 1044 | pipeline.batch_run(tasks=tasks, batch_size=2) |
1046 | 1045 | # 6 tasks / batch_size=2 -> 3 batches -> 3 executor instances |
@@ -1204,13 +1203,11 @@ def test_various_batch_sizes_return_all_results(self, batch_size): |
1204 | 1203 | assert len(results) == len(tasks) |
1205 | 1204 |
|
1206 | 1205 | def test_batch_size_one_still_uses_executor(self): |
1207 | | - """Even batch_size=1 should go through ThreadPoolExecutor.""" |
| 1206 | + """Even batch_size=1 should go through the executor.""" |
1208 | 1207 | pipeline = _make_pipeline("AgentA", "AgentB") |
1209 | 1208 | with patch( |
1210 | | - "swarms.structs.agent_rearrange.ThreadPoolExecutor", |
1211 | | - wraps=__import__( |
1212 | | - "concurrent.futures", fromlist=["ThreadPoolExecutor"] |
1213 | | - ).ThreadPoolExecutor, |
| 1209 | + "swarms.structs.agent_rearrange.ContextThreadPoolExecutor", |
| 1210 | + wraps=ContextThreadPoolExecutor, |
1214 | 1211 | ) as mock_tpe: |
1215 | 1212 | pipeline.batch_run(tasks=["only"], batch_size=1) |
1216 | 1213 | assert mock_tpe.call_count == 1 |
|
0 commit comments