gpt4 book ai didi

ruby-on-rails - 使用 capybara-webkit 检测到死锁

转载 作者:数据小太阳 更新时间:2023-10-29 06:56:34 24 4
gpt4 key购买 nike

我正在尝试通过此规范:

scenario "Edit a service", js: true do
service = create_service_for(provider, title: "First service")
fill_edit_service_form(service)
expect(page).to have_css('#price', text: '10,00 $')
end

这是使用 capybara 的标准 Rails 规范。我正在使用 capybara-webkit 处理所有使用 javascript 的场景。当我尝试传递它时,有时它会起作用,有时它会标记数据库中缺少记录,有时我会遇到此错误:

Run options: include {:locations=>{"./spec/acceptances/provider_services_spec.rb"=>[31]}}
[K 1) Provider Services Edit a service
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::StatementInvalid:
PG::TRDeadlockDetected: ERROR: deadlock detected
DETAIL: Process 24164 waits for AccessExclusiveLock on relation 3446991 of database 3446538; blocked by process 24184.
Process 24184 waits for AccessShareLock on relation 3446902 of database 3446538; blocked by process 24164.
HINT: See server log for query details.
: ALTER TABLE "active_admin_comments" DISABLE TRIGGER ALL;ALTER TABLE "provider_service_territory_provideds" DISABLE TRIGGER ALL;ALTER TABLE "provider_services" DISABLE TRIGGER ALL;ALTER TABLE "provider_divisions" DISABLE TRIGGER ALL;ALTER TABLE "provider_profiles" DISABLE TRIGGER ALL;ALTER TABLE "provider_service_intervention_level_provideds" DISABLE TRIGGER ALL;ALTER TABLE "provider_service_medium_provideds" DISABLE TRIGGER ALL;ALTER TABLE "provider_service_service_provideds" DISABLE TRIGGER ALL;ALTER TABLE "regions" DISABLE TRIGGER ALL;ALTER TABLE "service_formulas" DISABLE TRIGGER ALL;ALTER TABLE "region_translations" DISABLE TRIGGER ALL;ALTER TABLE "artists" DISABLE TRIGGER ALL;ALTER TABLE "admin_users" DISABLE TRIGGER ALL;ALTER TABLE "schema_migrations" DISABLE TRIGGER ALL;ALTER TABLE "services" DISABLE TRIGGER ALL;ALTER TABLE "services_provided" DISABLE TRIGGER ALL;ALTER TABLE "territories_provided" DISABLE TRIGGER ALL;ALTER TABLE "users" DISABLE TRIGGER ALL;ALTER TABLE "countries" DISABLE TRIGGER ALL;ALTER TABLE "coupons" DISABLE TRIGGER ALL;ALTER TABLE "currencies" DISABLE TRIGGER ALL;ALTER TABLE "formulas" DISABLE TRIGGER ALL;ALTER TABLE "collector_profiles" DISABLE TRIGGER ALL;ALTER TABLE "country_translations" DISABLE TRIGGER ALL;ALTER TABLE "images" DISABLE TRIGGER ALL;ALTER TABLE "intervention_levels_provided" DISABLE TRIGGER ALL;ALTER TABLE "measure_units" DISABLE TRIGGER ALL;ALTER TABLE "media_provided" DISABLE TRIGGER ALL;ALTER TABLE "messages" DISABLE TRIGGER ALL;ALTER TABLE "page_part_translations" DISABLE TRIGGER ALL;ALTER TABLE "orders" DISABLE TRIGGER ALL;ALTER TABLE "painting_categories" DISABLE TRIGGER ALL;ALTER TABLE "page_services" DISABLE TRIGGER ALL;ALTER TABLE "page_translations" DISABLE TRIGGER ALL;ALTER TABLE "painting_category_translations" DISABLE TRIGGER ALL;ALTER TABLE "page_parts" DISABLE TRIGGER ALL;ALTER TABLE "pages" DISABLE TRIGGER ALL;ALTER TABLE "painting_provenances" DISABLE TRIGGER ALL;ALTER TABLE "painting_prices" DISABLE TRIGGER ALL;ALTER TABLE "painting_technic_translations" DISABLE TRIGGER ALL;ALTER TABLE "painting_technics" DISABLE TRIGGER ALL;ALTER TABLE "painting_type_translations" DISABLE TRIGGER ALL;ALTER TABLE "period_translations" DISABLE TRIGGER ALL;ALTER TABLE "painting_types" DISABLE TRIGGER ALL;ALTER TABLE "paintings" DISABLE TRIGGER ALL;ALTER TABLE "periods" DISABLE TRIGGER ALL
# -e:1:in `<main>'

1/1 |========================= 100 ==========================>| Time: 00:00:03

Finished in 3.41 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/acceptances/provider_services_spec.rb:29 # Provider Services Edit a service
Screenshot: /home/dougui/rails/lescollectionneurs/tmp/capybara/screenshot_2014-02-20-18-22-55.658.png

Randomized with seed 34053

我在表中有一把锁。这并不总是同一张 table 。

我这样做会更好:

Capybara.reset_sessions!
DatabaseCleaner.clean

规范之前和之后,但它并不总是有效。如果我在同一个文件中运行所有规范,我将无法工作。

它是在我处理不相关的事情时附加的。它适用于 poltergeist。

这是我的 spec_helper 文件:

ENV["RAILS_ENV"] ||= 'test'
require 'rubygems'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara-webkit'
require 'database_cleaner'
require 'capybara/firebug'
require 'capybara-screenshot/rspec'

if defined?(Spring)
Spring.watch "#{Rails.root}/spec/factories"
else
require 'shoulda-matchers'
end

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.use_transactional_fixtures = false
config.order = "random"

config.include FactoryGirl::Syntax::Methods
config.include Warden::Test::Helpers, type: :feature
config.include FeatureHelpers, type: :feature
config.include Devise::TestHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include EmailSpec::Helpers
config.include EmailSpec::Matchers

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
if example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.start
end
end

config.after(:each) do
DatabaseCleaner.clean
DatabaseCleaner.strategy = :transaction if example.metadata[:js]
end
end

Capybara.javascript_driver = :webkit

include ActionDispatch::TestProcess
I18n.locale = :fr

Geocoder.configure(:lookup => :test)

Geocoder::Lookup::Test.set_default_stub(
[
{
'latitude' => 40.7143528,
'longitude' => -74.0059731,
'address' => 'New York, NY, USA',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
}
]
)

这是使用的 gem :https://github.com/GCorbel/lescollectionneursassocies/blob/master/Gemfile .

有什么建议吗?

更多详细信息,请查看 Github 存储库:https://github.com/GCorbel/lescollectionneursassocies/ .

最佳答案

发生这种情况是因为您有两个线程,即测试线程和请求线程,异步修改数据库。看起来您一直在正确地在 spec_helper 中尝试不同的配置。我花了相当多的时间来解决同样的问题,并得出了这个结论:

# adapted from https://gist.github.com/moonfly/4950750

require 'database_cleaner'

RSpec.configure do |config|

config.use_transactional_fixtures = false

config.before( :suite ) do
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.strategy = :transaction
end

config.around( :each ) do |spec|
if spec.metadata[:js]
# JS => doesn't share connections => can't use transactions
spec.run
DatabaseCleaner.clean_with :deletion
else
# No JS/Devise => run with Rack::Test => transactions are ok
DatabaseCleaner.start
spec.run
DatabaseCleaner.clean

# see https://github.com/bmabey/database_cleaner/issues/99
begin
ActiveRecord::Base.connection.send :rollback_transaction_records, true
rescue
end
end
end

end

我将其全部保存在 support/database_cleaner_configuration.rb 中,这对您也适用,或者您可以简单地替换 spec_helper 中的内容。如果这行不通,请告诉我 - 我还有其他一些想法,但它们比较古怪,如果行得通,则不值得深入探讨,但它可能会...

注意:

可能值得一提的是,Rails 5.1+ 解决了数据库问题。 Eileen Uchitelle on the Rails 团队制作了the changes necessary运行确保测试线程和 Rails 服务器可以通过共享数据库连接在同一进程中运行。

关于ruby-on-rails - 使用 capybara-webkit 检测到死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21922046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com