Skip to content

test(payment): pay the routers.payment import once per file, not per test - #23537

Merged
Git-on-my-level merged 3 commits into
mainfrom
lane/flake-proration
Oct 24, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
lane/flake-proration

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

What

tests/unit/test_payment_declined_proration.py tripped the fast-unit CPU duration guard on CI run 40118719182 (Backend unit shard 6/6, a pyright-fix PR that does not touch this file): 2.15–2.48s per test against the 1.00s budget.

Root cause: the three router tests each call _load_payment_router(), which re-imports routers.payment from scratch, and the autouse fixture wipes every module imported during a test out of sys.modules afterwards. Every test therefore re-paid the full real import graph inside its call phase — the exact signal the guard measures. Profiled under the same load: import stripe alone ≈ 2.1s CPU, import fastapi(+pydantic) ≈ 0.9s, models.users ≈ 0.05s.

Change

  • Move the stub-module install and the routers.payment import into a module-scoped fixture: the import graph is paid once per file, in fixture setup. The duration guard measures call-phase CPU only by design ("shared class/file setup ... is not misattributed as a per-test regression" — backend/tests/conftest.py).
  • Add a per-test autouse fixture that resets the shared stub mocks' call state (reset_mock(return_value=False, side_effect=False)), so every test starts from uncalled mocks with the once-installed stub configuration intact — the same isolation the per-test reload provided. Test 2 asserts update_user_subscription called once; tests 3–4 assert it not called, so leaky state would fail loudly.
  • Assertions and stub semantics are unchanged.
  • Stub modules now replace sys.modules entries unconditionally instead of writing mock attributes onto already-imported real modules. In a batched (non-file-isolated) pytest session the old pattern permanently mutated real modules (utils.subscription.get_plan_type_from_price_id became a bare MagicMock, which breaks test_subscription_lapsed_paid_heal.py::test_lapsed_paid_heals_from_stripe_subscription_id_directly — reproduced with the pre-change file; that pre-existing hazard is owned by test_payment_promotion_codes.py, unchanged here). This file now leaves zero real-module mutations behind.
  • Remove the three stopgap entries this file gained in fix(notes): stop persisting the 🧠 unset emoji default on deterministic and legacy paths #23512 — the tests are now genuinely fast and stay guard-visible.

Measured (backend/test.sh file-isolation runner, BACKEND_UNIT_TEST_FILE_LIST, nice -n 19)

  • Per-test call-phase CPU: 2.35s / 2.51s / 2.49s → 0.00s (<5ms) for all three (warn limit 0.001s probe run).
  • File wall: 11.05s → 2.0–3.5s. Batched duo with test_subscription_lapsed_paid_heal.py: 10 passed, no stub leak.
  • No budget raise, no skip, no allowlist entry kept.

Failure-Class: none
UI-Evidence: none -- reason: backend test files only, no web/admin/app changes

  • Round-2 hardening (Luna P2): the module fixture now also pins the utils package attributes stripe/subscription to the stub modules for the fixture window (with teardown restore), so from utils import stripe|subscription inside routers.payment binds the stubs even in a batched session where the real submodules are already attached to the package.

…test

Each of the three declined-proration tests re-imported the full real
module graph (stripe SDK ~2s CPU, FastAPI/pydantic, models.users,
config.*) inside its call phase because the per-test fixture wiped
sys.modules after every test. That tripped the fast-unit duration
guard at 2.15-2.48s against the 1.00s CPU budget (CI run 40118719182,
Backend unit shard 6/6, on a PR that did not touch this file).

Move the stub install + routers.payment import into a module-scoped
fixture: paid once, in fixture setup, which the guard intentionally
excludes from per-test CPU. Reset the shared stub mocks' call state
per test so every test still starts from uncalled mocks -- the same
isolation the per-test reload provided. Assertions unchanged.

Stub modules now replace sys.modules entries unconditionally instead
of writing mock attributes onto already-imported real modules, so a
batched pytest session no longer carries those mutations past
teardown.

Measured call-phase CPU per test: 2.35-2.51s -> 0.00s (<5ms); file
wall 11.05s -> ~2.4s in the file-isolation runner.

Removes the three stopgap fast_unit_duration_allowlist entries this
file gained in #23512: the tests are now genuinely fast and stay
guard-visible.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T20:41:59.558958Z da9d931 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Luna round 1 (PR #23537, P2): in a batched pytest session the real
utils.subscription/utils.stripe can already be bound as attributes of
the utils package when the fixture runs, and `from utils import
subscription` resolves through the parent attribute before the
sys.modules submodule fallback. routers.payment could then bind the
real module for the subscription_utils/stripe_utils aliases, so a
future test driving a checkout endpoint through this fixture would
run real subscription checks instead of the configured mocks.

Set utils.stripe/utils.subscription to the stub modules for the
fixture window and restore the original attributes at teardown.
Verified: file-isolation guard run still green (4 passed, per-test
call CPU < 5ms); batched duo with test_subscription_lapsed_paid_heal
still 10 passed with real modules restored afterwards.
#23527 landed a parallel guard fix for this file on main (per-test
payment_router fixture calling the old _load_payment_router in fixture
setup). Keep its public shape -- tests take a payment_router fixture and
alias it locally -- but back it with this lane's module-scoped fixture:
one import per file instead of one per test, shared-mock call-state reset
between tests, unconditional stub replacement (no real-module attribute
mutation in batched sessions), and utils.stripe/utils.subscription parent
pinning. Drops main's now-dead per-test fixture. Allowlist entries for
this file stay removed.
@Git-on-my-level
Git-on-my-level merged commit fcc2fec into main Oct 24, 2026
57 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/flake-proration branch October 24, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant