Skip to content

Commit

Permalink
Fix result in case of requeue (#47)
Browse files Browse the repository at this point in the history
Fix race condition when queue is empty and none of the workers are busy
  • Loading branch information
rajdeepv authored Feb 21, 2020
1 parent 8f09d57 commit 9764124
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
40 changes: 20 additions & 20 deletions lib/parallel_cucumber/helper/cucumber/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ def batch_mapped_files(options, batch, env)

def parse_json_report(json_report)
report = JSON.parse(json_report, symbolize_names: true)
report.map do |scenario, cucumber_status|
status = case cucumber_status
when 'failed'
Status::FAILED
when 'passed'
Status::PASSED
when 'pending'
Status::PENDING
when 'skipped'
Status::SKIPPED
when 'undefined'
Status::UNDEFINED
when 'unknown'
Status::UNKNOWN
else
Status::UNKNOWN
end
[scenario, status]
end.to_h
report.each do |scenario, details|
report[scenario][:status] = case details[:status]
when 'failed'
Status::FAILED
when 'passed'
Status::PASSED
when 'pending'
Status::PENDING
when 'skipped'
Status::SKIPPED
when 'undefined'
Status::UNDEFINED
when 'unknown'
Status::UNKNOWN
else
Status::UNKNOWN
end
end
report
end

private
Expand All @@ -57,7 +57,7 @@ def dry_run_report(options, args_string)
options = remove_strict_flag(options)
content = nil

Tempfile.open(%w(dry-run .json)) do |f|
Tempfile.open(%w[dry-run .json]) do |f|
dry_run_options = "--dry-run --format ParallelCucumber::Helper::Cucumber::JsonStatusFormatter --out #{f.path}"

cmd = "cucumber #{options} #{dry_run_options} #{args_string}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ def initialize(config)
end

def on_after_test_case(event)
@result[event.test_case.location.to_s] = event.result.to_sym
details = {status: event.result.to_sym}
if event.result.respond_to?(:exception)
details[:exception_classname] = event.result.exception.class.to_s
details[:exception_message] = event.result.exception.message
end
details[:finish_time] = Time.now.to_i
@result[event.test_case.location.to_s] = details
end

def on_finished_testing(*)
Expand Down
2 changes: 1 addition & 1 deletion lib/parallel_cucumber/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run

status_totals = Status.constants.map do |status|
status = Status.const_get(status)
tests_with_status = results.select { |_t, s| s == status }.keys
tests_with_status = results.select { |_t, s| s[:status] == status }.keys
[status, tests_with_status]
end.to_h

Expand Down
4 changes: 2 additions & 2 deletions lib/parallel_cucumber/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def precheck(env)
def running_totals(batch_results, running_total)
batch_info = Status.constants.map do |status|
status = Status.const_get(status)
[status, batch_results.select { |_t, s| s == status }.keys]
[status, batch_results.select { |_t, s| s[:status] == status }.keys]
end.to_h
batch_info.each do |s, tt|
@logger.info("#{s.to_s.upcase} #{tt.count} tests: #{tt.join(' ')}") unless tt.empty?
Expand All @@ -191,7 +191,7 @@ def process_results(batch_results, tests)
test_syms = tests.map(&:to_sym)
unrun = test_syms - batch_keys
surfeit = batch_keys - test_syms
unrun.each { |test| batch_results[test] = Status::UNKNOWN }
unrun.each { |test| batch_results[test][:status] = Status::UNKNOWN }
surfeit.each { |test| batch_results.delete(test) }
@logger.error("Did not run #{unrun.count}/#{tests.count}: #{unrun.join(' ')}") unless unrun.empty?
@logger.error("Extraneous runs (#{surfeit.count}): #{surfeit.join(' ')}") unless surfeit.empty?
Expand Down
10 changes: 7 additions & 3 deletions lib/parallel_cucumber/worker_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def inform_idle(worker)
def create_workers(number_of_workers)
number_of_workers.times do |index|
@workers["W#{index}"] =
ParallelCucumber::Worker.new(options: @options, index: index, stdout_logger: @logger, manager: self)
ParallelCucumber::Worker.new(options: @options, index: index, stdout_logger: @logger, manager: self)
end
end

Expand Down Expand Up @@ -67,11 +67,15 @@ def start_workers
puts "Starting W#{index}"
@workers["W#{index}"].start(env_for_worker(@options[:env_variables], index))
end
@results.inject(:merge) # Returns hash of file:line to statuses + :worker-index to summary.
@results.inject do |seed, result|
seed.merge(result) do |_key, oldval, newval|
(newval[:finish_time] > oldval[:finish_time]) ? newval : oldval
end
end
end

def kill_all_workers
@logger.info("=== Killing All Workers")
@logger.info('=== Killing All Workers')
@workers.values.each { |w| w.assign_job(Job.new(Job::DIE)) }
end

Expand Down

0 comments on commit 9764124

Please sign in to comment.