Last active
August 29, 2015 14:22
-
-
Save mrnugget/dea098129ee016e26e21 to your computer and use it in GitHub Desktop.
Run RSpec tests on multiple processes at the same time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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