Class: RSpecTurbo::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_turbo/worker.rb

Overview

A single RSpec process running one batch of work. Worker.spawn forks the process and returns a Worker the executor tracks until the process exits.

Every worker is wired with two helper files shipped by the gem:

* progress_reporter.rb — a formatter that streams the example count to a
  progress file so the parent can draw a global progress bar.
* slow_profile.rb      — an opt-in profiler (RSPEC_PROFILE_SLOW=1) that
  emits the "TOP N FILES BY TIME" block the report aggregates. It is a
  no-op when profiling is disabled, so requiring it is always safe.

Constant Summary collapse

PROGRESS_REPORTER_PATH =
File.expand_path("progress_reporter.rb", __dir__)
SLOW_PROFILE_PATH =
File.expand_path("slow_profile.rb", __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid:, label:, units:, slot:, started:, progress_file:) ⇒ Worker

Returns a new instance of Worker.



81
82
83
84
85
86
87
88
# File 'lib/rspec_turbo/worker.rb', line 81

def initialize(pid:, label:, units:, slot:, started:, progress_file:)
  @pid = pid
  @label = label
  @units = units
  @slot = slot
  @started = started
  @progress_file = progress_file
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



19
20
21
# File 'lib/rspec_turbo/worker.rb', line 19

def label
  @label
end

#pidObject (readonly)

Returns the value of attribute pid.



19
20
21
# File 'lib/rspec_turbo/worker.rb', line 19

def pid
  @pid
end

#progress_fileObject (readonly)

Returns the value of attribute progress_file.



19
20
21
# File 'lib/rspec_turbo/worker.rb', line 19

def progress_file
  @progress_file
end

#slotObject (readonly)

Returns the value of attribute slot.



19
20
21
# File 'lib/rspec_turbo/worker.rb', line 19

def slot
  @slot
end

#startedObject (readonly)

Returns the value of attribute started.



19
20
21
# File 'lib/rspec_turbo/worker.rb', line 19

def started
  @started
end

#unitsObject (readonly)

Returns the value of attribute units.



19
20
21
# File 'lib/rspec_turbo/worker.rb', line 19

def units
  @units
end

Class Method Details

.spawn(label:, units:, slot:, rspec_options:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rspec_turbo/worker.rb', line 21

def self.spawn(label:, units:, slot:, rspec_options:)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  progress_file = Config.progress_path(slot)
  File.write(progress_file, "0")

  pid = Process.spawn(
    env(slot, progress_file),
    "bundle", "exec", "rspec", "--color", "--order", "random",
    "--require", PROGRESS_REPORTER_PATH,
    "--require", SLOW_PROFILE_PATH,
    "--format", "progress",
    "--format", "RSpecTurbo::ProgressReporter",
    *junit_args(slot),
    *rspec_options, *rspec_args(units),
    out: Config.log_path(label), err: [:child, :out]
  )

  new(pid: pid, label: label, units: units, slot: slot, started: started, progress_file: progress_file)
end

Instance Method Details

#durationObject



90
# File 'lib/rspec_turbo/worker.rb', line 90

def duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started).round