Class: ParallelCucumber::Main
- Inherits:
-
Object
- Object
- ParallelCucumber::Main
- Includes:
- Helper::Utils
- Defined in:
- lib/parallel_cucumber/main.rb
Instance Method Summary collapse
- #determine_work_and_batch_size(count) ⇒ Object
-
#initialize(options) ⇒ Main
constructor
A new instance of Main.
- #load_external_files ⇒ Object
- #report_by_group(results) ⇒ Object
- #run ⇒ Object
- #run_parallel_workers(number_of_workers) ⇒ Object
Methods included from Helper::Utils
Constructor Details
#initialize(options) ⇒ Main
7 8 9 10 11 12 13 14 |
# File 'lib/parallel_cucumber/main.rb', line 7 def initialize() = @logger = ParallelCucumber::CustomLogger.new(STDOUT) load_external_files @logger.progname = 'Primary' # Longer than 'Main', to make the log file pretty. @logger.level = [:debug] ? ParallelCucumber::CustomLogger::DEBUG : ParallelCucumber::CustomLogger::INFO end |
Instance Method Details
#determine_work_and_batch_size(count) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/parallel_cucumber/main.rb', line 146 def determine_work_and_batch_size(count) if [:n] == 0 [:n] = [1, [:env_variables].map { |_k, v| v.is_a?(Array) ? v.count : 0 }].flatten.max @logger.info("Inferred worker count #{@options[:n]} from env_variables option") end number_of_workers = [[:n], count].min unless number_of_workers == [:n] @logger.info(" Number of workers was overridden to \#{number_of_workers}.\n More workers (\#{@options[:n]}) requested than tests (\#{count})\".\n LOG\n end\n\n @logger.info(<<-LOG)\n Number of workers is \#{number_of_workers}.\n LOG\n\n if (@options[:batch_size] - 1) * number_of_workers >= count\n original_batch_size = @options[:batch_size]\n @options[:batch_size] = [(count.to_f / number_of_workers).floor, 1].max\n @logger.info(<<-LOG)\n Batch size was overridden to \#{@options[:batch_size]}.\n Presumably it will be more optimal for \#{count} tests and \#{number_of_workers} workers\n than \#{original_batch_size}\n LOG\n end\n number_of_workers\nend\n") |
#load_external_files ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/parallel_cucumber/main.rb', line 16 def load_external_files return if [:load_files].nil? [:load_files].each do |file| @logger.debug("Loading File: #{file}") load file end end |
#report_by_group(results) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/parallel_cucumber/main.rb', line 105 def report_by_group(results) group = Hash.new { |h, k| h[k] = Hash.new(0) } # Default new keys to 0 Helper::Command.wrap_block([:log_decoration], 'Worker summary', @logger) do results.find_all { |w| w.first =~ /^:worker-/ }.each do |w| # w = [:worker-0, [[:batches, 7], [:group, "localhost2"], [:skipped, 7]]] gp = w.last[:group] next unless gp w.last.each { |(k, v)| group[gp][k] += w.last[k] if v && k != :group } group[gp][:group] = {} unless group[gp].key?(:group) group[gp][:group][w.first] = 1 end end @logger.info "== Groups key count #{group.keys.count}" return unless group.keys.count > 1 Helper::Command.wrap_block([:log_decoration], 'Group summary', @logger) do group.each { |(k, v)| @logger.info("#{k} #{v.sort}") } end end |
#run ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/parallel_cucumber/main.rb', line 24 def run @logger.debug("Connecting to Queue: #{@options[:queue_connection_params]}") queue = Helper::Queue.new([:queue_connection_params]) unless queue.empty? @logger.error("Queue '#{queue.name}' is not empty") exit(1) end all_tests = Helper::Cucumber.selected_tests([:cucumber_options], [:cucumber_args]) if all_tests.empty? @logger.error('There are no tests to run') exit(1) end count = all_tests.count long_running_tests = if [:long_running_tests] Helper::Cucumber.selected_tests([:cucumber_options], [:long_running_tests]) else [] end first_tests = long_running_tests & all_tests if !long_running_tests.empty? && first_tests.empty? @logger.info("No long running tests found in common with main options: #{long_running_tests}") end tests = first_tests + (all_tests - first_tests).shuffle [:directed_tests].each do |k, v| directed_tests = Helper::Cucumber.selected_tests([:cucumber_options], v) if directed_tests.empty? @logger.warn("Queue for #{k} is empty - nothing selected by #{v}") else directed_tests = (directed_tests & long_running_tests) + (directed_tests - long_running_tests).shuffle @logger.debug("Connecting to Queue: _#{k}") directed_queue = Helper::Queue.new([:queue_connection_params], "_#{k}") @logger.info("Adding #{directed_tests.count} tests to queue _#{k}") directed_queue.enqueue(directed_tests) tests -= directed_tests end end @logger.info("Adding #{tests.count} tests to Queue") queue.enqueue(tests) unless tests.empty? number_of_workers = determine_work_and_batch_size(count) status_totals = {} total_mm, total_ss = time_it do results = run_parallel_workers(number_of_workers) || {} unrun = tests - results.keys @logger.error("Tests #{unrun.join(' ')} were not run") unless unrun.empty? @logger.error("Queue #{queue.name} is not empty") unless queue.empty? status_totals = Status.constants.map do |status| status = Status.const_get(status) tests_with_status = results.select { |_t, s| s == status }.keys [status, tests_with_status] end.to_h Helper::Command.wrap_block([:log_decoration], 'Worker summary', @logger) do results.find_all { |w| w.first =~ /^:worker-/ }.each { |w| @logger.info("#{w.first} #{w.last.sort}") } end report_by_group(results) end @logger.debug("SUMMARY=#{@options[:summary]}") if [:summary] status_totals.each do |s, tt| next if tt.empty? @logger.info("Total: #{s.to_s.upcase} tests (#{tt.count}): #{tt.join(' ')}") filename = [:summary] && [:summary][s.to_s.downcase] open(filename, 'w') { |f| f << tt.join("\n") } if filename end @logger.info("\nTook #{total_mm} minutes #{total_ss} seconds") exit((tests - status_totals[Status::PASSED] - status_totals[Status::SKIPPED]).empty? ? 0 : 1) end |
#run_parallel_workers(number_of_workers) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/parallel_cucumber/main.rb', line 128 def run_parallel_workers(number_of_workers) Helper::Command.wrap_block([:log_decoration], [:log_decoration]['worker_block'] || 'workers', @logger) do remaining = (0...number_of_workers).to_a map = Parallel.map( remaining.dup, in_threads: number_of_workers, finish: -> (_, ix, _) { @logger.synch { |l| l.info("Finished: #{ix} remaining: #{remaining -= [ix]}") } } ) do |index| ParallelCucumber::Worker .new(, index, @logger) .start(env_for_worker([:env_variables], index)) end map.inject(:merge) # Returns hash of file:line to statuses + :worker-index to summary. end end |