Skip to content

Commit 1d6cdc0

Browse files
authored
Isolate tests in multi_process_shared with unique temp path. (#38498)
1 parent 39632ef commit 1d6cdc0

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

sdks/python/apache_beam/utils/multi_process_shared_test.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,29 @@
2525
from typing import Any
2626
from unittest.mock import patch
2727

28+
import pytest
29+
2830
from apache_beam.utils import multi_process_shared
2931

3032

33+
@pytest.fixture(autouse=True)
34+
def isolate_multi_process_shared_tests(tmp_path, monkeypatch):
35+
"""Isolates MultiProcessShared tests by using a unique temp directory per test.
36+
37+
This prevents tests running in parallel (e.g. with pytest-xdist) from
38+
interfering with each other by writing to the same shared default temp directory.
39+
"""
40+
orig_init = multi_process_shared.MultiProcessShared.__init__
41+
42+
def mock_init(self, constructor, tag, *args, **kwargs):
43+
if 'path' not in kwargs:
44+
kwargs['path'] = str(tmp_path)
45+
return orig_init(self, constructor, tag, *args, **kwargs)
46+
47+
monkeypatch.setattr(
48+
multi_process_shared.MultiProcessShared, '__init__', mock_init)
49+
50+
3151
class CallableCounter(object):
3252
def __init__(self, start=0):
3353
self.running = start
@@ -285,23 +305,6 @@ def test_release_always_proxy(self):
285305

286306

287307
class MultiProcessSharedSpawnProcessTest(unittest.TestCase):
288-
def setUp(self):
289-
tempdir = tempfile.gettempdir()
290-
for tag in ['basic',
291-
'main',
292-
'to_delete',
293-
'to_keep',
294-
'mix1',
295-
'mix2',
296-
'test_process_exit',
297-
'thundering_herd_test',
298-
'transient_test']:
299-
for ext in ['', '.address', '.address.error']:
300-
try:
301-
os.remove(os.path.join(tempdir, tag + ext))
302-
except OSError:
303-
pass
304-
305308
def tearDown(self):
306309
for p in multiprocessing.active_children():
307310
if p.is_alive():

0 commit comments

Comments
 (0)