Skip to content

Commit

Permalink
Attempt to deflake test_concurrent_lazy_init (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmills authored Oct 7, 2024
1 parent 0d4de48 commit cb530aa
Showing 1 changed file with 4 additions and 7 deletions.
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

0 comments on commit cb530aa

Please sign in to comment.