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



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/run_loop/core.rb', line 424

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::Cache.default.write(run_loop) unless RunLoop::Environment.xtc?
  result
end