Class: TestCenter::Helper::MultiScanManager::ParallelTestBatchWorker

Inherits:
TestBatchWorker
  • Object
show all
Defined in:
lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb

Instance Attribute Summary collapse

Attributes inherited from TestBatchWorker

#state

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ParallelTestBatchWorker

Returns a new instance of ParallelTestBatchWorker.



11
12
13
14
15
16
17
18
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 11

def initialize(options)
  super(options)
  @pipe_endpoint = nil

  @@colors ||= String.colors - %i[white black light_green default]
  @color = @@colors.sample
  @@colors = @@colors - [@color]
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 9

def pid
  @pid
end

Instance Method Details

#close_parent_process_writerObject



95
96
97
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 95

def close_parent_process_writer
  @writer.close
end

#handle_child_process_results(tests_passed) ⇒ Object



88
89
90
91
92
93
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 88

def handle_child_process_results(tests_passed)
  # as suggested by the method name, this is done in the Child process
  @logfile.close
  @writer.puts tests_passed.to_s
  @writer.close
end

#open_interprocess_communicationObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 66

def open_interprocess_communication
  # This is performed in the Parent process in preparation to setup
  # the STDOUT and STDOUT for printing messages from the Child process
  # to a file. This is done so that when multiple processes write
  # messages, they will not be written to the console in a broken
  # interlaced manner.
  @reader, @writer = IO.pipe
  @log_filepath = File.join(
    Dir.mktmpdir,
    "parallel-test-batch-#{@options[:batch_index] + 1}.txt"
  )
end


62
63
64
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 62

def print_final_results(tests_passed)
  FastlaneCore::UI.verbose("All tests passed for batch #{@options[:batch_index] + 1}? #{tests_passed}")
end

#process_resultsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 24

def process_results
  # This is performed in the Parent process
  @pid = nil

  worker_prefix = "[worker #{@options[:batch_index] + 1}] "
  File.foreach(@log_filepath) do |line|
    unless FastlaneCore::Helper.colors_disabled?
      worker_prefix = worker_prefix.colorize(@color)
    end
    print worker_prefix
    print line
  end
  state = :ready_to_work

  @options[:test_batch_results] << (@reader.gets.chomp.to_s == 'true')
end

#reroute_stdout_to_logfileObject



79
80
81
82
83
84
85
86
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 79

def reroute_stdout_to_logfile
  @reader.close # we are now in the subprocess. Write all stdout to the
  # log file to prevent interlaced messages
  @logfile = File.open(@log_filepath, 'w')
  @logfile.sync = true
  $stdout.reopen(@logfile)
  $stderr.reopen(@logfile)
end

#run(run_options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 41

def run(run_options)
  self.state = :working

  open_interprocess_communication
  @pid = Process.fork do
    tests_passed = false
    begin
      reroute_stdout_to_logfile
      tests_passed = super(run_options)
    rescue StandardError => e
      puts e.message
      puts e.backtrace.inspect
    ensure
      print_final_results(tests_passed)
      handle_child_process_results(tests_passed)
      exit!
    end
  end
  close_parent_process_writer
end

#state=(new_state) ⇒ Object



20
21
22
# File 'lib/fastlane/plugin/test_center/helper/multi_scan_manager/parallel_test_batch_worker.rb', line 20

def state=(new_state)
  super(new_state)
end