Skip to content
Snippets Groups Projects
Unverified Commit 33d7c81b authored by Mashiro's avatar Mashiro Committed by GitHub
Browse files

[Refactor]: Avoid creating instances with the same name (#483)

parent 3f5e08fa
No related branches found
No related tags found
No related merge requests found
...@@ -86,6 +86,8 @@ class DefaultScope(ManagerMixin): ...@@ -86,6 +86,8 @@ class DefaultScope(ManagerMixin):
yield yield
else: else:
tmp = copy.deepcopy(cls._instance_dict) tmp = copy.deepcopy(cls._instance_dict)
# To avoid create an instance with the same name.
time.sleep(1e-6)
cls.get_instance(f'overwrite-{time.time()}', scope_name=scope_name) cls.get_instance(f'overwrite-{time.time()}', scope_name=scope_name)
try: try:
yield yield
......
...@@ -33,3 +33,16 @@ class TestDefaultScope: ...@@ -33,3 +33,16 @@ class TestDefaultScope:
).scope_name == 'test_overwrite' ).scope_name == 'test_overwrite'
assert DefaultScope.get_current_instance( assert DefaultScope.get_current_instance(
).scope_name == origin_scope.scope_name == 'origin_scope' ).scope_name == origin_scope.scope_name == 'origin_scope'
# Test overwrite default scope immediately.
# Test sequentially overwrite.
with DefaultScope.overwrite_default_scope(scope_name='test_overwrite'):
pass
with DefaultScope.overwrite_default_scope(scope_name='test_overwrite'):
pass
# Test nested overwrite.
with DefaultScope.overwrite_default_scope(scope_name='test_overwrite'):
with DefaultScope.overwrite_default_scope(
scope_name='test_overwrite'):
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment