Class: TurboTests::JsonRowsFormatter

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

Overview

An RSpec formatter used for each subprocess during parallel test execution

Constant Summary collapse

INTERNAL_BACKTRACE_PATTERNS =
[
  %r{/bin/turbo_tests2\b},
  %r{/exe/turbo_tests2\b},
  %r{/lib/turbo_tests(?:\.rb|/)}
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ JsonRowsFormatter

Returns a new instance of JsonRowsFormatter.



47
48
49
50
51
# File 'lib/turbo_tests/json_rows_formatter.rb', line 47

def initialize(output)
  @output = output
  @current_example = nil
  @last_progress = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Attribute Details

#output ⇒ Object (readonly)

Returns the value of attribute output.



45
46
47
# File 'lib/turbo_tests/json_rows_formatter.rb', line 45

def output
  @output
end

Instance Method Details

#close(_notification) ⇒ Object



171
172
173
174
175
176
# File 'lib/turbo_tests/json_rows_formatter.rb', line 171

def close(_notification)
  @stall_dumper&.kill
  output_row(
    type: :close
  )
end

#deprecation(notification) ⇒ Object



185
186
187
188
189
190
# File 'lib/turbo_tests/json_rows_formatter.rb', line 185

def deprecation(notification)
  output_row(
    type: :deprecation,
    deprecation: deprecation_to_json(notification)
  )
end

#dump_profile(notification) ⇒ Object



192
193
194
195
196
197
# File 'lib/turbo_tests/json_rows_formatter.rb', line 192

def dump_profile(notification)
  output_row(
    type: :profile,
    profile: profile_to_json(notification)
  )
end

#dump_stall(idle) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/turbo_tests/json_rows_formatter.rb', line 92

def dump_stall(idle)
  lines = ["turbo_tests2 worker #{ENV["TEST_ENV_NUMBER"].inspect} pid #{Process.pid}: " \
    "no example finished for #{idle.round}s; current example: #{@current_example.inspect}"]
  Thread.list.each do |thread|
    lines << "--- #{thread.inspect}"
    lines.concat(Array(thread.backtrace).map { |frame| "    #{frame}" })
  end
  @stall_io.puts(lines.join("\n"))
  @stall_io.flush
rescue
  nil
end

#example_failed(notification) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/turbo_tests/json_rows_formatter.rb', line 156

def example_failed(notification)
  touch_progress
  output_row(
    type: :example_failed,
    example: example_to_json(notification.example)
  )
end

#example_group_finished(notification) ⇒ Object



133
134
135
136
137
138
# File 'lib/turbo_tests/json_rows_formatter.rb', line 133

def example_group_finished(notification)
  output_row(
    type: :group_finished,
    group: group_to_json(notification)
  )
end

#example_group_started(notification) ⇒ Object



126
127
128
129
130
131
# File 'lib/turbo_tests/json_rows_formatter.rb', line 126

def example_group_started(notification)
  output_row(
    type: :group_started,
    group: group_to_json(notification)
  )
end

#example_passed(notification) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/turbo_tests/json_rows_formatter.rb', line 140

def example_passed(notification)
  touch_progress
  output_row(
    type: :example_passed,
    example: example_to_json(notification.example)
  )
end

#example_pending(notification) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/turbo_tests/json_rows_formatter.rb', line 148

def example_pending(notification)
  touch_progress
  output_row(
    type: :example_pending,
    example: example_to_json(notification.example)
  )
end

#example_started(notification) ⇒ Object



53
54
55
56
# File 'lib/turbo_tests/json_rows_formatter.rb', line 53

def example_started(notification)
  @current_example = notification.example.location
  touch_progress
end

#message(notification) ⇒ Object



178
179
180
181
182
183
# File 'lib/turbo_tests/json_rows_formatter.rb', line 178

def message(notification)
  output_row(
    type: :message,
    message: notification.message
  )
end

#seed(notification) ⇒ Object



164
165
166
167
168
169
# File 'lib/turbo_tests/json_rows_formatter.rb', line 164

def seed(notification)
  output_row(
    type: :seed,
    seed: notification.seed
  )
end

#stall_dump_seconds ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/turbo_tests/json_rows_formatter.rb', line 105

def stall_dump_seconds
  explicit = ENV["TURBO_TESTS2_STACK_DUMP_SECONDS"]
  return Float(explicit) if explicit

  Float(ENV.fetch("TURBO_TESTS2_WATCHDOG_SECONDS", "900")) / 2
rescue ArgumentError
  0.0
end

#start(notification) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/turbo_tests/json_rows_formatter.rb', line 118

def start(notification)
  start_stall_dumper
  output_row(
    type: :load_summary,
    summary: load_summary_to_json(notification)
  )
end

#start_stall_dumper ⇒ Object

When no example finishes for a long time the parent’s watchdog can only
say “silent”. The worker is the only one that can say where it is stuck,
so dump every thread’s backtrace to stderr (which the parent surfaces)
once per stall, at half the watchdog. Disabled when the watchdog is.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/turbo_tests/json_rows_formatter.rb', line 62

def start_stall_dumper
  threshold = stall_dump_seconds
  return unless threshold.positive?

  # Specs routinely silence or swap $stderr (silent_stream reopens the fd),
  # which would swallow the dump exactly when a spec hangs inside such a
  # block. Keep our own duplicate of the original stderr.
  @stall_io = begin
    $stderr.dup
  rescue
    $stderr
  end
  # rubocop:disable ThreadSafety/NewThread -- watchdog helper thread, one per worker process.
  @stall_dumper = Thread.new do
    Thread.current.report_on_exception = false
    dumped = false
    loop do
      sleep([threshold / 4.0, 1.0].max)
      idle = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @last_progress
      if idle < threshold
        dumped = false
      elsif !dumped
        dumped = true
        dump_stall(idle)
      end
    end
  end
  # rubocop:enable ThreadSafety/NewThread
end

#touch_progress ⇒ Object



114
115
116
# File 'lib/turbo_tests/json_rows_formatter.rb', line 114

def touch_progress
  @last_progress = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end