Skip to content

Commit 03f57c5

Browse files
committed
Make the system metrics tests independent of the installed mlflow
The monitor is only started when the mlflow system metrics module is importable, so the tests that assert it starts were relying on that being true in the environment they run in. Pin it for those tests and cover the case where it is missing, where the workflow should carry on with a warning. Signed-off-by: uditmahato <uditmahato29271@gmail.com>
1 parent fb48af9 commit 03f57c5

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

tests/handlers/test_handler_mlflow.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ def _train_func(engine, batch):
270270
close_on_complete=True,
271271
)
272272
monitor = MagicMock()
273-
with patch("monai.handlers.mlflow_handler.SystemMetricsMonitor", return_value=monitor) as monitor_class:
273+
with (
274+
patch("monai.handlers.mlflow_handler.SystemMetricsMonitor", return_value=monitor) as monitor_class,
275+
patch("monai.handlers.mlflow_handler.has_system_metrics", True),
276+
):
274277
handler.attach(engine)
275278
engine.run(range(3), max_epochs=1)
276279

@@ -303,7 +306,10 @@ def _train_func(engine, batch):
303306
for _ in range(3)
304307
]
305308
monitor = MagicMock()
306-
with patch("monai.handlers.mlflow_handler.SystemMetricsMonitor", return_value=monitor) as monitor_class:
309+
with (
310+
patch("monai.handlers.mlflow_handler.SystemMetricsMonitor", return_value=monitor) as monitor_class,
311+
patch("monai.handlers.mlflow_handler.has_system_metrics", True),
312+
):
307313
for handler in handlers:
308314
handler.start(engine)
309315

@@ -322,6 +328,31 @@ def _train_func(engine, batch):
322328
for handler in handlers:
323329
handler.close()
324330

331+
def test_system_metrics_warns_when_mlflow_is_too_old(self):
332+
"""
333+
Test that a workflow still runs, with a warning, when the installed mlflow cannot
334+
record the system metrics.
335+
"""
336+
with tempfile.TemporaryDirectory() as tempdir:
337+
338+
def _train_func(engine, batch):
339+
return [batch + 1.0]
340+
341+
engine = Engine(_train_func)
342+
test_path = os.path.join(tempdir, "mlflow_system_metrics_unavailable")
343+
handler = MLFlowHandler(
344+
iteration_log=False,
345+
tracking_uri=path_to_uri(test_path),
346+
log_system_metrics=True,
347+
close_on_complete=True,
348+
)
349+
with patch("monai.handlers.mlflow_handler.has_system_metrics", False):
350+
with self.assertWarns(Warning):
351+
handler.attach(engine)
352+
engine.run(range(3), max_epochs=1)
353+
354+
self.assertIsNone(handler.system_metrics_monitor)
355+
325356
def test_system_metrics_settings_are_validated(self):
326357
"""
327358
Test that a sampling setting that mlflow does not define a behaviour for is rejected.

0 commit comments

Comments
 (0)