support running specs in parallel

Change-Id: I521adc994f5f2bd58099a89315468111435dbbf8
Reviewed-on: https://gerrit.instructure.com/4218
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Brian Palmer <brianp@instructure.com>
This commit is contained in:
JT Olds 2011-06-15 11:33:25 -06:00
parent 7af31d40e8
commit c567ff64fe
4 changed files with 44 additions and 2 deletions

View File

@ -13,6 +13,12 @@ test:
username: canvas
timeout: 5000
test-in-memory:
adapter: sqlite3
encoding: utf8
database: ':memory:'
pool: 1
development:
adapter: mysql
encoding: utf8

View File

@ -11,6 +11,12 @@ test:
timeout: 5000
pool: 5
test-in-memory:
adapter: sqlite3
encoding: utf8
database: ':memory:'
pool: 1
development:
adapter: sqlite3
encoding: utf8

View File

@ -0,0 +1,11 @@
def in_memory_database?
ENV["RAILS_ENV"] == "test" and
ENV["IN_MEMORY_DB"] and
Rails::Configuration.new.database_configuration.has_key?('test-in-memory') and
Rails::Configuration.new.database_configuration['test-in-memory']['database'] == ':memory:'
end
if in_memory_database?
ActiveRecord::Base.establish_connection(Rails::Configuration.new.database_configuration['test-in-memory'])
load "#{Rails.root}/db/schema.rb"
end

View File

@ -42,12 +42,31 @@ Spec::Rake::SpecTask.new(:spec) do |t|
if ENV['SINGLE_TEST']
t.spec_opts += ['-e', %{"#{ENV['SINGLE_TEST']}"}]
end
t.spec_files = ['spec'] + FileList['vendor/plugins/*/spec_canvas']
spec_files = ['spec'] + FileList['vendor/plugins/*/spec_canvas']
Gem.loaded_specs.values.each do |spec|
path = spec.full_gem_path
spec_canvas_path = File.expand_path(path+"/spec_canvas")
next unless File.directory?(spec_canvas_path)
t.spec_files << spec_canvas_path
spec_files << spec_canvas_path
end
if ENV['IN_MEMORY_DB']
N_PROCESSES = [ENV['IN_MEMORY_DB'].to_i, 1].max
spec_files = spec_files.map{|x| Dir[x + "/**/*_spec.rb" ]}.flatten.sort.in_groups_of(N_PROCESSES)
processes = []
Signal.trap "SIGINT", (lambda { Process.kill "-KILL", Process.getpgid(0) })
child = false
N_PROCESSES.times do |j|
pid = Process.fork
unless pid
child = true
t.spec_files = spec_files.map{|x|x[j]}.compact
break
end
processes << pid
end
exit Process.waitall.map(&:last).map(&:exitstatus).count{|x|x != 0} unless child
else
t.spec_files = spec_files
end
end