Class: ParallelRSpec::Workers

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_rspec/workers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number_of_workers = Workers.number_of_workers) ⇒ Workers

Returns a new instance of Workers.



11
12
13
# File 'lib/parallel_rspec/workers.rb', line 11

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

Instance Attribute Details

#number_of_workersObject (readonly)

Returns the value of attribute number_of_workers.



9
10
11
# File 'lib/parallel_rspec/workers.rb', line 9

def number_of_workers
  @number_of_workers
end

Class Method Details

.number_of_workersObject



3
4
5
6
7
# File 'lib/parallel_rspec/workers.rb', line 3

def self.number_of_workers
  workers = ENV['WORKERS'].to_i
  workers = 4 if workers.zero?
  workers
end

Instance Method Details

#establish_test_database_connection(worker) ⇒ Object



26
27
28
29
30
# File 'lib/parallel_rspec/workers.rb', line 26

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

#run_test_workersObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/parallel_rspec/workers.rb', line 15

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

  verify_children(children)
end

#verify_children(children) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/parallel_rspec/workers.rb', line 32

def verify_children(children)
  results = children.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