Class: TurboTests::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_tests/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Runner

Returns a new instance of Runner.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/turbo_tests/runner.rb', line 93

def initialize(**opts)
  @formatters = opts[:formatters]
  @reporter = opts[:reporter]
  @files = opts[:files]
  @tags = opts[:tags]
  @verbose = opts[:verbose]
  @fail_fast = opts[:fail_fast]
  @start_time = opts[:start_time]
  @count = opts[:count]
  @seed = opts[:seed]
  @seed_used = opts[:seed_used]
  @nice = opts[:nice]
  @use_runtime_info = opts[:use_runtime_info]

  @load_time = 0
  @load_count = 0
  @failure_count = 0

  # Supports runtime_log as a top level option,
  #   but also nested inside parallel_options
  @runtime_log = opts[:runtime_log] || "tmp/turbo_rspec_runtime.log"
  @parallel_options = opts.fetch(:parallel_options, {})
  @parallel_options[:runtime_log] ||= @runtime_log
  @record_runtime = @parallel_options[:group_by] == :runtime

  @messages = Thread::Queue.new
  @threads = []
  @wait_threads = []
  @exited_process_ids = []
  @error = false
  @print_failed_group = opts[:print_failed_group]
end

Class Method Details

.create(count) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/turbo_tests/runner.rb', line 14

def create(count)
  # We are unable to load parallel tests' tasks in the normal way (top of file)
  # because it requires that the Rails.application instance already be configured
  require "parallel_tests/tasks"

  ENV["PARALLEL_TEST_FIRST_IS_1"] = "true"
  command = ["bundle", "exec", "rake", "db:create", "RAILS_ENV=#{ParallelTests::Tasks.rails_env}"]
  args = {count: count.to_s}
  ParallelTests::Tasks.run_in_parallel(command, args)
end

.run(opts = {}) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/turbo_tests/runner.rb', line 25

def run(opts = {})
  files = opts[:files]
  formatters = opts[:formatters]
  tags = opts[:tags]
  parallel_options = opts[:parallel_options] || {}

  start_time = opts.fetch(:start_time) { RSpec::Core::Time.now }
  runtime_log = opts.fetch(:runtime_log, nil)
  example_status_log = opts.fetch(:example_status_log, nil)
  verbose = opts.fetch(:verbose, false)
  fail_fast = opts.fetch(:fail_fast, nil)
  count = opts.fetch(:count, nil)
  seed = opts.fetch(:seed, nil)
  seed_used = !seed.nil?
  print_failed_group = opts.fetch(:print_failed_group, false)
  nice = opts.fetch(:nice, false)

  use_runtime_info = files == ["spec"]

  if example_status_log
    runtime_log = runtime_log_from_example_status(example_status_log)
    parallel_options[:runtime_log] = runtime_log
  elsif use_runtime_info
    parallel_options[:runtime_log] = runtime_log
  else
    parallel_options[:group_by] = :filesize
  end

  warn("VERBOSE") if verbose

  reporter = Reporter.from_config(formatters, start_time, seed, seed_used, files, parallel_options)

  new(
    reporter: reporter,
    formatters: formatters,
    start_time: start_time,
    files: files,
    tags: tags,
    runtime_log: runtime_log,
    example_status_log: example_status_log,
    verbose: verbose,
    fail_fast: fail_fast,
    count: count,
    seed: seed,
    seed_used: seed_used,
    print_failed_group: print_failed_group,
    use_runtime_info: use_runtime_info,
    parallel_options: parallel_options,
    nice: nice
  ).run
end

.runtime_log_from_example_status(example_status_log) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/turbo_tests/runner.rb', line 77

def runtime_log_from_example_status(example_status_log)
  statuses = RSpec::Core::ExampleStatusPersister.load_from(example_status_log)
  runtimes = statuses.each_with_object(Hash.new(0.0)) do |status, sums|
    next unless status.fetch(:status).match?(/pass/i)

    file_name = RSpec::Core::Example.parse_id(status.fetch(:example_id)).first
    sums[file_name] += status.fetch(:run_time).to_s[/\d+(\.\d+)?/].to_f
  end

  path = File.join("tmp", "turbo_tests2_example_status_runtime.log")
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, runtimes.sort.map { |file, runtime| "#{file}:#{runtime}" }.join("\n"))
  path
end

Instance Method Details

#runObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/turbo_tests/runner.rb', line 126

def run
  @num_processes = [
    ParallelTests.determine_number_of_processes(@count),
    ParallelTests::RSpec::Runner.tests_with_size(@files, {}).size
  ].min

  tests_in_groups =
    ParallelTests::RSpec::Runner.tests_in_groups(
      @files,
      @num_processes,
      **@parallel_options
    )
  @tests_in_groups = tests_in_groups

  subprocess_opts = {
    record_runtime: @record_runtime
  }

  ParallelTests.with_pid_file do
    @reporter.report(tests_in_groups) do |_reporter|
      old_signal = Signal.trap(:INT) { handle_interrupt }

      @wait_threads = tests_in_groups.map.with_index do |tests, process_id|
        start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
      end.compact
      @interrupt_handled = false

      handle_messages

      @threads.each(&:join)

      report_failed_group(tests_in_groups) if @print_failed_group

      Signal.trap(:INT, old_signal)

      if @reporter.failed_examples.empty? && @wait_threads.map(&:value).all?(&:success?)
        0
      else
        # From https://github.com/galtzo-floss/turbo_tests2/pull/20/
        @wait_threads.map { |thread| thread.value.exitstatus }.max
      end
    end
  end
end