Class: ParallelRSpec::Workers
- Inherits:
-
Object
- Object
- ParallelRSpec::Workers
- Defined in:
- lib/parallel_rspec/workers.rb
Instance Attribute Summary collapse
-
#number_of_workers ⇒ Object
readonly
Returns the value of attribute number_of_workers.
Class Method Summary collapse
Instance Method Summary collapse
- #establish_test_database_connection(worker) ⇒ Object
-
#initialize(number_of_workers = Workers.number_of_workers) ⇒ Workers
constructor
A new instance of Workers.
- #invoke_server_for_channels(server, channels) ⇒ Object
- #run_test_workers ⇒ Object
- #run_test_workers_with_server(server) ⇒ Object
- #verify_children(child_pids) ⇒ Object
Constructor Details
#initialize(number_of_workers = Workers.number_of_workers) ⇒ 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_workers ⇒ Object (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_workers ⇒ Object
3 4 5 6 7 |
# File 'lib/parallel_rspec/workers.rb', line 3 def self.number_of_workers workers = ENV['WORKERS'].to_i workers = 2 if workers.zero? workers end |
Instance Method Details
#establish_test_database_connection(worker) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/parallel_rspec/workers.rb', line 62 def establish_test_database_connection(worker) ENV['TEST_ENV_NUMBER'] = worker.to_s if defined?(ActiveRecord) if ActiveRecord::Base.configurations.respond_to?(:configs_for) ActiveRecord::Base.configurations.configs_for(env_name: 'test').each do |configuration| configuration.database << worker.to_s unless worker == 1 ActiveRecord::Base.establish_connection(configuration) end else configuration = ActiveRecord::Base.configurations['test'] configuration.with_indifferent_access['database'] << worker.to_s unless worker == 1 ActiveRecord::Base.establish_connection(configuration) end end end |
#invoke_server_for_channels(server, channels) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/parallel_rspec/workers.rb', line 50 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 |
#run_test_workers ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/parallel_rspec/workers.rb', line 15 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 |
#run_test_workers_with_server(server) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/parallel_rspec/workers.rb', line 26 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) Config.after_fork.each do |proc| proc.call(worker) end 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 |
#verify_children(child_pids) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/parallel_rspec/workers.rb', line 78 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 |