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 = RSpec::MultiprocessRunner::CommandLineOptions.new) ⇒ FileCoordinator

Returns a new instance of FileCoordinator.



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

def initialize(files, options=RSpec::MultiprocessRunner::CommandLineOptions.new)
  @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.



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

def failed_workers
  @failed_workers
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#resultsObject (readonly)

Returns the value of attribute results.



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

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 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



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

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 StandardError => e
    puts("Got exception #{e} in get_file")
    return nil # If Error, assume done, cease function
  end
end

#missing_filesObject



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

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



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

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