Class: RSpec::Conductor::WorkerProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/conductor/worker_process.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number:, on_message: nil) ⇒ WorkerProcess

Returns a new instance of WorkerProcess.



33
34
35
36
37
# File 'lib/rspec/conductor/worker_process.rb', line 33

def initialize(number:, on_message: nil)
  @number = number
  @on_message = on_message
  @status = :running
end

Instance Attribute Details

#child_processObject

Returns the value of attribute child_process.



31
32
33
# File 'lib/rspec/conductor/worker_process.rb', line 31

def child_process
  @child_process
end

#current_specObject

Returns the value of attribute current_spec.



31
32
33
# File 'lib/rspec/conductor/worker_process.rb', line 31

def current_spec
  @current_spec
end

#numberObject (readonly)

Returns the value of attribute number.



30
31
32
# File 'lib/rspec/conductor/worker_process.rb', line 30

def number
  @number
end

#statusObject

Returns the value of attribute status.



31
32
33
# File 'lib/rspec/conductor/worker_process.rb', line 31

def status
  @status
end

Class Method Details

.spawn(number:, test_env_number:, on_message:, on_stdout: nil, on_stderr: nil, **worker_init_args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rspec/conductor/worker_process.rb', line 6

def self.spawn(number:, test_env_number:, on_message:, on_stdout: nil, on_stderr: nil, **worker_init_args)
  worker_process = new(number: number, on_message: on_message)

  child_process = Util::ChildProcess.fork(on_stdout: on_stdout, on_stderr: on_stderr, on_message: proc { worker_process.handle_message }) do |_, child_socket|
    ENV["TEST_ENV_NUMBER"] = test_env_number
    Worker.new(
      worker_number: number,
      socket: Protocol::Socket.new(child_socket),
      **worker_init_args
    ).run
  end

  worker_process.child_process = child_process
  worker_process
end

.tick_all(worker_processes) ⇒ Object



22
23
24
# File 'lib/rspec/conductor/worker_process.rb', line 22

def self.tick_all(worker_processes)
  Util::ChildProcess.tick_all(worker_processes.map(&:child_process))
end

.wait_all(worker_processes) ⇒ Object



26
27
28
# File 'lib/rspec/conductor/worker_process.rb', line 26

def self.wait_all(worker_processes)
  Util::ChildProcess.wait_all(worker_processes.map(&:child_process))
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rspec/conductor/worker_process.rb', line 79

def eql?(other)
  other.is_a?(self.class) && other.number == number
end

#handle_messageObject



39
40
41
42
43
44
45
46
47
# File 'lib/rspec/conductor/worker_process.rb', line 39

def handle_message
  message = receive_message
  unless message
    socket.close
    return
  end

  @on_message&.call(self, message)
end

#hashObject



75
76
77
# File 'lib/rspec/conductor/worker_process.rb', line 75

def hash
  [number].hash
end

#pidObject



61
62
63
# File 'lib/rspec/conductor/worker_process.rb', line 61

def pid
  child_process.pid
end

#receive_messageObject



53
54
55
# File 'lib/rspec/conductor/worker_process.rb', line 53

def receive_message
  socket.receive_message
end

#running?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rspec/conductor/worker_process.rb', line 71

def running?
  status == :running
end

#send_message(message) ⇒ Object



49
50
51
# File 'lib/rspec/conductor/worker_process.rb', line 49

def send_message(message)
  socket.send_message(message)
end

#shut_down(status) ⇒ Object



65
66
67
68
69
# File 'lib/rspec/conductor/worker_process.rb', line 65

def shut_down(status)
  return unless running?

  @status = status
end

#socketObject



57
58
59
# File 'lib/rspec/conductor/worker_process.rb', line 57

def socket
  @socket ||= Protocol::Socket.new(child_process.message_socket)
end