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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/turbo_tests/runner.rb', line 72

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 = []
  @error = false
  @print_failed_group = opts[:print_failed_group]
end

Class Method Details

.create(count) ⇒ Object



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

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



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

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

Instance Method Details

#runObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/turbo_tests/runner.rb', line 104

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

  subprocess_opts = {
    record_runtime: @record_runtime,
  }

  @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