Skip to content

Commit aa5c25c

Browse files
Add fixture to cleanup dataloader after each test runs (#1852)
1 parent aad1112 commit aa5c25c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import subprocess
2424
import time
2525
from pathlib import Path
26+
from unittest.mock import patch
2627

2728
import dask
2829
import pandas as pd
@@ -371,3 +372,19 @@ def devices(request):
371372
@pytest.fixture
372373
def report(request):
373374
return request.config.getoption("--report")
375+
376+
377+
@pytest.fixture(scope="function", autouse=True)
378+
def cleanup_dataloader():
379+
"""After each test runs. Call .stop() on any dataloaders created during the test.
380+
The avoids issues with background threads hanging around and interfering with subsequent tests.
381+
This happens when a dataloader is partially consumed (not all batches are iterated through).
382+
"""
383+
from merlin.dataloader.loader_base import LoaderBase
384+
385+
with patch.object(
386+
LoaderBase, "__iter__", side_effect=LoaderBase.__iter__, autospec=True
387+
) as patched:
388+
yield
389+
for call in patched.call_args_list:
390+
call.args[0].stop()

0 commit comments

Comments
 (0)