Class: RSpec::MultiprocessRunner::FileCoordinator

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/multiprocess_runner/file_coordinator.rb

Constant Summary collapse

COMMAND_FILE =
"file"
COMMAND_RESULTS =
"results"
COMMAND_PROCESS =
"process"
COMMAND_FINISHED =
"finished"
COMMAND_START =
"start"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, options = {}) ⇒ FileCoordinator

Returns a new instance of FileCoordinator.



17
18
19
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
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 17

def initialize(files, options={})
  @spec_files = []
  @results = Set.new
  @threads = []
  @failed_workers = []
  self.options = options
  @spec_files_reference = files.to_set
  if options.head_node
    @spec_files = options.use_given_order ? files : sort_files(files)
    Thread.start { run_tcp_server }
    @node_socket, head_node_socket = Socket.pair(:UNIX, :STREAM)
    Thread.start { server_connection_established(head_node_socket) }
  else
    count = 100
    while @node_socket.nil? do
      begin
        @node_socket = TCPSocket.new options.hostname, options.port
        raise unless start?
      rescue BadStartStringError
        @node_socket.close if @node_socket
        raise
      rescue
        @node_socket.close if @node_socket
        @node_socket = nil
        raise if count < 0
        count -= 1
        sleep(6)
      end
    end
    puts
  end
  ObjectSpace.define_finalizer( self, proc { @node_socket.close } )
end

Instance Attribute Details

#failed_workersObject (readonly)

Returns the value of attribute failed_workers.



8
9
10
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 8

def failed_workers
  @failed_workers
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 9

def options
  @options
end

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 8

def results
  @results
end

Instance Method Details

#finishedObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 86

def finished
  if options.head_node
    if @tcp_server_running
     @tcp_server_running = false
     @threads.each(&:join)
     @spec_files += missing_files.to_a
    end
  else
    @node_socket.puts [COMMAND_FINISHED].to_json
  end
end

#get_fileObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 63

def get_file
  begin
    @node_socket.puts [COMMAND_FILE].to_json
    file = @node_socket.gets.chomp
    if @spec_files_reference.include? file
      return file
    else
      return nil # Malformed response, assume done, cease function
    end
  rescue Exception => e
    puts("Got exception #{e} in get_file")
    return nil # If Error, assume done, cease function
  end
end

#missing_filesObject



55
56
57
58
59
60
61
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 55

def missing_files
  if options.head_node
    @spec_files_reference - @results.map(&:filename) - @failed_workers.map(&:current_file) - @spec_files
  else
    []
  end
end

#remaining_filesObject



51
52
53
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 51

def remaining_files
  @spec_files
end

#send_results(results) ⇒ Object



78
79
80
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 78

def send_results(results)
  @node_socket.puts [COMMAND_RESULTS, results].to_json
end

#send_worker_status(worker) ⇒ Object



82
83
84
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 82

def send_worker_status(worker)
  @node_socket.puts [COMMAND_PROCESS, worker, Socket.gethostname].to_json
end