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
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_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 = 4 if workers.zero? workers end |
Instance Method Details
#establish_test_database_connection(worker) ⇒ Object
57 58 59 60 61 |
# File 'lib/parallel_rspec/workers.rb', line 57 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 |
#invoke_server_for_channels(server, channels) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/parallel_rspec/workers.rb', line 45 def invoke_server_for_channels(server, channels) while !channels.empty? Channel.read_select(channels).each do |channel| if command = channel.read server.send(*command) 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 |
# 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) 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
63 64 65 66 67 68 69 70 |
# File 'lib/parallel_rspec/workers.rb', line 63 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 |