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(sock) ⇒ Iterator

Returns a new instance of Iterator.



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

def initialize(sock)
  @sock = sock
  @done = false
  @stats = {}
  @procline = $0
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



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

def stats
  @stats
end

Instance Method Details

#eachObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/test_queue/iterator.rb', line 12

def each
  fail 'already used this iterator' if @done

  while true
    client = UNIXSocket.new(@sock)
    r, w, e = IO.select([client], nil, [client], nil)
    break if !e.empty?

    if data = client.read(16384)
      client.close
      item = Marshal.load(data)
      $0 = "#{@procline} - #{item.respond_to?(:description) ? item.description : item}"

      start = Time.now
      yield item
      @stats[item] = Time.now - start
    else
      break
    end
  end
rescue Errno::ENOENT, Errno::ECONNRESET, Errno::ECONNREFUSED
ensure
  @done = true
  File.open("/tmp/test_queue_worker_#{$$}_stats", "wb") do |f|
    f.write Marshal.dump(@stats)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/test_queue/iterator.rb', line 42

def empty?
  false
end