Skip to content

Commit b0c13a4

Browse files
SongChiYoungekzhu
andauthored
test_docker_commandline_code_executor.py : 161.66s -> 108.07 (#6429)
## Why are these changes needed? Current autogen-ext's test is too slow. So, I will search slow test case and makes more fast. [init docker executor function to module 180s->140s](a3cf70b) [reuse executor at some tests 140s->120s](ca15938) [Remove unnecessary start of docker 120s->110s](6124761) ## Related issue number <!-- For example: "Closes #1234" --> Part of #6376 ## Checks - [ ] I've included any doc changes needed for <https://microsoft.github.io/autogen/>. See <https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to build and test documentation locally. - [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR. - [x] I've made sure all auto checks have passed. --------- Co-authored-by: Eric Zhu <[email protected]>
1 parent bfdf816 commit b0c13a4

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncio
33
import os
44
import sys
5+
import shutil
56
import tempfile
67
from pathlib import Path
78
from typing import AsyncGenerator, TypeAlias
@@ -32,7 +33,7 @@ def docker_tests_enabled() -> bool:
3233
return False
3334

3435

35-
@pytest_asyncio.fixture(scope="function") # type: ignore
36+
@pytest_asyncio.fixture(scope="module") # type: ignore
3637
async def executor_and_temp_dir(
3738
request: pytest.FixtureRequest,
3839
) -> AsyncGenerator[tuple[DockerCommandLineCodeExecutor, str], None]:
@@ -47,9 +48,20 @@ async def executor_and_temp_dir(
4748
ExecutorFixture: TypeAlias = tuple[DockerCommandLineCodeExecutor, str]
4849

4950

51+
@pytest_asyncio.fixture(scope="function") # type: ignore
52+
async def cleanup_temp_dir(executor_and_temp_dir: ExecutorFixture) -> AsyncGenerator[None, None]:
53+
_executor, temp_dir = executor_and_temp_dir
54+
for file in Path(temp_dir).iterdir():
55+
if file.is_file():
56+
file.unlink()
57+
elif file.is_dir():
58+
shutil.rmtree(file)
59+
yield None
60+
61+
5062
@pytest.mark.asyncio
5163
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
52-
async def test_execute_code(executor_and_temp_dir: ExecutorFixture) -> None:
64+
async def test_execute_code(executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None) -> None:
5365
executor, _temp_dir = executor_and_temp_dir
5466
cancellation_token = CancellationToken()
5567

@@ -97,7 +109,9 @@ async def test_execute_code(executor_and_temp_dir: ExecutorFixture) -> None:
97109

98110
@pytest.mark.asyncio
99111
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
100-
async def test_commandline_code_executor_timeout(executor_and_temp_dir: ExecutorFixture) -> None:
112+
async def test_commandline_code_executor_timeout(
113+
executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None
114+
) -> None:
101115
_executor, temp_dir = executor_and_temp_dir
102116
cancellation_token = CancellationToken()
103117
code_blocks = [CodeBlock(code="import time; time.sleep(10); print('hello world!')", language="python")]
@@ -110,7 +124,9 @@ async def test_commandline_code_executor_timeout(executor_and_temp_dir: Executor
110124

111125
@pytest.mark.asyncio
112126
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
113-
async def test_commandline_code_executor_cancellation(executor_and_temp_dir: ExecutorFixture) -> None:
127+
async def test_commandline_code_executor_cancellation(
128+
executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None
129+
) -> None:
114130
_executor, temp_dir = executor_and_temp_dir
115131
cancellation_token = CancellationToken()
116132
# Write code that sleep for 10 seconds and then write "hello world!"
@@ -137,7 +153,7 @@ async def test_commandline_code_executor_cancellation(executor_and_temp_dir: Exe
137153

138154
@pytest.mark.asyncio
139155
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
140-
async def test_invalid_relative_path(executor_and_temp_dir: ExecutorFixture) -> None:
156+
async def test_invalid_relative_path(executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None) -> None:
141157
executor, _temp_dir = executor_and_temp_dir
142158
cancellation_token = CancellationToken()
143159
code = """# filename: /tmp/test.py
@@ -152,7 +168,7 @@ async def test_invalid_relative_path(executor_and_temp_dir: ExecutorFixture) ->
152168

153169
@pytest.mark.asyncio
154170
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
155-
async def test_valid_relative_path(executor_and_temp_dir: ExecutorFixture) -> None:
171+
async def test_valid_relative_path(executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None) -> None:
156172
executor, temp_dir_str = executor_and_temp_dir
157173

158174
cancellation_token = CancellationToken()
@@ -243,18 +259,13 @@ async def test_docker_commandline_code_executor_extra_args() -> None:
243259
async def test_docker_commandline_code_executor_serialization() -> None:
244260
with tempfile.TemporaryDirectory() as temp_dir:
245261
executor = DockerCommandLineCodeExecutor(work_dir=temp_dir)
246-
await executor.start()
247262

248263
executor_config = executor.dump_component()
249264
loaded_executor = DockerCommandLineCodeExecutor.load_component(executor_config)
250-
await loaded_executor.start()
251265

252266
assert executor.bind_dir == loaded_executor.bind_dir
253267
assert executor.timeout == loaded_executor.timeout
254268

255-
await executor.stop()
256-
await loaded_executor.stop()
257-
258269

259270
def test_invalid_timeout() -> None:
260271
with pytest.raises(ValueError, match="Timeout must be greater than or equal to 1."):
@@ -269,23 +280,23 @@ async def test_directory_not_initialized() -> None:
269280

270281

271282
@pytest.mark.asyncio
272-
async def test_error_wrong_path() -> None:
283+
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
284+
async def test_error_wrong_path(executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None) -> None:
273285
if not docker_tests_enabled():
274286
pytest.skip("Docker tests are disabled")
275287

276-
with tempfile.TemporaryDirectory() as temp_dir:
277-
async with DockerCommandLineCodeExecutor(work_dir=temp_dir) as executor:
278-
cancellation_token = CancellationToken()
279-
code_blocks = [
280-
CodeBlock(
281-
code="""with open("/nonexistent_dir/test.txt", "w") as f:
282-
f.write("hello world!")""",
283-
language="python",
284-
)
285-
]
286-
result = await executor.execute_code_blocks(code_blocks, cancellation_token)
287-
assert result.exit_code != 0
288-
assert "No such file or directory" in result.output
288+
executor, _ = executor_and_temp_dir
289+
cancellation_token = CancellationToken()
290+
code_blocks = [
291+
CodeBlock(
292+
code="""with open("/nonexistent_dir/test.txt", "w") as f:
293+
f.write("hello world!")""",
294+
language="python",
295+
)
296+
]
297+
result = await executor.execute_code_blocks(code_blocks, cancellation_token)
298+
assert result.exit_code != 0
299+
assert "No such file or directory" in result.output
289300

290301

291302
@pytest.mark.asyncio
@@ -364,7 +375,10 @@ async def test_delete_tmp_files() -> None:
364375

365376

366377
@pytest.mark.asyncio
367-
async def test_docker_commandline_code_executor_with_multiple_tasks() -> None:
378+
@pytest.mark.parametrize("executor_and_temp_dir", ["docker"], indirect=True)
379+
async def test_docker_commandline_code_executor_with_multiple_tasks(
380+
executor_and_temp_dir: ExecutorFixture, cleanup_temp_dir: None
381+
) -> None:
368382
if not docker_tests_enabled():
369383
pytest.skip("Docker tests are disabled")
370384

@@ -382,6 +396,5 @@ async def run_cancellation_scenario(executor: DockerCommandLineCodeExecutor) ->
382396
def run_scenario_in_new_loop(executor_instance: DockerCommandLineCodeExecutor) -> None:
383397
asyncio.run(run_cancellation_scenario(executor_instance))
384398

385-
with tempfile.TemporaryDirectory() as temp_dir:
386-
async with DockerCommandLineCodeExecutor(work_dir=temp_dir) as executor:
387-
await asyncio.get_running_loop().run_in_executor(None, run_scenario_in_new_loop, executor)
399+
executor, _ = executor_and_temp_dir
400+
await asyncio.get_running_loop().run_in_executor(None, run_scenario_in_new_loop, executor)

0 commit comments

Comments
 (0)