Skip to content

Commit

Permalink
Merge pull request #35434 from matthewd/faster-isolated-ar
Browse files Browse the repository at this point in the history
Copy the forking isolated test runner from railties
  • Loading branch information
matthewd committed Mar 5, 2019
2 parents a8c0ebc + 99d84c6 commit 2756419
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
52 changes: 50 additions & 2 deletions activerecord/Rakefile
Expand Up @@ -66,8 +66,38 @@ end
adapter_short = adapter == "db2" ? adapter : adapter[/^[a-z0-9]+/]
puts [adapter, adapter_short].inspect

dash_i = [
"test",
"lib",
"../activesupport/lib",
"../activemodel/lib"
].map { |dir| File.expand_path(dir, __dir__) }

dash_i.reverse_each do |x|
$:.unshift(x) unless $:.include?(x)
end
$-w = true

require "bundler/setup" unless defined?(Bundler)

# Every test file loads "cases/helper" first, so doing it
# post-fork gains us nothing.

# We need to dance around minitest autorun, though.
require "minitest"
Minitest.instance_eval do
alias _original_autorun autorun
def autorun
# no-op
end
require "cases/helper"
alias autorun _original_autorun
end

failing_files = []

test_options = ENV["TESTOPTS"].to_s.split(/[\s]+/)

test_files = (Dir["test/cases/**/*_test.rb"].reject {
|x| x.include?("/adapters/")
} + Dir["test/cases/adapters/#{adapter_short}/**/*_test.rb"]).sort
Expand All @@ -81,8 +111,26 @@ end

test_files.each do |file|
puts "--- #{file}"
success = sh(Gem.ruby, "-w", "-Itest", file)
unless success
fake_command = Shellwords.join([
FileUtils::RUBY,
"-w",
*dash_i.map { |dir| "-I#{Pathname.new(dir).relative_path_from(Pathname.pwd)}" },
file,
])
puts fake_command

# We could run these in parallel, but pretty much all of the
# railties tests already run in parallel, so ¯\_(⊙︿⊙)_/¯
Process.waitpid fork {
ARGV.clear.concat test_options
Rake.application = nil

Minitest.autorun

load file
}

unless $?.success?
failing_files << file
puts "^^^ +++"
end
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/unconnected_test.rb
Expand Up @@ -11,6 +11,12 @@ class TestUnconnectedAdapter < ActiveRecord::TestCase
def setup
@underlying = ActiveRecord::Base.connection
@specification = ActiveRecord::Base.remove_connection

# Clear out connection info from other pids (like a fork parent) too
pool_map = ActiveRecord::Base.connection_handler.instance_variable_get(:@owner_to_pool)
(pool_map.keys - [Process.pid]).each do |other_pid|
pool_map.delete(other_pid)
end
end

teardown do
Expand Down

0 comments on commit 2756419

Please sign in to comment.