diff options
author | robot-piglet <[email protected]> | 2025-08-28 14:27:58 +0300 |
---|---|---|
committer | robot-piglet <[email protected]> | 2025-08-28 14:57:06 +0300 |
commit | 81d828c32c8d5477cb2f0ce5da06a1a8d9392ca3 (patch) | |
tree | 3081d566f0d5158d76e9093261344f6406fd09f7 /contrib/python/portalocker/py2/tests | |
parent | 77ea11423f959e51795cc3ef36a48d808b4ffb98 (diff) |
Intermediate changes
commit_hash:d5b1af16dbe9030537a04c27eb410c88c2f496cd
Diffstat (limited to 'contrib/python/portalocker/py2/tests')
-rw-r--r-- | contrib/python/portalocker/py2/tests/__init__.py | 0 | ||||
-rw-r--r-- | contrib/python/portalocker/py2/tests/conftest.py | 14 | ||||
-rw-r--r-- | contrib/python/portalocker/py2/tests/temporary_file_lock.py | 14 | ||||
-rw-r--r-- | contrib/python/portalocker/py2/tests/test_combined.py | 15 | ||||
-rw-r--r-- | contrib/python/portalocker/py2/tests/tests.py | 202 | ||||
-rw-r--r-- | contrib/python/portalocker/py2/tests/ya.make | 20 |
6 files changed, 265 insertions, 0 deletions
diff --git a/contrib/python/portalocker/py2/tests/__init__.py b/contrib/python/portalocker/py2/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/contrib/python/portalocker/py2/tests/__init__.py diff --git a/contrib/python/portalocker/py2/tests/conftest.py b/contrib/python/portalocker/py2/tests/conftest.py new file mode 100644 index 00000000000..a92117e3951 --- /dev/null +++ b/contrib/python/portalocker/py2/tests/conftest.py @@ -0,0 +1,14 @@ +import py +import pytest + + +def tmpfile(tmpdir_factory): + tmpdir = tmpdir_factory.mktemp('temp') + filename = tmpdir.join('tmpfile') + yield str(filename) + try: + filename.remove(ignore_errors=True) + except (py.error.EBUSY, py.error.ENOENT): + pass + diff --git a/contrib/python/portalocker/py2/tests/temporary_file_lock.py b/contrib/python/portalocker/py2/tests/temporary_file_lock.py new file mode 100644 index 00000000000..b250bad6510 --- /dev/null +++ b/contrib/python/portalocker/py2/tests/temporary_file_lock.py @@ -0,0 +1,14 @@ +import os +import portalocker + + +def test_temporary_file_lock(tmpfile): + with portalocker.TemporaryFileLock(tmpfile): + pass + + assert not os.path.isfile(tmpfile) + + lock = portalocker.TemporaryFileLock(tmpfile) + lock.acquire() + del lock + diff --git a/contrib/python/portalocker/py2/tests/test_combined.py b/contrib/python/portalocker/py2/tests/test_combined.py new file mode 100644 index 00000000000..594de74b9ba --- /dev/null +++ b/contrib/python/portalocker/py2/tests/test_combined.py @@ -0,0 +1,15 @@ +import sys + + +def test_combined(tmpdir): + from distutils import dist + import setup + + output_file = tmpdir.join('combined.py') + combine = setup.Combine(dist.Distribution()) + combine.output_file = str(output_file) + combine.run() + sys.path.append(output_file.dirname) + import combined + assert combined + diff --git a/contrib/python/portalocker/py2/tests/tests.py b/contrib/python/portalocker/py2/tests/tests.py new file mode 100644 index 00000000000..a6567426be0 --- /dev/null +++ b/contrib/python/portalocker/py2/tests/tests.py @@ -0,0 +1,202 @@ +from __future__ import print_function +from __future__ import with_statement + +import pytest +import portalocker + + +def test_exceptions(tmpfile): + # Open the file 2 times + a = open(tmpfile, 'a') + b = open(tmpfile, 'a') + + # Lock exclusive non-blocking + lock_flags = portalocker.LOCK_EX | portalocker.LOCK_NB + + # First lock file a + portalocker.lock(a, lock_flags) + + # Now see if we can lock file b + with pytest.raises(portalocker.LockException): + portalocker.lock(b, lock_flags) + + # Cleanup + a.close() + b.close() + + +def test_with_timeout(tmpfile): + # Open the file 2 times + with pytest.raises(portalocker.AlreadyLocked): + with portalocker.Lock(tmpfile, timeout=0.1) as fh: + print('writing some stuff to my cache...', file=fh) + with portalocker.Lock(tmpfile, timeout=0.1, mode='wb', + fail_when_locked=True): + pass + print('writing more stuff to my cache...', file=fh) + + +def test_without_timeout(tmpfile): + # Open the file 2 times + with pytest.raises(portalocker.LockException): + with portalocker.Lock(tmpfile, timeout=None) as fh: + print('writing some stuff to my cache...', file=fh) + with portalocker.Lock(tmpfile, timeout=None, mode='w'): + pass + print('writing more stuff to my cache...', file=fh) + + +def test_without_fail(tmpfile): + # Open the file 2 times + with pytest.raises(portalocker.LockException): + with portalocker.Lock(tmpfile, timeout=0.1) as fh: + print('writing some stuff to my cache...', file=fh) + lock = portalocker.Lock(tmpfile, timeout=0.1) + lock.acquire(check_interval=0.05, fail_when_locked=False) + + +def test_simple(tmpfile): + with open(tmpfile, 'w') as fh: + fh.write('spam and eggs') + + fh = open(tmpfile, 'r+') + portalocker.lock(fh, portalocker.LOCK_EX) + + fh.seek(13) + fh.write('foo') + + # Make sure we didn't overwrite the original text + fh.seek(0) + assert fh.read(13) == 'spam and eggs' + + portalocker.unlock(fh) + fh.close() + + +def test_truncate(tmpfile): + with open(tmpfile, 'w') as fh: + fh.write('spam and eggs') + + with portalocker.Lock(tmpfile, mode='a+') as fh: + # Make sure we didn't overwrite the original text + fh.seek(0) + assert fh.read(13) == 'spam and eggs' + + with portalocker.Lock(tmpfile, mode='w+') as fh: + # Make sure we truncated the file + assert fh.read() == '' + + +def test_class(tmpfile): + lock = portalocker.Lock(tmpfile) + lock2 = portalocker.Lock(tmpfile, fail_when_locked=False, timeout=0.01) + + with lock: + lock.acquire() + + with pytest.raises(portalocker.LockException): + with lock2: + pass + + with lock2: + pass + + +def test_acquire_release(tmpfile): + lock = portalocker.Lock(tmpfile) + lock2 = portalocker.Lock(tmpfile, fail_when_locked=False) + + lock.acquire() # acquire lock when nobody is using it + with pytest.raises(portalocker.LockException): + # another party should not be able to acquire the lock + lock2.acquire(timeout=0.01) + + # re-acquire a held lock is a no-op + lock.acquire() + + lock.release() # release the lock + lock.release() # second release does nothing + + +def test_rlock_acquire_release_count(tmpfile): + lock = portalocker.RLock(tmpfile) + # Twice acquire + h = lock.acquire() + assert not h.closed + lock.acquire() + assert not h.closed + + # Two release + lock.release() + assert not h.closed + lock.release() + assert h.closed + + +def test_rlock_acquire_release(tmpfile): + lock = portalocker.RLock(tmpfile) + lock2 = portalocker.RLock(tmpfile, fail_when_locked=False) + + lock.acquire() # acquire lock when nobody is using it + with pytest.raises(portalocker.LockException): + # another party should not be able to acquire the lock + lock2.acquire(timeout=0.01) + + # Now acquire again + lock.acquire() + + lock.release() # release the lock + lock.release() # second release does nothing + + +def test_release_unacquired(tmpfile): + with pytest.raises(portalocker.LockException): + portalocker.RLock(tmpfile).release() + + +def test_exlusive(tmpfile): + with open(tmpfile, 'w') as fh: + fh.write('spam and eggs') + + fh = open(tmpfile, 'r') + portalocker.lock(fh, portalocker.LOCK_EX | portalocker.LOCK_NB) + + # Make sure we can't read the locked file + with pytest.raises(portalocker.LockException): + with open(tmpfile, 'r') as fh2: + portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB) + fh2.read() + + # Make sure we can't write the locked file + with pytest.raises(portalocker.LockException): + with open(tmpfile, 'w+') as fh2: + portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB) + fh2.write('surprise and fear') + + # Make sure we can explicitly unlock the file + portalocker.unlock(fh) + fh.close() + + +def test_shared(tmpfile): + with open(tmpfile, 'w') as fh: + fh.write('spam and eggs') + + f = open(tmpfile, 'r') + portalocker.lock(f, portalocker.LOCK_SH | portalocker.LOCK_NB) + + # Make sure we can read the locked file + with open(tmpfile, 'r') as fh2: + portalocker.lock(fh2, portalocker.LOCK_SH | portalocker.LOCK_NB) + assert fh2.read() == 'spam and eggs' + + # Make sure we can't write the locked file + with pytest.raises(portalocker.LockException): + with open(tmpfile, 'w+') as fh2: + portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB) + fh2.write('surprise and fear') + + # Make sure we can explicitly unlock the file + portalocker.unlock(f) + f.close() + diff --git a/contrib/python/portalocker/py2/tests/ya.make b/contrib/python/portalocker/py2/tests/ya.make new file mode 100644 index 00000000000..6917e170a2a --- /dev/null +++ b/contrib/python/portalocker/py2/tests/ya.make @@ -0,0 +1,20 @@ +PY2TEST() + +SUBSCRIBER(g:python-contrib) + +NO_LINT() + +PEERDIR( + contrib/python/portalocker +) + +TEST_SRCS( + __init__.py + conftest.py + temporary_file_lock.py + # Tests intallation. + # test_combined.py + tests.py +) + +END() |