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.



15
16
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
50
51
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 15

def initialize(files, options={})
  @spec_files = []
  @results = Set.new
  @threads = []
  @failed_workers = []
  @spec_files_reference = files.to_set
  @hostname = options[:hostname]
  @port = options[:port]
  @max_threads = options[:max_nodes]
  @head_node = options[:head_node]
  @start_string = options[:run_identifier]
  if @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 @hostname, @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.



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

def failed_workers
  @failed_workers
end

#resultsObject (readonly)

Returns the value of attribute results.



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

def results
  @results
end

Instance Method Details

#finishedObject



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

def finished
  if @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



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

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
    return nil # If Error, assume done, cease function
  end
end

#missing_filesObject



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

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

#remaining_filesObject



53
54
55
# File 'lib/rspec/multiprocess_runner/file_coordinator.rb', line 53

def remaining_files
  @spec_files
end

#send_results(results) ⇒ Object



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

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

#send_worker_status(worker) ⇒ Object



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

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