Skip to content

A platform for community discussion. Free, open, simple.

DEV: Print puma threads backtraces when Capybara's helpers time out (#32033)

Alan Guo Xiang Tan6f92f42eb83f
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 4fcad805be8..6a1602407f9 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -405,6 +405,19 @@ RSpec.configure do |config|
if RSpec.current_example
# Store timeout for later, we'll only raise it if the test otherwise passes
RSpec.current_example.metadata[:_capybara_timeout_exception] ||= timeout_error
+
+ RSpec.current_example.metadata[:_capybara_server_threads_backtraces] = Thread
+ .list
+ .reduce([]) do |array, thread|
+ if thread.backtrace.any? { |line| line.include?("puma") }
+ app_backtraces = thread.backtrace.filter { |line| !line.match?(%r{/gems/}) }
+ array << app_backtraces if app_backtraces.any?
+ end
+
+ array
+ end
+ .uniq
+
raise # re-raise original error
else
# Outside an example... maybe a `before(:all)` hook?
@@ -735,6 +748,21 @@ RSpec.configure do |config|
config.after(:each, type: :system) do |example|
lines = RSpec.current_example.metadata[:extra_failure_lines]
+ if example.exception &&
+ (
+ backtraces = RSpec.current_example.metadata[:_capybara_server_threads_backtraces]
+ ).present?
+ lines << "~~~~~~~ SERVER THREADS BACKTRACES ~~~~~~~"
+
+ backtraces.each_with_index do |backtrace, index|
+ lines << "\n" if index != 0
+ backtrace.each { |line| lines << line }
+ end
+
+ lines << "~~~~~~~ END SERVER THREADS BACKTRACES ~~~~~~~"
+ lines << "\n"
+ end
+
# This is disabled by default because it is super verbose,
# if you really need to dig into how selenium is communicating
# for system tests then enable it.