diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2023-08-27 17:24:46 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2023-08-27 17:33:36 +0300 |
commit | 37029766cb60f4caef6d62d2564063cc14636727 (patch) | |
tree | eb1f250eb6730e7890e7e0213f4647f6bf3820b5 | |
parent | 8cfc0792df1b972b7dcd1a27120102b52c80119a (diff) | |
download | ydb-37029766cb60f4caef6d62d2564063cc14636727.tar.gz |
Intermediate changes
-rw-r--r-- | library/python/filelock/ut/test_filelock.py | 83 | ||||
-rw-r--r-- | library/python/filelock/ut/ya.make | 9 | ||||
-rw-r--r-- | library/python/filelock/ya.make | 4 |
3 files changed, 96 insertions, 0 deletions
diff --git a/library/python/filelock/ut/test_filelock.py b/library/python/filelock/ut/test_filelock.py new file mode 100644 index 0000000000..60a3722390 --- /dev/null +++ b/library/python/filelock/ut/test_filelock.py @@ -0,0 +1,83 @@ +import os +import time +import logging +import multiprocessing +import tempfile +import threading + +import library.python.filelock + + +def _acquire_lock(lock_path, out_file_path): + with library.python.filelock.FileLock(lock_path): + with open(out_file_path, "a") as out: + out.write("{}:{}\n".format(os.getpid(), time.time())) + time.sleep(2) + + +def test_filelock(): + temp_dir = tempfile.mkdtemp() + lock_path = os.path.join(temp_dir, "file.lock") + out_file_path = os.path.join(temp_dir, "out.txt") + + process_count = 5 + processes = [] + for i in range(process_count): + process = multiprocessing.Process(target=_acquire_lock, args=(lock_path, out_file_path)) + process.start() + processes.append(process) + + for process in processes: + process.join() + + pids = [] + times = [] + with open(out_file_path) as out: + content = out.read() + logging.info("Times:\n%s", content) + for line in content.strip().split("\n"): + pid, time_val = line.split(":") + pids.append(pid) + times.append(float(time_val)) + + assert len(set(pids)) == process_count + time1 = times.pop() + while times: + time2 = times.pop() + assert int(time1) - int(time2) >= 2 + time1 = time2 + + +def test_filelock_init_acquired(): + temp_dir = tempfile.mkdtemp() + lock_path = os.path.join(temp_dir, "file.lock") + + with library.python.filelock.FileLock(lock_path): + sublock = library.python.filelock.FileLock(lock_path) + del sublock + + +def test_concurrent_lock(): + filename = 'con.lock' + + def lock(): + lock = library.python.filelock.FileLock(filename) + time.sleep(1) + lock.acquire() + lock.release() + try: + os.unlink(filename) + except OSError: + pass + + threads = [] + for i in range(100): + t = threading.Thread(target=lock) + t.daemon = True + threads.append(t) + + for t in threads: + t.start() + + for t in threads: + t.join() diff --git a/library/python/filelock/ut/ya.make b/library/python/filelock/ut/ya.make new file mode 100644 index 0000000000..a62699640f --- /dev/null +++ b/library/python/filelock/ut/ya.make @@ -0,0 +1,9 @@ +PY23_TEST() + +TEST_SRCS(test_filelock.py) + +PEERDIR( + library/python/filelock +) + +END() diff --git a/library/python/filelock/ya.make b/library/python/filelock/ya.make index 00302eb3d8..55c6c23ef3 100644 --- a/library/python/filelock/ya.make +++ b/library/python/filelock/ya.make @@ -7,3 +7,7 @@ PEERDIR( ) END() + +RECURSE_FOR_TESTS( + ut +) |