Skip to content

Parallel test databases are truncated before each run, but non-parallel test databases are not #46820

Closed
@ghiculescu

Description

@ghiculescu

Steps to reproduce

Repro here: https://github.com/ghiculescu/parallel-tests-repro. The key bits are as follows.

In the test helper:

parallelize(workers: :number_of_processors, threshold: 2)

# Load some data in the test database before tests run.
# We don't use fixtures for this because it's a huge static dataset.
# A good example might be loading data from the countries gem or something like that.
# We don't do this in `setup` because it is slow. We want to just do it once (per database), before all tests.
10.times {|n| User.create!(name: "User #{n}") }

The test:

test "static data is created in test_helper.rb" do
  assert_equal 10, User.count
end

This just confirms that the static data was successfully loaded and is available to the test.

Expected behavior

I expect the test to pass if you run in singular or in parallel.

rails db:reset test # run tests in singular, should pass
PARALLEL_WORKERS=2 rails db:reset test # run tests in parallel, should pass

Actual behavior

Running not in parallel passes.

Running in parallel fails.

PARALLEL_WORKERS=2 rails db:reset test
Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
Created database 'db/development.sqlite3'
Created database 'db/test.sqlite3'
Running 1 tests in parallel using 2 processes
Run options: --seed 10094

# Running:

F

Failure:
UserTest#test_static_data_is_created_in_test_helper.rb_-_0
Expected: 10
  Actual: 0

It is failing because

create_and_load_schema(i, env_name: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call)

calls

which truncates all tables. The equivalent does not happen if you aren't running in parallel.

I tried commenting out that line, and all tests still passed, but I assumed there's a reason for it (though I can't think of one...) so wanted to make an issue for discussion first.

System configuration

Rails version: main

Ruby version: 3.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions