Use an in-memory, shared-cache SQLite database for tests#508
Merged
Conversation
The test suite ran on per-worker SQLite files. Under mutant's parallel kill-forks that shared one file, the database lock made results non-deterministic (a subject flaked between 5 and 9 surviving mutations across runs). Switch the test environment to `file::memory:?cache=shared`: - cache=shared lets every connection in a process (the test thread and Capybara's Puma server thread) see the same schema and data, so browser (js: true) tests keep working. - Shared cache is per-process, so parallel_test workers and mutant kill-forks are isolated automatically - no file, no lock. TEST_ENV_NUMBER is no longer needed to separate databases. - rails_helper loads db/schema.rb into the empty in-memory database at boot instead of the file-based maintain_test_schema!, and skips the parallel-worker reconnect that would drop the shared cache. - Drop the now-unnecessary db:migrate/parallel:prepare steps from the test and coverage workflows; the schema loads at boot. Verified: the full model/request/service/helper/lib/controller/view/seed suites and the feature suite (including a js browser test) pass, and mutant now returns identical results across repeated parallel runs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CLseCgvqE2jdan8K57ENJn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The test suite ran on per-worker SQLite files. Under mutant's parallel kill-forks (which all share one test file), the SQLite database lock made results non-deterministic — the
Eventsubject flaked between 5 and 9 surviving mutations across identical runs, because a lock-timeout error during a mutation was counted as a "kill".This switches the test environment to an in-memory, shared-cache database:
file::memory:?cache=shared.Why shared-cache
:memory:gives every connection its own private database, which breaks any test that uses two connections — notablyjs: truebrowser tests, where the test thread writes data and Capybara's Puma server thread must read it.cache=sharedmakes all connections within a process share one in-memory database, so browser tests keep working.parallel_testworkers and mutant kill-forks are isolated automatically. There's no file and no lock, andTEST_ENV_NUMBERis no longer needed to keep workers apart.Changes
config/database.yml—testnow usesfile::memory:?cache=sharedwith in-memory-appropriate pragmas (WAL/mmap dropped).spec/rails_helper.rb— loadsdb/schema.rbinto the empty in-memory DB at boot (an in-memory DB starts empty every run) instead of the file-basedmaintain_test_schema!, and skips the parallel-worker reconnect that would drop the shared cache and its schema..github/workflows/test-ruby.yml,coverage.yml— drop the now-unnecessarydb:migrate/parallel:preparesteps (parallel:prepareactually errors against an in-memory DB); the schema loads at boot.CLAUDE.md— document the in-memory test database.Validation (all on the in-memory DB)
models,requests,services,helpers,lib,controllers,views,seedssuites: 0 failures.featuressuite in parallel, including ajs: truebrowser test driving Chromium against the Puma server: 0 failures.parallel_rspec -n 2across model specs: 0 failures, with noparallel:prepare.Eventreturned an identical 9 surviving mutations (92.56%) with nodatabase is lockederrors — versus the 5↔9 flakiness on files.🤖 Generated with Claude Code
https://claude.ai/code/session_01CLseCgvqE2jdan8K57ENJn
Generated by Claude Code