Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to deflake test_concurrent_lazy_init #51

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, as_completed
from functools import lru_cache
from random import shuffle
from typing import Sequence

Expand Down Expand Up @@ -464,21 +463,19 @@ def test_concurrent_lazy_init(self) -> None:
query_per_class = 2
num_classes = num_queries // query_per_class

@lru_cache(maxsize=None)
def new_type(i):
return type(f"NewType{i}", (), {})
types = [type(f"NewType{i}", (), {}) for i in range(num_classes)]

def lazy_load_object(i):
return self.registry[new_type(i % num_classes)]
return self.registry[types[i % num_classes]]

with ThreadPoolExecutor(max_workers=query_per_class) as executor:
futures = [executor.submit(lazy_load_object, i) for i in range(num_queries)]
results = [future.result() for future in as_completed(futures)]

# group results by object id, and assert that each type is only instantiated once If each type is
# instantiated only once but accessed N times, we would see N objects for each type in the counter.
counter = Counter(map(id, results))
assert all(count == query_per_class for count in counter.values())
for count in Counter(map(id, results)).values():
self.assertEqual(count, query_per_class)


# "Test"/check type hints. These are not meant to be run by the unit test runner, but instead to
Expand Down