Skip to content
Open
Show file tree
Hide file tree
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
39 changes: 25 additions & 14 deletions tests/unit/test_pg_retirement_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import asyncio
import os
import time
import weakref
from types import SimpleNamespace
from unittest.mock import AsyncMock
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
22 changes: 15 additions & 7 deletions tests/unit/test_pg_retirement_settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import asyncio
import sys
import time
import weakref
from types import SimpleNamespace
from unittest.mock import AsyncMock
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)


Expand All @@ -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()
Expand Down Expand Up @@ -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"
Expand Down
Loading