Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run RSpec tests on multiple processes at the same time
#!/usr/bin/env ruby
ENV['RAILS_ENV'] = 'test'
require 'benchmark'
rails_loading_time = Benchmark.measure { require './config/environment' }
puts "Rails env loaded in #{rails_loading_time}"
NUM_FORKS = 2
test_groups = `find ./spec -type f -iname "*foobar*"`.split("\n").in_groups(NUM_FORKS).to_a
pipes = {}
NUM_FORKS.times do |i|
read_end, write_end = IO.pipe
child_pid = fork do
read_end.close
$stdout.reopen(write_end)
puts "[#{Process.pid}] Executing #{test_groups[i].join(' ')} ..."
require 'rspec/autorun'
test_groups[i].each {|f| require f }
end
write_end.close
pipes[child_pid] = read_end
end
loop do
break if pipes.length == 0
readable, _, _ = IO.select(pipes.values)
begin
print readable.first.readchar
rescue EOFError
child_pid = pipes.find {|k, v| v == readable.first }.first
pipes.delete(child_pid)
end
end
status = Process.waitall
status.each do |s|
puts "Process #{s.first} exited with #{s.last.exitstatus}"
end
non_zero = status.map(&:last).map(&:exitstatus).reject {|i| i == 0 }
if non_zero.empty?
exit 0
else
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.