Spaces:
Running
Running
Download tests/core/test_interprocess_lock.py from dherain24/claude-proxy: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/dherain24/claude-proxy/resolve/main/tests/core/test_interprocess_lock.py
- Command line
-
hf download hf://spaces/dherain24/claude-proxy/tests/core/test_interprocess_lock.py
-
curl -L -o test_interprocess_lock.py https://huggingface.co/spaces/dherain24/claude-proxy/resolve/main/tests/core/test_interprocess_lock.py
2.2 kB
| import os | |
| import pytest | |
| from free_claude_code.core import interprocess_lock | |
| def test_failed_acquisition_closes_handle_and_allows_retry( | |
| tmp_path, monkeypatch, stage, error_type | |
| ): | |
| lock = interprocess_lock.InterprocessFileLock(tmp_path / "lock") | |
| opened = [] | |
| failure = error_type("acquisition interrupted") | |
| def try_lock(handle): | |
| opened.append(handle) | |
| if stage == "acquire": | |
| raise failure | |
| return False | |
| def sleep(_): | |
| raise failure | |
| try: | |
| with monkeypatch.context() as patch: | |
| patch.setattr(interprocess_lock, "_try_lock", try_lock) | |
| patch.setattr(interprocess_lock.time, "sleep", sleep) | |
| with pytest.raises(error_type) as caught: | |
| lock.acquire(wait=stage == "wait") | |
| assert caught.value is failure | |
| assert opened[0].closed | |
| assert lock.acquire() | |
| finally: | |
| lock.release() | |
| for handle in opened: | |
| handle.close() | |
| def test_empty_file_locked_by_another_handle_is_normal_contention( | |
| tmp_path, monkeypatch | |
| ): | |
| if os.name != "nt": | |
| pytest.skip("Windows byte-range locking") | |
| import msvcrt | |
| path = tmp_path / "lock" | |
| contender = interprocess_lock.InterprocessFileLock(path) | |
| opened = [] | |
| try_lock = interprocess_lock._try_lock | |
| def capture(handle): | |
| opened.append(handle) | |
| return try_lock(handle) | |
| monkeypatch.setattr(interprocess_lock, "_try_lock", capture) | |
| with path.open("a+b") as holder: | |
| # Windows supports locks beyond EOF. Hold byte zero without writing it. | |
| msvcrt.locking(holder.fileno(), msvcrt.LK_NBLCK, 1) | |
| try: | |
| assert contender.acquire() is False | |
| assert opened[0].closed | |
| finally: | |
| msvcrt.locking(holder.fileno(), msvcrt.LK_UNLCK, 1) | |
| contender.release() | |
| for handle in opened: | |
| handle.close() | |
| try: | |
| assert contender.acquire() | |
| assert path.stat().st_size == 0 | |
| finally: | |
| contender.release() | |