yaoyaoding commented on code in PR #216:
URL: https://github.com/apache/tvm-ffi/pull/216#discussion_r2485020703


##########
tests/python/utils/test_filelock.py:
##########
@@ -0,0 +1,188 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Tests for the FileLock utility."""
+
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+import pytest
+from tvm_ffi.utils.lockfile import FileLock
+
+
+def test_basic_lock_acquire_and_release() -> None:
+    """Test basic lock acquisition and release."""
+    with tempfile.TemporaryDirectory() as temp_dir:
+        lock_path = Path(temp_dir) / "test.lock"
+        lock = FileLock(str(lock_path))
+
+        # Test acquire
+        assert lock.acquire() is True
+        assert lock._file_descriptor is not None
+
+        # Test release
+        lock.release()
+        assert lock._file_descriptor is None
+
+
+def test_context_manager() -> None:
+    """Test FileLock as a context manager."""
+    with tempfile.TemporaryDirectory() as temp_dir:
+        lock_path = Path(temp_dir) / "test.lock"
+
+        with FileLock(str(lock_path)) as lock:
+            assert lock._file_descriptor is not None
+            # Lock should be acquired
+            assert lock_path.exists()
+
+        # Lock should be released after exiting context
+        # Note: file may still exist but should be unlocked
+
+
+def test_exclusive_locking() -> None:
+    """Test that locks are mutually exclusive."""
+    with tempfile.TemporaryDirectory() as temp_dir:
+        lock_path = Path(temp_dir) / "test.lock"
+
+        lock1 = FileLock(str(lock_path))
+        lock2 = FileLock(str(lock_path))
+
+        # First lock should succeed
+        assert lock1.acquire() is True
+
+        # Second lock should fail (non-blocking)
+        assert lock2.acquire() is False
+
+        # After releasing first lock, second should succeed
+        lock1.release()
+        assert lock2.acquire() is True
+        lock2.release()
+
+
+def test_multiple_acquire_attempts() -> None:
+    """Test multiple acquire attempts on the same lock instance."""
+    with tempfile.TemporaryDirectory() as temp_dir:
+        lock_path = Path(temp_dir) / "test.lock"
+        lock = FileLock(str(lock_path))
+
+        # First acquire should succeed
+        assert lock.acquire() is True
+
+        # Second acquire on same instance should fail
+        # (can't acquire same lock twice)
+        assert lock.acquire() is False

Review Comment:
   I've added a check in `acquire`, to check whether the lock is held or not, 
if it's already held, return False. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to