Skip to content

Commit 99d482d

Browse files
authored
TEST: Fix plugin invocation, use an initializer that can be verified (#880)
* TEST: Fix plugin invocation, use an initializer that can be verified * doc: Update MultiProcPlugin.__init__ docstring to explain app_config
1 parent a744a53 commit 99d482d

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

niworkflows/engine/plugin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@ def __init__(self, pool=None, plugin_args=None):
401401
"""
402402
Initialize the plugin.
403403
404+
If passed a nipreps-style configuration object in `plugin_args["app_config"]`,
405+
the following fields must be present:
406+
407+
app_config.environment.total_memory : :obj:`float`
408+
Memory available to the workflow in gigabytes.
409+
app_config._process_initializer : :obj:`callable`
410+
A function that accepts a file path and returns None, to be run in each worker.
411+
app_config.file_path : :obj:`str`
412+
The path to a file that will be passed to the initializer.
413+
404414
Arguments
405415
---------
406416
pool : :obj:`~concurrent.futures.Executor`

niworkflows/engine/tests/test_plugin.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,28 @@ def test_plugin_defaults(workflow, caplog):
7272
def test_plugin_args_noconfig(workflow, caplog):
7373
"""Test the plugin works with typical nipype arguments."""
7474
caplog.set_level(logging.CRITICAL, logger="nipype.workflow")
75-
workflow.run(
76-
plugin=MultiProcPlugin(),
77-
plugin_args={"n_procs": 2, "memory_gb": 0.1},
78-
)
75+
workflow.run(plugin=MultiProcPlugin(plugin_args={"n_procs": 2, "memory_gb": 0.1}))
76+
7977

78+
def touch_file(file_path: str) -> None:
79+
"""Module-level functions play more nicely with multiprocessing."""
80+
with open(file_path, "w") as f:
81+
f.write("flag")
8082

81-
def test_plugin_app_config(workflow, caplog, capsys):
83+
84+
def test_plugin_app_config(tmp_path, workflow, caplog):
8285
"""Test the plugin works with a nipreps-style configuration."""
8386

84-
def init_print():
85-
print("Custom init")
87+
init_flag = tmp_path / "init_flag.txt"
8688

8789
app_config = SimpleNamespace(
88-
environment=SimpleNamespace(total_memory_gb=1),
89-
_process_initializer=init_print(),
90-
file_path='/does/not/need/to/exist/for/testing',
90+
environment=SimpleNamespace(total_memory=1),
91+
_process_initializer=touch_file,
92+
file_path=str(init_flag),
9193
)
92-
caplog.set_level(logging.CRITICAL, logger="nipype.workflow")
94+
caplog.set_level(logging.INFO, logger="nipype.workflow")
9395
workflow.run(
94-
plugin=MultiProcPlugin(),
95-
plugin_args={"n_procs": 2, "app_config": app_config},
96+
plugin=MultiProcPlugin(plugin_args={"n_procs": 2, "app_config": app_config})
9697
)
9798

98-
captured = capsys.readouterr()
99-
assert "Custom init" in captured.out
99+
assert init_flag.exists() and init_flag.read_text() == "flag"

0 commit comments

Comments
 (0)