Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Forked from mrnugget/forking_rspec.rb
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cupakromer/3007f29a4a738c11db50 to your computer and use it in GitHub Desktop.
Save cupakromer/3007f29a4a738c11db50 to your computer and use it in GitHub Desktop.
#!/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