Sha256: 5ec7ea7978f165db6892e822755d9213f646b9dca44c87ca2be0116d64161341

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module ParallelRSpec
  class Workers
    def self.number_of_workers
      workers = ENV['WORKERS'].to_i
      workers = 2 if workers.zero?
      workers
    end

    attr_reader :number_of_workers

    def initialize(number_of_workers = Workers.number_of_workers)
      @number_of_workers = number_of_workers
    end

    def run_test_workers
      child_pids = (1..number_of_workers).collect do |worker|
        fork do
          establish_test_database_connection(worker)
          yield worker
        end
      end

      verify_children(child_pids)
    end

    def run_test_workers_with_server(server)
      child_pids, channels = (1..number_of_workers).collect do |worker|
        channel_to_client, channel_to_server = ParallelRSpec::Channel.pipe

        pid = fork do
          channel_to_client.close
          establish_test_database_connection(worker)
          yield worker, channel_to_server
        end

        channel_to_server.close
        [pid, channel_to_client]
      end.transpose

      invoke_server_for_channels(server, channels)

      verify_children(child_pids)
    end

    def invoke_server_for_channels(server, channels)
      while !channels.empty?
        Channel.read_select(channels).each do |channel|
          if command = channel.read
            server.send(*(command + [channel]))
          else
            channels.delete(channel)
          end
        end
      end
    end

    def establish_test_database_connection(worker)
      ENV['TEST_ENV_NUMBER'] = worker.to_s
      ActiveRecord::Base.configurations['test']['database'] << worker.to_s unless worker == 1
      ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
    end

    def verify_children(child_pids)
      results = child_pids.collect { |pid| Process.wait2(pid).last }.reject(&:success?)

      unless results.empty?
        STDERR.puts "\n#{results.size} worker#{'s' unless results.size == 1} failed"
        exit 1
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parallel_rspec-0.4.1 lib/parallel_rspec/workers.rb