From c567ff64fe0ffdb752e761b39838a839e1f624a7 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Wed, 15 Jun 2011 11:33:25 -0600 Subject: [PATCH] support running specs in parallel Change-Id: I521adc994f5f2bd58099a89315468111435dbbf8 Reviewed-on: https://gerrit.instructure.com/4218 Tested-by: Hudson Reviewed-by: Brian Palmer --- config/database.yml.example | 6 ++++++ config/database.yml.sqlite-example | 6 ++++++ config/initializers/in-memory-test.rb | 11 +++++++++++ lib/tasks/rspec.rake | 23 +++++++++++++++++++++-- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 config/initializers/in-memory-test.rb diff --git a/config/database.yml.example b/config/database.yml.example index 6c18bc4c352..2c026d1eaaa 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -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 diff --git a/config/database.yml.sqlite-example b/config/database.yml.sqlite-example index ec47e8b3c71..a2e5cf4021f 100644 --- a/config/database.yml.sqlite-example +++ b/config/database.yml.sqlite-example @@ -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 diff --git a/config/initializers/in-memory-test.rb b/config/initializers/in-memory-test.rb new file mode 100644 index 00000000000..5ede8deae52 --- /dev/null +++ b/config/initializers/in-memory-test.rb @@ -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 diff --git a/lib/tasks/rspec.rake b/lib/tasks/rspec.rake index a58ee570fc7..227f06c0d42 100644 --- a/lib/tasks/rspec.rake +++ b/lib/tasks/rspec.rake @@ -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