Read more

Using Spring and parallel_tests in your Rails application

Arne Hartherz
September 09, 2016Software engineer at makandra GmbH

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.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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

Posted by Arne Hartherz to makandra dev (2016-09-09 14:45)