From 2bcd73f47d721174ae604a8904197bed1b0bb2b1 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Sat, 14 Dec 2019 10:03:46 +0000 Subject: [PATCH] Attempt to fix random CI failures We get frequent random CI failures with errors on feature specs, like the following: ``` Failures: 1) Search admin searches with an unknown filter Failure/Error: DatabaseCleaner.clean ActiveRecord::StatementInvalid: PG::TRDeadlockDetected: ERROR: deadlock detected DETAIL: Process 12270 waits for AccessExclusiveLock on relation 16412 of database 16386; blocked by process 12192. Process 12192 waits for AccessShareLock on relation 16400 of database 16386; blocked by process 12270. HINT: See server log for query details. : TRUNCATE TABLE "public"."blog_posts", "public"."series", "public"."countries", "public"."log_entries", "public"."payments", "public"."product_meta_tags", "public"."customers", "public"."products", "public"."orders", "public"."line_items" RESTART IDENTITY CASCADE; # ./spec/support/database_cleaner.rb:19:in `block (2 levels) in ' # ------------------ # --- Caused by: --- # PG::TRDeadlockDetected: # ERROR: deadlock detected # DETAIL: Process 12270 waits for AccessExclusiveLock on relation 16412 of database 16386; blocked by process 12192. # Process 12192 waits for AccessShareLock on relation 16400 of database 16386; blocked by process 12270. # HINT: See server log for query details. # ./spec/support/database_cleaner.rb:19:in `block (2 levels) in ' Finished in 29.41 seconds (files took 3.65 seconds to load) 418 examples, 1 failure ``` This may or may not be related to `DatabaseCleaner`. As an attempt to fix it, I'm trying this configuration, copied straight from their documentation at https://github.com/DatabaseCleaner/database_cleaner/blob/v1.7.0/README.markdown --- spec/support/database_cleaner.rb | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 65d31433da..1b31f4cfac 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -1,5 +1,17 @@ RSpec.configure do |config| config.before(:suite) do + if config.use_transactional_fixtures? + raise(<<-MSG) + Delete line `config.use_transactional_fixtures = true` from rails_helper.rb + (or set it to false) to prevent uncommitted transactions being used in + JavaScript-dependent specs. + + During testing, the app-under-test that the browser driver connects to + uses a different database connection to the database connection used by + the spec. The app's database connection would not be able to access + uncommitted transaction data setup over the spec's database connection. + MSG + end DatabaseCleaner.clean_with(:truncation) end @@ -7,15 +19,24 @@ DatabaseCleaner.strategy = :transaction end - config.before(:each, :js => true) do - DatabaseCleaner.strategy = :truncation + config.before(:each, type: :feature) do + # :rack_test driver's Rack app under test shares database connection + # with the specs, so continue to use transaction strategy for speed. + driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test + + unless driver_shares_db_connection_with_specs + # Driver is probably for an external browser with an app + # under test that does *not* share a database connection with the + # specs, so use truncation strategy. + DatabaseCleaner.strategy = :truncation + end end config.before(:each) do DatabaseCleaner.start end - config.after(:each) do + config.append_after(:each) do DatabaseCleaner.clean end end