Skip to content

tests: Use string and subprocess for isolation#6514

Merged
wenzeslaus merged 6 commits intoOSGeo:mainfrom
wenzeslaus:replace-multiprocessing-by-subprocess
Oct 16, 2025
Merged

tests: Use string and subprocess for isolation#6514
wenzeslaus merged 6 commits intoOSGeo:mainfrom
wenzeslaus:replace-multiprocessing-by-subprocess

Conversation

@wenzeslaus
Copy link
Member

@wenzeslaus wenzeslaus commented Oct 16, 2025

Instead of using multiprocessing, use code in a string and subprocess with sys.executable to run code in isolation. While this is requires the tested code to be in a string as opposed to a function, this avoids many issues of multiprocessing (stuck queue on failure, complexities of spawning). It replaces queue communication with JSON in stdout which requires additional care in what is actually printed.

As a result, the global environment is clearly not changed (it is a subprocess) as opposed to relying on multiprocessing behavior. For C library test, the separation of processes is even more clear, while at the same time, the test itself can now set up the runtime environment for dynamic libraries instead of relying on the external environment.

Renames and document tests in grass_script_setup_test.py with their current scope as their scope shifted when only one way of isolating and calling is used (no more partial vs nested function). Clarify role of setting the global capturing stderr in the tests and add more capture stderr smoke testing.

Be more consistent in mocking no global session with the fixture.

Uses capture outputs and exception to report stderr from the subprocess to ensure it is reported instead of relying on stderr being printed through pytest (stderr does not show up on Windows in CI with pytest for failures in lib_gis_env_test.py).

The constructed script needs to use raw strings for the path to accommodate backslashes (as a way of dealing with Windows paths).

The library test is skipped on Windows because the environment is not refreshed with the changes made in Python. Python updates it, but the getter function on Windows apparently keeps using a cached version, so the C functions access with ctypes don't see GISRC and fail with no session.

Instead of using multiprocessing, use code in a string and subprocess with sys.executable to run code in isolation. While this is requires the tested code to be in a string as opposed to a function, this avoids many issues of multiprocessing (stuck queue on failure, complexities of spawning). It replaces queue communication with JSON in stdout which requires additional care in what is actually printed.

As a result, the global environment is clearly not changed (it is a subprocess) as opposed to relying on multiprocessing behavior. For C library test, the separation of processes is even more clear, while at the same time, the test itself can now set up the runtime environment for dynamic libraries instead of relying on the external environmet.
@github-actions github-actions bot added Python Related code is in Python libraries tests Related to Test Suite labels Oct 16, 2025
@wenzeslaus
Copy link
Member Author

A remaining issue here are the tests for GridModule which is using the C API, but I don't plan to address that in this PR and I don't have a specific plan for it. The string solution looks good for couple specialized tests, but I would prefer not to use it for all GridModule tests. Suggestions welcome.

Anyway, I'm hoping this PR will help with failures in #6482.

…odate backslashes (as a way of dealing with Windows paths).
…lying on stderr being printed through pytest (does not show up on Windows in CI)
…smoke testing. Be more consitent in mocking no global session.
@wenzeslaus
Copy link
Member Author

I updated the PR description to clarify the what is included with all the additional changes. The grass_script_setup_test.py might be better reviewed as the file, not just the diff, because there is just a lot of structural changes due to switch to code in strings, passing of JSON as dict, not queue with a tuple, and a lot of new comments. However, the diff does show the related old and new lines together, so review with diff only should fine too.

… Python (it is updated, but the getter function keeps using a cached version), so the C functions don't see GISRC.
@wenzeslaus wenzeslaus enabled auto-merge (squash) October 16, 2025 19:14
@wenzeslaus wenzeslaus merged commit 2bd2d0a into OSGeo:main Oct 16, 2025
27 of 28 checks passed
@wenzeslaus wenzeslaus deleted the replace-multiprocessing-by-subprocess branch October 16, 2025 21:00
@github-actions github-actions bot added this to the 8.5.0 milestone Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants