diff --git a/apps/api/tests/contract/test_indexing_execution_contract.py b/apps/api/tests/contract/test_indexing_execution_contract.py index f18896e4e..e0e57d9ed 100644 --- a/apps/api/tests/contract/test_indexing_execution_contract.py +++ b/apps/api/tests/contract/test_indexing_execution_contract.py @@ -211,24 +211,43 @@ def test_execution_timeout_stops_native_wait_and_returns_without_replay( # check measures cancellation of the blocked native wait itself. monkeypatch.setattr(OpenSearchRuntime, "resolve", lambda endpoint: SimpleNamespace(client=None, release=lambda: None)) + nativeWaitSeconds: float = executionBudget * 20 + nativeWait: dict[str, float] = {} + def blockedNative(self: OpenSearchLexicalIndex, *, isExact: bool, timeoutSeconds: float) -> NativeLifecycleSnapshot: calls.append("native") - gevent.sleep(executionBudget * 20) + nativeWait["startedAt"] = time.monotonic() + try: + gevent.sleep(nativeWaitSeconds) + finally: + nativeWait["endedAt"] = time.monotonic() raise LexicalIndexError("Native wait reached its deliberately long end") monkeypatch.setattr(settings, "KNOWHERE_INDEXING_TIMEOUT_SECONDS", executionBudget) monkeypatch.setattr(OpenSearchLexicalIndex, "getLifecycleSnapshot", blockedNative) # The failure transition's audit metadata goes through make_json_safe, which imports numpy and - # pandas on first use. Load them here so the timed region measures cancellation, not a cold import. + # pandas on first use. Load them here so a cold import does not eat into the settlement bound. from shared.utils.json_utils import make_json_safe make_json_safe({}) startedAt: float = time.monotonic() worker: IndexingWorker = IndexingWorker(sessionFactory=sessionmaker(engine, expire_on_commit=False)) assert worker.run(jobId) == "failed" - assert time.monotonic() - startedAt < executionBudget + 0.32 + ( - settings.KNOWHERE_INDEXING_SETTLEMENT_TIMEOUT_SECONDS if executionBudget > 0.08 else 0) + returnedAt: float = time.monotonic() assert calls == ["native"] + nativeStartedAt: float = nativeWait["startedAt"] + nativeEndedAt: float = nativeWait["endedAt"] + phases: str = (f"budget {executionBudget:.3f}s; native wait started +{nativeStartedAt - startedAt:.3f}s, " + f"ended +{nativeEndedAt - startedAt:.3f}s; run returned +{returnedAt - startedAt:.3f}s") + # Time only the cancellation. The SQL before and after the native wait is blocking psycopg2 + # work that the cooperative timer cannot interrupt. It takes about 0.15 s locally, yet whole + # runs reached 0.55-0.72 s on CI runners, so a bound on the whole run flaked without saying + # anything about cancellation. If that SQL already used up the budget, the wait must end as + # soon as it starts. + cancellationToleranceSeconds: float = 0.1 + assert nativeEndedAt - max(nativeStartedAt, startedAt + executionBudget) < cancellationToleranceSeconds, phases + # After cancellation, failure settlement runs under its own budget and does not replay the wait. + assert returnedAt - nativeEndedAt < settings.KNOWHERE_INDEXING_SETTLEMENT_TIMEOUT_SECONDS, phases with Session(engine) as session: attempt: IndexingAttempt = session.scalars(select(IndexingAttempt)).one() job: Job = session.scalars(select(Job)).one()