From b375dcb14a60bf83ee2e1ef23acdca95bb4ce40d Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 1 Apr 2019 09:27:49 +0100 Subject: [PATCH] DEV: Introduce parallel rspec testing Adds the parallel_tests gem, and redis/postgres configuration for running rspec tests in parallel. To use: ``` rake parallel:rake[db:create] rake parallel:rake[db:migrate] rake parallel:spec ``` This brings the test suite from 12m20s to 3m11s on my macOS machine --- .rspec_parallel | 4 ++++ Gemfile | 1 + Gemfile.lock | 3 +++ app/models/global_setting.rb | 4 ++++ config/boot.rb | 16 ++++++++++++++++ 5 files changed, 28 insertions(+) create mode 100644 .rspec_parallel diff --git a/.rspec_parallel b/.rspec_parallel new file mode 100644 index 0000000000000..252e250b4924c --- /dev/null +++ b/.rspec_parallel @@ -0,0 +1,4 @@ +--format progress +--format ParallelTests::RSpec::RuntimeLogger --out tmp/parallel_runtime_rspec.log +--format ParallelTests::RSpec::SummaryLogger --out tmp/spec_summary.log +--format ParallelTests::RSpec::FailuresLogger --out tmp/failing_specs.log diff --git a/Gemfile b/Gemfile index 525fa178017ba..0ab635464857f 100644 --- a/Gemfile +++ b/Gemfile @@ -140,6 +140,7 @@ group :test, :development do gem 'pry-nav' gem 'byebug', require: ENV['RM_INFO'].nil? gem 'rubocop', require: false + gem 'parallel_tests' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 08fcda1a031ff..322daf1f2c633 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -276,6 +276,8 @@ GEM ruby-openid optimist (3.0.0) parallel (1.13.0) + parallel_tests (2.28.0) + parallel parser (2.6.0.0) ast (~> 2.4.0) pg (1.1.4) @@ -524,6 +526,7 @@ DEPENDENCIES omniauth-twitter onebox (= 1.8.82) openid-redis-store + parallel_tests pg pry-nav pry-rails diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index 67320ee5e8c2e..56abedade9a10 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -236,6 +236,10 @@ def keys class BlankProvider < BaseProvider def lookup(key, default) + + if key == :redis_port + return ENV["DISCOURSE_REDIS_PORT"] if ENV["DISCOURSE_REDIS_PORT"] + end default end diff --git a/config/boot.rb b/config/boot.rb index 97806eac15ace..fdf031f52feb9 100644 --- a/config/boot.rb +++ b/config/boot.rb @@ -28,3 +28,19 @@ ) end end + +# Parallel spec system +if ENV['RAILS_ENV'] == "test" && ENV['TEST_ENV_NUMBER'] + n = ENV['TEST_ENV_NUMBER'].to_i + port = 10000 + n + + puts "Setting up parallel test mode - starting Redis #{n} on port #{port}" + + `rm -rf tmp/test_data_#{n} && mkdir -p tmp/test_data_#{n}/redis` + pid = Process.spawn("redis-server --dir tmp/test_data_#{n}/redis --port #{port}", out: "/dev/null") + + ENV["DISCOURSE_REDIS_PORT"] = port.to_s + ENV["RAILS_DB"] = "discourse_test_#{ENV['TEST_ENV_NUMBER']}" + + at_exit { puts "Terminating redis #{n}"; Process.kill("SIGTERM", pid); Process.wait } +end