Skip to content

Commit

Permalink
DEV: Capture and log AR debug logs on GitHub actions for flaky tests (d…
Browse files Browse the repository at this point in the history
…iscourse#25048)

Why this change?

We have been running into flaky tests which seems to be related to
AR transaction problems. However, we are not able to reproduce this
locally and do not have sufficient information on our builds now to
debug the problem.

What does this change do?

Noe the following changes only applies when `ENV["GITHUB_ACTIONS"]` is
present.

This change introduces an RSpec around hook when `capture_log: true` has
been set for a test. The responsibility of the hook is to capture the
ActiveRecord debug logs and print them out.
  • Loading branch information
tgxworld authored Dec 27, 2023
1 parent 7852daa commit 655c106
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
config.assets.compile = true
config.assets.digest = false

config.active_record.verbose_query_logs = true

config.after_initialize do
ActiveRecord::LogSubscriber.backtrace_cleaner.add_silencer do |line|
line =~ %r{lib/freedom_patches}
end
end

if ENV["RAILS_ENABLE_TEST_LOG"]
config.logger = Logger.new(STDOUT)
config.log_level =
Expand Down
13 changes: 13 additions & 0 deletions lib/turbo_tests/documentation_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def example_passed(notification)
:success,
)

output_activerecord_debug_logs(output, notification.example)

output.flush
end

Expand All @@ -31,11 +33,22 @@ def example_failed(notification)
:failure,
)

output_activerecord_debug_logs(output, notification.example)

output.flush
end

private

def output_activerecord_debug_logs(output, example)
if ENV["GITHUB_ACTIONS"] &&
active_record_debug_logs = example.metadata[:active_record_debug_logs]
output.puts "::group::ActiveRecord Debug Logs"
output.puts active_record_debug_logs
output.puts "::endgroup::"
end
end

def output_example(example)
output =
+"[#{example.process_id}] (##{example.metadata[:process_pid]}) #{example.full_description}"
Expand Down
1 change: 1 addition & 0 deletions lib/turbo_tests/json_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def to_json
run_duration_ms: @rspec_example.metadata[:run_duration_ms],
process_pid: Process.pid,
js_deprecations: @rspec_example.metadata[:js_deprecations],
active_record_debug_logs: @rspec_example.metadata[:active_record_debug_logs],
},
location_rerun_argument: @rspec_example.location_rerun_argument,
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/chat/spec/system/single_thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

before { channel.update!(threading_enabled: false) }

it "does not open the side panel for a single thread" do
it "does not open the side panel for a single thread", capture_log: true do
thread =
chat_thread_chain_bootstrap(channel: channel, users: [current_user, Fabricate(:user)])
chat_page.visit_channel(channel)
Expand Down
16 changes: 16 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ class SpecTimeoutError < StandardError
end
end

if ENV["GITHUB_ACTIONS"]
config.around :each, capture_log: true do |example|
original_logger = ActiveRecord::Base.logger
io = StringIO.new
io_logger = Logger.new(io)
io_logger.level = Logger::DEBUG
ActiveRecord::Base.logger = io_logger

example.run

RSpec.current_example.metadata[:active_record_debug_logs] = io.string
ensure
ActiveRecord::Base.logger = original_logger
end
end

config.before :each do
# This allows DB.transaction_open? to work in tests. See lib/mini_sql_multisite_connection.rb
DB.test_transaction = ActiveRecord::Base.connection.current_transaction
Expand Down

0 comments on commit 655c106

Please sign in to comment.