Skip to content

Commit cb81ebc

Browse files
wuliang229copybara-github
authored andcommitted
test: fix hanging resource limits test with proper mocking
The `test_resource_limits_set` unit test was previously setting resource limits (such as a 100MB memory cap) on the test runner process itself, rather than the isolated subprocess. This caused the CI runner (e.g., GitHub Actions) to starve for resources and hang or crash. This change fixes the issue by scoping the `setrlimit` mock correctly and adds a safety mechanism to prevent future regressions from bringing down CI. Co-authored-by: Liang Wu <wuliang@google.com> PiperOrigin-RevId: 895565166
1 parent bbad9ec commit cb81ebc

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

tests/unittests/tools/test_bash_tool.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,25 @@ async def test_resource_limits_set(self, workspace, tool_context_confirmed):
263263
assert "preexec_fn" in mock_exec.call_args.kwargs
264264
preexec_fn = mock_exec.call_args.kwargs["preexec_fn"]
265265

266-
mock_setrlimit = mock.create_autospec(resource.setrlimit, instance=True)
267-
with mock.patch("resource.setrlimit", mock_setrlimit):
266+
# Save current limits to detect if the real process is modified
267+
orig_as = resource.getrlimit(resource.RLIMIT_AS)
268+
269+
with mock.patch(
270+
"google.adk.tools.bash_tool.resource.setrlimit"
271+
) as mock_setrlimit:
268272
preexec_fn()
269-
mock_setrlimit.assert_any_call(resource.RLIMIT_CORE, (0, 0))
270-
mock_setrlimit.assert_any_call(
271-
resource.RLIMIT_AS, (100 * 1024 * 1024, 100 * 1024 * 1024)
272-
)
273-
mock_setrlimit.assert_any_call(
274-
resource.RLIMIT_FSIZE, (50 * 1024 * 1024, 50 * 1024 * 1024)
275-
)
273+
274+
# Check if process limits were accidentally modified (leaked)
275+
current_as = resource.getrlimit(resource.RLIMIT_AS)
276+
if current_as != orig_as:
277+
# Reset to prevent environment corruption
278+
resource.setrlimit(resource.RLIMIT_AS, orig_as)
279+
pytest.fail("Test modified process resource limits! Mocking failed.")
280+
281+
mock_setrlimit.assert_any_call(resource.RLIMIT_CORE, (0, 0))
282+
mock_setrlimit.assert_any_call(
283+
resource.RLIMIT_AS, (100 * 1024 * 1024, 100 * 1024 * 1024)
284+
)
285+
mock_setrlimit.assert_any_call(
286+
resource.RLIMIT_FSIZE, (50 * 1024 * 1024, 50 * 1024 * 1024)
287+
)

0 commit comments

Comments
 (0)