Using Spring and parallel_tests in your Rails application

Posted Over 7 years ago. Visible to the public.

You want Spring Show archive.org snapshot for super-fast binstubs like bin/rails or bin/rspec which avoid Rails boot time.
You want parallel_tests Show archive.org snapshot to speed up full test runs of large test suites.

Unfortunately, you do not want parallel_tests to use your Spring binstubs as those parallelized tests will share data and/or loose some information. There are some issues Show archive.org snapshot about this on GitHub and there is a suggested workaround Show archive.org snapshot which never worked for me properly.

The only "real" solution that always worked for us was patching binstubs so that they do not use Spring when running in parallel.
Since boot-up time is not an issue for full test runs that take several minutes, we can accept Rails booting a few seconds.

In your binstubs, there should be something like this:

begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end

Change it as follows.

running_in_parallel = ENV.has_key?('TEST_ENV_NUMBER') || ARGV.any? { |arg| arg =~ /^parallel:/ }

begin
  load File.expand_path("../spring", __FILE__) unless running_in_parallel
rescue LoadError
end

That's it. bin/rspec your_spec.rb will now boot up instantly while parallel_tests still works.
Note that the ARGV check is required for rake tasks like parallel:prepare which would otherwise overwrite your development database.


Do not try to fix this, it is not as simple as it seems.
If you still want to tackle that problem, you should at least confirm these aspects work flawlessly: DB connection, Redis, memoization, spec stubs, Rake tasks like rake parallel:prepare

Arne Hartherz
Last edit
Almost 7 years ago
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2016-09-09 12:45)