diff --git a/tests/unit/test_pg_retirement_ownership.py b/tests/unit/test_pg_retirement_ownership.py index 6b6ad05fb5..3e3e9b10db 100644 --- a/tests/unit/test_pg_retirement_ownership.py +++ b/tests/unit/test_pg_retirement_ownership.py @@ -4,7 +4,6 @@ import asyncio import os -import time import weakref from types import SimpleNamespace from unittest.mock import AsyncMock @@ -167,7 +166,7 @@ async def test_every_death_recovery_path_requires_positive_backend_absence( backend(world, provider_process.participant_tag(UUID(int=712))) if backend_live: backend(world, provider_process.participant_tag(TARGET)) - await recover(path, world, end=time.monotonic() + 0.2) + await recover(path, world, end=asyncio.get_running_loop().time() + 0.2) assert world.censuses and all(not legacy for _, legacy, _ in world.censuses) assert len(world.rows) == (0 if backend_live else 1) if world.rows: @@ -186,7 +185,9 @@ async def test_raw_legacy_retired_row_waits_for_all_generic_sessions(ownership, backend(world, provider_process.participant_tag(UUID(int=712))) if generic_idle: backend(world, "loom") - result = await barrier.retire_participant(TARGET, ch=world, end=time.monotonic() + 0.2) + result = await barrier.retire_participant( + TARGET, ch=world, end=asyncio.get_running_loop().time() + 0.2 + ) assert result == ("live" if generic_idle else "retired") assert len(world.rows) == (0 if generic_idle else 1) assert world.censuses and all(legacy for _, legacy, _ in world.censuses) @@ -205,7 +206,10 @@ async def test_raw_clickhouse_recovery_requires_no_postgres_io(ownership, monkey connect = AsyncMock(side_effect=AssertionError("CH recovery cannot contact PG")) monkeypatch.setattr(asyncpg, "connect", connect) assert ( - await barrier.retire_participant(TARGET, ch=world, end=time.monotonic() + 0.2) == "retired" + await barrier.retire_participant( + TARGET, ch=world, end=asyncio.get_running_loop().time() + 0.2 + ) + == "retired" ) assert len(world.rows) == 1 and not world.censuses connect.assert_not_awaited() @@ -245,12 +249,12 @@ async def test_failed_close_is_retained_until_later_positive_census(ownership): pool = Pool(fail=True) pg._pools[loop] = pool peer = backend(world, provider_process.participant_tag(provider_process.process_identity())) - assert not await barrier.retire_process(world, end=time.monotonic() + 0.2) + assert not await barrier.retire_process(world, end=asyncio.get_running_loop().time() + 0.2) child = pg._closing[loop] assert child.done() and pg._pools[loop] is pool assert pool.close_calls == 1 and not world.rows world.sessions.pop(peer.pid) - assert await barrier.retire_process(world, end=time.monotonic() + 0.2) + assert await barrier.retire_process(world, end=asyncio.get_running_loop().time() + 0.2) assert len(world.rows) == 1 and pool.close_calls == 1 assert loop not in pg._pools and loop not in pg._closing @@ -264,10 +268,10 @@ async def test_pending_resistant_close_keeps_one_child_and_ownership(ownership): pool = Pool(finish=finish) pg._pools[loop] = pool try: - assert not await pg.close_pool_until(end=time.monotonic() + 0.01) + assert not await pg.close_pool_until(end=asyncio.get_running_loop().time() + 0.01) child = pg._closing[loop] assert not child.done() and gate.active == 1 - assert not await pg.close_pool_until(end=time.monotonic()) + assert not await pg.close_pool_until(end=asyncio.get_running_loop().time()) assert pg._closing[loop] is child and pool.close_calls == 1 assert not gate.quiescent and not world.rows finally: @@ -276,7 +280,7 @@ async def test_pending_resistant_close_keeps_one_child_and_ownership(ownership): await asyncio.gather(pg._closing[loop], return_exceptions=True) await asyncio.sleep(0) assert gate.quiescent and pg._pools[loop] is pool - assert await barrier.retire_process(world, end=time.monotonic() + 0.2) + assert await barrier.retire_process(world, end=asyncio.get_running_loop().time() + 0.2) assert len(world.rows) == 1 and pool.close_calls == 1 @@ -295,7 +299,7 @@ async def close(**kwargs): pass conn = SimpleNamespace(close=close, terminate=lambda: aborted.append(True)) - start = time.monotonic() + start = asyncio.get_running_loop().time() end = start + 0.03 task = asyncio.create_task(pg.close_owned(conn, end=end)) try: @@ -535,7 +539,7 @@ async def test_other_live_event_loop_pool_is_not_silently_ignored_at_retirement( pg._pools[other] = pool try: with pytest.raises(retirement.ProcessStopping): - await pg.close_pool_until(end=time.monotonic() + 0.2) + await pg.close_pool_until(end=asyncio.get_running_loop().time() + 0.2) assert pg._pools[other] is pool and pool.close_calls == 0 finally: pg._pools.pop(other) @@ -576,7 +580,9 @@ def unrelated_default(): monkeypatch.setattr(barrier, "_default_client", unrelated_default) assert ( - await barrier.retire_participant(TARGET, ch=ownership, end=time.monotonic() + 0.2) + await barrier.retire_participant( + TARGET, ch=ownership, end=asyncio.get_running_loop().time() + 0.2 + ) == "retired" ) assert len(ownership.rows) == 1 and ownership.censuses @@ -586,13 +592,18 @@ async def test_verifier_close_failure_prevents_certificate_then_positive_close_r target_row(ownership) ownership.verifier_close_ack = False assert ( - await barrier.retire_participant(TARGET, ch=ownership, end=time.monotonic() + 0.2) == "live" + await barrier.retire_participant( + TARGET, ch=ownership, end=asyncio.get_running_loop().time() + 0.2 + ) + == "live" ) assert not ownership.rows and ownership.sessions ownership.sessions.clear() ownership.verifier_close_ack = True assert ( - await barrier.retire_participant(TARGET, ch=ownership, end=time.monotonic() + 0.2) + await barrier.retire_participant( + TARGET, ch=ownership, end=asyncio.get_running_loop().time() + 0.2 + ) == "retired" ) assert len(ownership.rows) == 1 and not ownership.sessions diff --git a/tests/unit/test_pg_retirement_settlement.py b/tests/unit/test_pg_retirement_settlement.py index 4f7bd93365..391b9c22a1 100644 --- a/tests/unit/test_pg_retirement_settlement.py +++ b/tests/unit/test_pg_retirement_settlement.py @@ -4,7 +4,6 @@ import asyncio import sys -import time import weakref from types import SimpleNamespace from unittest.mock import AsyncMock @@ -18,6 +17,7 @@ from server.insights.app import provider_command as command from server.insights.store import provider_participants as store from server.settings import settings +from tests.unit.clock_fake import ManualClock, idle_time pytestmark = pytest.mark.asyncio DEPLOYMENT = "00000000-0000-0000-0000-000000000019" @@ -112,7 +112,7 @@ async def insert(self, table, rows, **kwargs): @pytest.fixture -def world(monkeypatch): +async def world(monkeypatch): import asyncpg from server.core import ch, rmt_carry @@ -146,7 +146,15 @@ async def enroll(): for module in (barrier, command): monkeypatch.setattr(module, "drain_task_sets", AsyncMock(return_value=True)) monkeypatch.setattr(module, "flush_registered_buffers", AsyncMock(return_value=True)) - return world + + # The fake authority spends its deadline waiting for work, independent of runner pauses. + loop = asyncio.get_running_loop() + clock = ManualClock(t=loop.time()) + monkeypatch.setattr(barrier, "monotonic", clock.monotonic) + monkeypatch.setattr(cell_control, "time", clock) + monkeypatch.setattr(pg, "time", clock) + with idle_time(loop, clock): + yield world async def owned_command(world): @@ -284,7 +292,7 @@ async def test_later_positive_backend_absence_can_resolve_an_unknown_close(world assert found.value is first assert not world.rows, "the first unknown attempt must remain uncertified" world.sessions.clear() # The authority now positively reports backend absence. - assert await barrier._retire_until(world, time.monotonic() + 0.2) + assert await barrier._retire_until(world, asyncio.get_running_loop().time() + 0.2) assert_certificate(world) @@ -306,12 +314,12 @@ async def work(): task = owner._start(work) try: await entered.wait() - owner.close_end = time.monotonic() + owner.close_end = asyncio.get_running_loop().time() await owner._stop({task}) assert not task.done() gate.stopped() with pytest.raises(TimeoutError): - await barrier._retire_until(world, time.monotonic() + 0.02) + await barrier._retire_until(world, asyncio.get_running_loop().time() + 0.02) assert not world.rows, "a pending application child can still issue work" finally: finish.set() @@ -340,7 +348,7 @@ async def test_retirement_requires_fresh_valid_authority_and_backend_evidence(wo gate.stopped() result = False try: - result = await barrier._retire_until(world, time.monotonic() + 0.02) + result = await barrier._retire_until(world, asyncio.get_running_loop().time() + 0.02) except (TimeoutError, pg.AuthorityMismatch, OSError, pg.PostgresUnavailableError): pass assert not world.rows, "unknown or contradictory authority evidence cannot certify"