Class: TestQueue::Iterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/test_queue/iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_framework, sock, filter = nil, run_token:, early_failure_limit: nil) ⇒ Iterator

Returns a new instance of Iterator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/test_queue/iterator.rb', line 9

def initialize(test_framework, sock, filter = nil, run_token:, early_failure_limit: nil)
  @test_framework = test_framework
  @done = false
  @suite_stats = []
  @sock = sock
  @filter = filter
  if @sock =~ /\A(.+):(\d+)\z/
    @tcp_address = $1
    @tcp_port = $2.to_i
  end
  @failures = 0
  @early_failure_limit = early_failure_limit
  @run_token = run_token
end

Instance Attribute Details

#sockObject (readonly)

Returns the value of attribute sock.



7
8
9
# File 'lib/test_queue/iterator.rb', line 7

def sock
  @sock
end

Instance Method Details

#connect_to_master(cmd) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/test_queue/iterator.rb', line 84

def connect_to_master(cmd)
  sock =
    if @tcp_address
      TCPSocket.new(@tcp_address, @tcp_port)
    else
      UNIXSocket.new(@sock)
    end
  sock.puts("TOKEN=#{@run_token}")
  sock.puts(cmd)
  sock
rescue Errno::EPIPE
  nil
end

#eachObject



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
# File 'lib/test_queue/iterator.rb', line 24

def each
  raise "already used this iterator. previous caller: #{@done}" if @done

  procline = $0

  loop do
    # If we've hit too many failures in one worker, assume the entire
    # test suite is broken, and notify master so the run
    # can be immediately halted.
    if @early_failure_limit && @failures >= @early_failure_limit
      connect_to_master('KABOOM')
      break
    else
      client = connect_to_master("POP #{Socket.gethostname} #{Process.pid}")
    end
    break if client.nil?

    # This false positive will be resolved by https://github.com/rubocop/rubocop/pull/11830.
    _r, _w, e = IO.select([client], nil, [client], nil)
    break unless e.empty?

    if (data = client.read(65536))
      client.close
      item = Marshal.load(data)
      break if item.nil? || item.empty?

      if item == 'WAIT'
        $0 = "#{procline} - Waiting for work"
        sleep 0.1
        next
      end
      suite_name, path = item
      suite = load_suite(suite_name, path)

      # Maybe we were told to load a suite that doesn't exist anymore.
      next unless suite

      $0 = "#{procline} - #{suite.respond_to?(:description) ? suite.description : suite}"
      start = Time.now
      if @filter
        @filter.call(suite) { yield suite }
      else
        yield suite
      end
      @suite_stats << TestQueue::Stats::Suite.new(suite_name, path, Time.now - start, Time.now)
      @failures += suite.failure_count if suite.respond_to? :failure_count
    else
      break
    end
  end
rescue Errno::ENOENT, Errno::ECONNRESET, Errno::ECONNREFUSED
  # noop
ensure
  $0 = procline
  @done = caller(1..1).first
  File.open("/tmp/test_queue_worker_#{$$}_suites", 'wb') do |f|
    Marshal.dump(@suite_stats, f)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/test_queue/iterator.rb', line 98

def empty?
  false
end

#load_suite(suite_name, path) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/test_queue/iterator.rb', line 102

def load_suite(suite_name, path)
  @loaded_suites ||= {}
  suite = @loaded_suites[suite_name]
  return suite if suite

  @test_framework.suites_from_file(path).each do |name, suite_from_file|
    @loaded_suites[name] = suite_from_file
  end
  @loaded_suites[suite_name]
end