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.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/test_queue/iterator.rb', line 5

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 =~ /^(.+):(\d+)$/
    @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.



3
4
5
# File 'lib/test_queue/iterator.rb', line 3

def sock
  @sock
end

Instance Method Details

#connect_to_master(cmd) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/test_queue/iterator.rb', line 76

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



20
21
22
23
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
# File 'lib/test_queue/iterator.rb', line 20

def each
  fail "already used this iterator. previous caller: #@done" if @done

  procline = $0

  while true
    # 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?
    _r, _w, e = IO.select([client], nil, [client], nil)
    break if !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
ensure
  $0 = procline
  @done = caller.first
  File.open("/tmp/test_queue_worker_#{$$}_suites", "wb") do |f|
    Marshal.dump(@suite_stats, f)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/test_queue/iterator.rb', line 92

def empty?
  false
end

#load_suite(suite_name, path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/test_queue/iterator.rb', line 96

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|
    @loaded_suites[name] = suite
  end
  @loaded_suites[suite_name]
end