Skip to content

Commit

Permalink
Add test for all arg differences for singleton locks
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbb committed Jun 12, 2024
1 parent e667171 commit 18544de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __init__( # noqa: PLR0913
raise RuntimeError(msg)

params_to_check = {
"is_thread_local": (thread_local, self.is_thread_local()),
"thread_local": (thread_local, self.is_thread_local()),
"timeout": (timeout, self.timeout),
"mode": (mode, self.mode),
"blocking": (blocking, self.blocking),
Expand Down
19 changes: 13 additions & 6 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,19 @@ def test_singleton_locks_are_distinct_per_lock_file(lock_type: type[BaseFileLock
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_singleton_locks_must_be_initialized_with_the_same_args(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
lock_path = tmp_path / "a"
lock = lock_type(str(lock_path), is_singleton=True) # noqa: F841

with pytest.raises(ValueError, match="Singleton lock instances cannot be initialized with differing arguments"):
lock_type(str(lock_path), timeout=10, is_singleton=True)
with pytest.raises(ValueError, match="Singleton lock instances cannot be initialized with differing arguments"):
lock_type(str(lock_path), mode=0, is_singleton=True)
args: dict[str, Any] = {"timeout": -1, "mode": 0o644, "thread_local": True, "blocking": True}
alternate_args: dict[str, Any] = {"timeout": 10, "mode": 0, "thread_local": False, "blocking": False}

lock = lock_type(str(lock_path), is_singleton=True, **args)

for arg_name in args:
general_msg = "Singleton lock instances cannot be initialized with differing arguments"
altered_args = args.copy()
altered_args[arg_name] = alternate_args[arg_name]
with pytest.raises(ValueError, match=general_msg) as exc_info:
lock_type(str(lock_path), is_singleton=True, **altered_args)
exc_info.match(arg_name) # ensure specific non-matching argument is included in exception text
del lock


@pytest.mark.skipif(hasattr(sys, "pypy_version_info"), reason="del() does not trigger GC in PyPy")
Expand Down

0 comments on commit 18544de

Please sign in to comment.