Skip to content

Commit 84d9a66

Browse files
authored
refactor: one seconds-interval helper; coverage tracks greenlet switches (#242)
* refactor: build the server-side seconds interval in one helper * test: track greenlet switches in coverage SQLAlchemy's async layer runs each statement in a greenlet, and the tracer drops lines that run after an untracked switch. On Python 3.11, which can't use sysmon, test_batched_flush_dlq_row_stays_inline lost a line in about a third of runs and failed the 100% gate.
1 parent db6d9f5 commit 84d9a66

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

‎faststream_outbox/client.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ async def ping(self) -> bool: ...
187187
)
188188

189189

190+
def _seconds(name: str) -> "ColumnElement[typing.Any]":
191+
"""``make_interval(secs => :name)``, computed by Postgres so worker/DB clock skew can't shift it."""
192+
return func.make_interval(0, 0, 0, 0, 0, 0, bindparam(name, type_=Float))
193+
194+
190195
def _build_fetch_stmt(outbox: "Table") -> "Update":
191196
"""Build the claim statement once; queues, limit, lease TTL and the new token are bind parameters.
192197
@@ -196,8 +201,7 @@ def _build_fetch_stmt(outbox: "Table") -> "Update":
196201
"""
197202
t = outbox
198203
limit = bindparam("limit", type_=Integer)
199-
# ``make_interval(secs => :lease_ttl)`` keeps the cutoff server-side, immune to worker/DB clock skew.
200-
lease_cutoff = func.now() - func.make_interval(0, 0, 0, 0, 0, 0, bindparam("lease_ttl", type_=Float))
204+
lease_cutoff = func.now() - _seconds("lease_ttl")
201205
queue_array = ARRAY(String)
202206
queue_name = func.unnest(cast(bindparam("queues", type_=queue_array), queue_array)).label("name")
203207
queues = select(queue_name).distinct().cte("queues")
@@ -252,9 +256,7 @@ def _build_insert_stmts(table: "Table", channel: str) -> "dict[tuple[bool, bool,
252256
The notify variants call ``pg_notify`` in ``RETURNING``, so the NOTIFY costs no extra round trip and
253257
fires only when a row actually landed (an ``ON CONFLICT DO NOTHING`` skip returns no row).
254258
"""
255-
# Server-side compute keeps timing immune to worker/DB clock skew (mirrors
256-
# client.mark_pending_with_lease).
257-
activate_in = func.now() + func.make_interval(0, 0, 0, 0, 0, 0, bindparam("activate_in_seconds", type_=Float))
259+
activate_in = func.now() + _seconds("activate_in_seconds")
258260
plain = insert(table)
259261
timer = pg_insert(table).on_conflict_do_nothing(
260262
index_elements=[table.c.queue, table.c.timer_id],
@@ -604,7 +606,7 @@ async def mark_pending_with_lease(
604606
msg = "OutboxClient.mark_pending_with_lease requires a live AsyncConnection (got None)"
605607
raise TypeError(msg)
606608
t = self._table
607-
next_attempt_at_expr = func.now() + func.make_interval(0, 0, 0, 0, 0, 0, bindparam("delay", type_=Float))
609+
next_attempt_at_expr = func.now() + _seconds("delay")
608610
stmt = (
609611
update(t)
610612
.where(t.c.id == message_id, t.c.acquired_token == acquired_token)

‎pyproject.toml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ Changelog = "https://github.com/modern-python/faststream-outbox/releases"
149149

150150
[tool.coverage.run]
151151
omit = ["benchmarks/*"]
152-
# The C tracer intermittently drops executed lines in async tests; 3.11 falls back to it.
153-
core = "sysmon"
154-
disable_warnings = ["no-sysmon"]
152+
# SQLAlchemy's async layer runs statements in greenlets; untracked switches drop executed lines.
153+
concurrency = ["greenlet", "thread"]
155154

156155
[tool.coverage.report]
157156
fail_under = 100

0 commit comments

Comments
 (0)