"""Test doubles for what a replay runs at once.""" import collections import threading import typing class Together: """What a fake replay calls to show how many replays run at once: each call returns once `parties` calls are inside it together, so it neither sleeps nor reads a clock.""" def __init__(self, parties: int) -> None: # The timeout only ends a test whose replays never run `parties` at once: it breaks the barrier, which raises in every call. self.barrier = threading.Barrier(parties, timeout=60) self.lock = threading.Lock() self.inside = 0 # The most calls inside at once, by the key each call gave. self.most: dict[typing.Hashable, int] = collections.defaultdict(int) def __call__(self, key: typing.Hashable) -> None: with self.lock: self.inside += 1 self.most[key] = max(self.most[key], self.inside) try: self.barrier.wait() finally: with self.lock: self.inside -= 1