test(client): remove polling stop race
This commit is contained in:
@@ -106,12 +106,15 @@ class FakeGateway:
|
|||||||
def __init__(self, outcomes: list[object] | None = None, gate: threading.Event | None = None) -> None:
|
def __init__(self, outcomes: list[object] | None = None, gate: threading.Event | None = None) -> None:
|
||||||
self.outcomes = list(outcomes or [None])
|
self.outcomes = list(outcomes or [None])
|
||||||
self.gate = gate
|
self.gate = gate
|
||||||
|
self.entered = threading.Event()
|
||||||
self.calls = 0
|
self.calls = 0
|
||||||
|
|
||||||
def claim_next(self, profile_id: str):
|
def claim_next(self, profile_id: str):
|
||||||
self.calls += 1
|
self.calls += 1
|
||||||
|
self.entered.set()
|
||||||
if self.gate is not None:
|
if self.gate is not None:
|
||||||
self.gate.wait(2)
|
if not self.gate.wait(5):
|
||||||
|
raise RuntimeError("test_gateway_release_timeout")
|
||||||
outcome = self.outcomes.pop(0) if self.outcomes else None
|
outcome = self.outcomes.pop(0) if self.outcomes else None
|
||||||
if isinstance(outcome, Exception):
|
if isinstance(outcome, Exception):
|
||||||
raise outcome
|
raise outcome
|
||||||
@@ -251,7 +254,9 @@ class PollingCoordinatorTests(unittest.TestCase):
|
|||||||
visible: list[object] = []
|
visible: list[object] = []
|
||||||
coordinator.claim_visible.connect(visible.append)
|
coordinator.claim_visible.connect(visible.append)
|
||||||
coordinator.start()
|
coordinator.start()
|
||||||
wait_until(lambda: coordinator.state == PollingState.CLAIMING)
|
# CLAIMING 状态先于 queued worker 实际进入 claim_next;必须等待
|
||||||
|
# worker 入口,避免与 FakeGateway 的超时边界竞争而产生套件级偶发失败。
|
||||||
|
wait_until(lambda: coordinator.state == PollingState.CLAIMING and gateway.entered.is_set())
|
||||||
coordinator._begin_claim(coordinator._epoch)
|
coordinator._begin_claim(coordinator._epoch)
|
||||||
self.assertEqual(gateway.calls, 1)
|
self.assertEqual(gateway.calls, 1)
|
||||||
coordinator.stop()
|
coordinator.stop()
|
||||||
|
|||||||
Reference in New Issue
Block a user