Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions sdks/python/apache_beam/utils/multi_process_shared_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@
from typing import Any
from unittest.mock import patch

import pytest

from apache_beam.utils import multi_process_shared


@pytest.fixture(autouse=True)
def isolate_multi_process_shared_tests(tmp_path, monkeypatch):
"""Isolates MultiProcessShared tests by using a unique temp directory per test.

This prevents tests running in parallel (e.g. with pytest-xdist) from
interfering with each other by writing to the same shared default temp directory.
"""
orig_init = multi_process_shared.MultiProcessShared.__init__

def mock_init(self, constructor, tag, *args, **kwargs):
if 'path' not in kwargs:
kwargs['path'] = str(tmp_path)
return orig_init(self, constructor, tag, *args, **kwargs)
Comment thread
shunping marked this conversation as resolved.

monkeypatch.setattr(
multi_process_shared.MultiProcessShared, '__init__', mock_init)


class CallableCounter(object):
def __init__(self, start=0):
self.running = start
Expand Down Expand Up @@ -285,23 +305,6 @@ def test_release_always_proxy(self):


class MultiProcessSharedSpawnProcessTest(unittest.TestCase):
def setUp(self):
tempdir = tempfile.gettempdir()
for tag in ['basic',
'main',
'to_delete',
'to_keep',
'mix1',
'mix2',
'test_process_exit',
'thundering_herd_test',
'transient_test']:
for ext in ['', '.address', '.address.error']:
try:
os.remove(os.path.join(tempdir, tag + ext))
except OSError:
pass

def tearDown(self):
for p in multiprocessing.active_children():
if p.is_alive():
Expand Down
Loading