Method: RunLoop::Core.read_response

Defined in:
lib/run_loop/core.rb

.read_response(run_loop, expected_index, empty_file_timeout = 10, search_for_property = 'index') ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/run_loop/core.rb', line 621

def self.read_response(run_loop, expected_index, empty_file_timeout=10, search_for_property='index')
  debug_read = RunLoop::Environment.debug_read?

  log_file = run_loop[:log_file]
  initial_offset = run_loop[:initial_offset] || 0
  offset = initial_offset

  result = nil
  loop do
    unless File.exist?(log_file) && File.size?(log_file)
      sleep(0.2)
      next
    end

    size = File.size(log_file)
    output = File.read(log_file, size-offset, offset)

    if /AXError: Could not auto-register for pid status change/.match(output)
      if /kAXErrorServerNotFound/.match(output)
        self.log_instruments_error('Accessibility is not enabled on device/simulator, please enable it.')
      end
      raise RunLoop::TimeoutError.new('AXError: Could not auto-register for pid status change')
    end

    if /Automation Instrument ran into an exception/.match(output)
      raise RunLoop::TimeoutError.new('Exception while running script')
    end

    if /FBSOpenApplicationErrorDomain error/.match(output)
      msg = "Instruments failed to launch app: 'FBSOpenApplicationErrorDomain error 8"
      if RunLoop::Environment.debug?
        self.log_instruments_error(msg)
      end
      raise RunLoop::TimeoutError.new(msg)
    end

    if /Error: Script threw an uncaught JavaScript error: unknown JavaScript exception/.match(output)
      msg = "Instruments failed to launch: because of an unknown JavaScript exception"
      if RunLoop::Environment.debug?
        self.log_instruments_error(msg)
      end
      raise RunLoop::TimeoutError.new(msg)
    end

    index_if_found = output.index(START_DELIMITER)
    if debug_read
      puts output.gsub('*', '')
      puts "Size #{size}"
      puts "offset #{offset}"
      puts "index_of #{START_DELIMITER}: #{index_if_found}"
    end

    if index_if_found

      offset = offset + index_if_found
      rest = output[index_if_found+START_DELIMITER.size..output.length]
      index_of_json = rest.index("}#{END_DELIMITER}")

      if index_of_json.nil?
        #Wait for rest of json
        sleep(0.1)
        next
      end

      json = rest[0..index_of_json]


      if debug_read
        puts "Index #{index_if_found}, Size: #{size} Offset #{offset}"

        puts ("parse #{json}")
      end

      offset = offset + json.size
      parsed_result = JSON.parse(json)
      if debug_read
        p parsed_result
      end
      json_index_if_present = parsed_result[search_for_property]
      if json_index_if_present && json_index_if_present == expected_index
        result = parsed_result
        break
      end
    else
      sleep(0.1)
    end
  end

  run_loop[:initial_offset] = offset
  RunLoop::HostCache.default.write(run_loop) unless RunLoop::Environment.xtc?
  result
end