module ParallelTests

rake tasks for Rails 3+

Constants

RUBY_BINARY
VERSION
WINDOWS

Public Class Methods

bundler_enabled?() click to toggle source

copied from github.com/carlhuda/bundler Bundler::SharedHelpers#find_gemfile

# File lib/parallel_tests.rb, line 52
def bundler_enabled?
  return true if Object.const_defined?(:Bundler)

  previous = nil
  current = File.expand_path(Dir.pwd)

  until !File.directory?(current) || current == previous
    filename = File.join(current, "Gemfile")
    return true if File.exist?(filename)
    previous = current
    current = File.expand_path("..", current)
  end

  false
end
delta() { || ... } click to toggle source
# File lib/parallel_tests.rb, line 97
def delta
  before = now.to_f
  yield
  now.to_f - before
end
determine_number_of_processes(count) click to toggle source
# File lib/parallel_tests.rb, line 16
def determine_number_of_processes(count)
  [
    count,
    ENV["PARALLEL_TEST_PROCESSORS"],
    Parallel.processor_count
  ].detect { |c| !c.to_s.strip.empty? }.to_i
end
first_process?() click to toggle source
# File lib/parallel_tests.rb, line 68
def first_process?
  ENV["TEST_ENV_NUMBER"].to_i <= 1
end
last_process?() click to toggle source
# File lib/parallel_tests.rb, line 72
def last_process?
  current_process_number = ENV['TEST_ENV_NUMBER']
  total_processes = ENV['PARALLEL_TEST_GROUPS']
  return true if current_process_number.nil? && total_processes.nil?
  current_process_number = '1' if current_process_number.nil?
  current_process_number == total_processes
end
now() click to toggle source
# File lib/parallel_tests.rb, line 93
def now
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
number_of_running_processes() click to toggle source
# File lib/parallel_tests.rb, line 89
def number_of_running_processes
  pids.count
end
pid_file_path() click to toggle source
# File lib/parallel_tests.rb, line 41
def pid_file_path
  ENV.fetch('PARALLEL_PID_FILE')
end
pids() click to toggle source
# File lib/parallel_tests.rb, line 37
def pids
  @pids ||= Pids.new(pid_file_path)
end
stop_all_processes() click to toggle source
# File lib/parallel_tests.rb, line 45
def stop_all_processes
  pids.all.each { |pid| Process.kill(:INT, pid) }
rescue Errno::ESRCH, Errno::EPERM
  # Process already terminated, do nothing
end
wait_for_other_processes_to_finish() click to toggle source
# File lib/parallel_tests.rb, line 84
def wait_for_other_processes_to_finish
  return unless ENV["TEST_ENV_NUMBER"]
  sleep 1 until number_of_running_processes <= 1
end
with_pid_file() { || ... } click to toggle source
# File lib/parallel_tests.rb, line 24
def with_pid_file
  Tempfile.open('parallel_tests-pidfile') do |f|
    ENV['PARALLEL_PID_FILE'] = f.path
    # Pids object should be created before threads will start adding pids to it
    # Otherwise we would have to use Mutex to prevent creation of several instances
    @pids = pids
    yield
  ensure
    ENV['PARALLEL_PID_FILE'] = nil
    @pids = nil
  end
end
with_ruby_binary(command) click to toggle source
# File lib/parallel_tests.rb, line 80
def with_ruby_binary(command)
  WINDOWS ? [RUBY_BINARY, '--', command] : [command]
end