Skip to content

MAYBE: Harden spec suite against test-order pollution (Faker reseed + Current reset + Faker-order address flake) - #2187

Open
maebeale wants to merge 3 commits into
mainfrom
maebeale/test-order-pollution-seed18114
Open

MAYBE: Harden spec suite against test-order pollution (Faker reseed + Current reset + Faker-order address flake)#2187
maebeale wants to merge 3 commits into
mainfrom
maebeale/test-order-pollution-seed18114

Conversation

@maebeale

@maebeale maebeale commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

🤖 suggested review level: 3 Read 📖 spec-infra only (two after/before hooks + a factory-value pin); no app code — but touches all specs via the Faker hook, so worth a careful read

Hardens the spec suite against test-order pollution. Spec-only; no app code changes.

1. Reseed Faker per example (root fix for order-dependent faked data)

  • Faker draws from one shared global RNG stream, so a factory's "random" value depends on how many Faker calls ran earlier in the suite — making data order-dependent and specs flaky under some seeds.
  • spec/support/faker.rb now reseeds Random.new(42) in a before(:each) so every example draws the same fixed sequence regardless of order. Faker's .unique generator remembers used values for the whole run, so a bare reseed replays them and hits RetryLimitExceeded (that alone caused 1782 failures) — clearing it per example fixes that.
  • Verified green on the full suite (system specs included) at seeds 11334 and 18114: 6330 examples, 0 failures.

2. Reset Current (ActiveSupport::CurrentAttributes) between examples

  • Current.user / Current.source is only auto-reset by the executor around real requests/jobs. Controller/view/service specs set it in the test thread with nothing to clear it, so a stale Current leaks into later examples and silently flips model behavior that branches on it — Organization#affiliation_dates_locked (organization.rb:58) and AhoyTrackable (ahoy_trackable.rb:266).
  • Added config.after { Current.reset } beside the existing travel_back / Warden.test_reset! hooks, plus an order: :defined regression spec (red without the hook, green with it).

3. Pin address fields in upsert_address_spec

  • The concrete flake that started this: at seed 62433 the factory's random country landed on "Canada" — the value the upsert sets — so "Country" dropped from the reported changes. Pinned the changed fields to fixed, distinct values. Now also covered by fix but kept for clarity/robustness.

Scope note

  • I could not reproduce the originally-reported 11 failures (age-range / category / public-registration) from code + seed — seed 18114 was green across repeated runs and a full-suite leak detector found no residual Current or committed "AgeRange" CategoryType. Those look environment-specific (residual data in that workspace's test DB, or a selenium flake); RAILS_ENV=test bin/rails db:reset in that workspace is the likely remedy. The changes here fix the order-dependence hazards that are reproducible and real.

…er pollution

Current.user/source is only auto-reset by the executor around real
requests/jobs. Controller/view/service specs set it in the test thread
with no executor to clear it, so a stale Current leaks into later
examples and silently flips model behavior that branches on it
(Organization affiliation-lock validation, AhoyTrackable lifecycle
tracking). Mirror the existing travel_back / Warden.test_reset! hooks
with an after-each reset, plus an order:defined regression guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 13, 2026 13:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Faker::Config uses one shared, order-seeded stream (Random.new(42)), so a
factory's random country/street/zip is determined by the example's position
in that stream. Under some seeds (e.g. 62433) the existing address's country
came out as 'Canada' — the exact value the upsert sets — so 'Country' dropped
out of the reported changes and the example failed. Pin the changed fields to
fixed, distinct values so the assertion is order-independent.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 13, 2026 13:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@maebeale maebeale changed the title Reset Current attributes between specs to prevent test-order pollution Harden spec suite against test-order pollution (Current reset + Faker-order address flake) Aug 13, 2026
@maebeale
maebeale marked this pull request as ready for review August 13, 2026 13:57
Faker draws from one shared global RNG stream, so a factory's value depends
on how many Faker calls ran earlier in the suite — making data order-dependent
and specs flaky under some seeds (the upsert_address country=Canada collision
was one instance). Reseed Random.new(42) before each example so every example
draws the same fixed sequence regardless of order. Faker's .unique generator
remembers used values for the whole run, so a bare reseed replays them and hits
RetryLimitExceeded (1782 failures); clearing it per example fixes that.

Verified green on the full suite (system specs included) at seeds 11334 and
18114: 6330 examples, 0 failures.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 13, 2026 14:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@maebeale
maebeale requested a review from jmilljr24 August 13, 2026 14:59
@maebeale maebeale changed the title Harden spec suite against test-order pollution (Current reset + Faker-order address flake) MAYBE: Harden spec suite against test-order pollution (Faker reseed + Current reset + Faker-order address flake) Aug 13, 2026
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.

2 participants