Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a1422840b0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 112 lines (88 sloc) 3.095 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
namespace :spec do
  namespace :parallel do
    desc "prepare parallel test running by calling db:reset for every test database needed with spec:parallel:"
    task :prepare, :count do |t,args|
      num_processes = (args[:count] || 2).to_i
      num_processes.times do |i|
        puts "Preparing database #{i + 1}"
        `export TEST_ENV_NUMBER=#{i == 0 ? '' : i + 1} ; export RAILS_ENV=test ; rake db:reset`
      end
    end
  end

  desc "run specs in parallel with spec:parallel[count]"
  task :parallel, :count do |t,args|
    require File.join(File.dirname(__FILE__), '..', 'lib', 'parallel_specs')

    start = Time.now

    num_processes = (args[:count] || 2).to_i
    groups = ParallelSpecs.specs_in_groups(RAILS_ROOT, num_processes)

    num_specs = groups.sum { |g| g.size }
    puts "#{num_processes} processes for #{num_specs} specs, ~ #{num_specs / num_processes} specs per process"

    #run each of the groups
    pids = []
    read, write = IO.pipe
    groups.each_with_index do |files, process_number|
      pids << Process.fork do
        write.puts ParallelSpecs.run_tests(files, process_number)
      end
    end

    #handle user interrup (Ctrl+c)
    Signal.trap 'SIGINT' do
      STDERR.puts "Parallel specs interrupted, exiting ..."
      pids.each { |pid| Process.kill("KILL", pid) }
      exit 1
    end

    #wait for processes to finish
    pids.each { Process.wait }


    #parse and print results
    write.close
    results = ParallelSpecs.find_results(read.read)
    read.close
    puts ""
    puts "Results:"
    results.each{|r| puts r}

    #report total time taken
    puts ""
    puts "Took #{Time.now - start} seconds"

    #exit with correct status code
    exit ParallelSpecs.failed?(results) ? 1 : 0
  end
end

namespace :test do

  desc "run tests in parallel with test:parallel[count]"
  task :parallel, :count do |t,args|
    require File.join(File.dirname(__FILE__), '..', 'lib', 'parallel_tests')

    start = Time.now

    num_processes = (args[:count] || 2).to_i
    groups = ParallelTests.tests_in_groups(RAILS_ROOT, num_processes)

    num_specs = groups.sum { |g| g.size }
    puts "#{num_processes} processes for #{num_specs} tests, ~ #{num_specs / num_processes} tests per process"

    #run each of the groups
    pids = []
    read, write = IO.pipe
    groups.each_with_index do |files, process_number|
      pids << Process.fork do
        write.puts ParallelTests.run_tests(files, process_number)
      end
    end

    #handle user interrup (Ctrl+c)
    Signal.trap 'SIGINT' do
      STDERR.puts "Parallel tests interrupted, exiting ..."
      pids.each { |pid| Process.kill("KILL", pid) }
      exit 1
    end

    #wait for processes to finish
    pids.each { Process.wait }


    #parse and print results
    write.close
    results = ParallelTests.find_results(read.read)
    read.close
    puts ""
    puts "Results:"
    results.each{|r| puts r}

    #report total time taken
    puts ""
    puts "Took #{Time.now - start} seconds"

    #exit with correct status code
    exit ParallelTests.failed?(results) ? 1 : 0
  end
end
Something went wrong with that request. Please try again.