Example #1
0
async def test_posix_file_lock(tmpdir):
    file1 = Path(tmpdir / "1.lock")
    # Also check having missing folders in the path is not an issue
    file2 = Path(tmpdir / "missing_parent/missing_child/2.lock")

    # Test multiple time to make sure we can re-acquire the mutex once released
    for _ in range(2):
        with _install_posix_file_lock(file1):
            # Check mutex works
            with pytest.raises(IPCServerAlreadyRunning):
                with _install_posix_file_lock(file1):
                    pass

            # Check we can create new mutex
            with _install_posix_file_lock(file2):
                pass
Example #2
0
async def test_posix_file_lock_interprocess(tmpdir):
    file1 = Path(tmpdir / "1.lock")
    python_cmd = f"""
from pathlib import Path
from parsec.core.ipcinterface import _install_posix_file_lock, IPCServerAlreadyRunning
try:
    with _install_posix_file_lock(Path("{file1}")):
        pass
except IPCServerAlreadyRunning:
    raise SystemExit(0)
else:
    raise SystemExit(1)
"""

    with _install_posix_file_lock(file1):
        ret = subprocess.run(["python", "-c", python_cmd],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        print(ret.stdout.decode())
        print(ret.stderr.decode())
        assert ret.returncode == 0