aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/func/ut/test_func.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-06-04 09:18:28 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-06-04 09:25:53 +0300
commit2f30887a9d7c920d55d57d65e4111495f0d57f42 (patch)
tree8dcdda885a8f109e3e3685332b0120845354dfe0 /library/python/func/ut/test_func.py
parent83e2d309d5d6613e344ca00a7d25fa473ceea74b (diff)
downloadydb-2f30887a9d7c920d55d57d65e4111495f0d57f42.tar.gz
Intermediate changes
Diffstat (limited to 'library/python/func/ut/test_func.py')
-rw-r--r--library/python/func/ut/test_func.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/library/python/func/ut/test_func.py b/library/python/func/ut/test_func.py
index 70a10d62cb..90fe125a11 100644
--- a/library/python/func/ut/test_func.py
+++ b/library/python/func/ut/test_func.py
@@ -3,6 +3,7 @@ import multiprocessing
import random
import threading
import time
+import six
import library.python.func as func
@@ -135,6 +136,32 @@ def test_flatten_dict():
assert func.flatten_dict({"a": 1, "b": {"c": {"d": 2}}}, separator="/") == {"a": 1, "b/c/d": 2}
+def test_threadsafe_singleton():
+ class ShouldBeSingle(six.with_metaclass(func.Singleton, object)):
+ def __new__(cls, *args, **kwargs):
+ time.sleep(0.1)
+ return super(ShouldBeSingle, cls).__new__(cls, *args, **kwargs)
+
+ threads_count = 100
+ threads = [None] * threads_count
+ results = [None] * threads_count
+
+ def class_factory(results, i):
+ time.sleep(0.1)
+ results[i] = ShouldBeSingle()
+
+ for i in range(threads_count):
+ threads[i] = threading.Thread(target=class_factory, args=(results, i))
+
+ for i in range(threads_count):
+ threads[i].start()
+
+ for i in range(threads_count):
+ threads[i].join()
+
+ assert len(set(results)) == 1
+
+
def test_memoize_thread_local():
class Counter(object):
def __init__(self, s):