gpt4 book ai didi

ruby-on-rails - 使用 Rspec + Factory Girl 在新线程/进程测试中数据库为空

转载 作者:数据小太阳 更新时间:2023-10-29 07:18:15 26 4
gpt4 key购买 nike

在这篇博文之后,http://blog.arkency.com/2015/09/testing-race-conditions/

我正在尝试测试并发性。但是在我的规范中,当我启动一个新线程或 fork 一个进程时,我无法找到记录。

describe 'test concurrency' do
let(:order_1) { create(:order) }
let(:order_2) { create(:order) }
let(:order_3) { create(:order) }
let(:order_4) { create(:order) }
let(:product) { create(:product) }

it 'test concurrency' do
wait_for_all_threads = true
product.update(quantity_available: 4, quantity_sold: 0, quantity_in_carts: 0)
orders = [order_1, order_2, order_3, order_4]

# Objects are persisted in here
# because order_n.persisted => true
# product.persisted => true
# product.reload works without issue (and this is essentially what causes the RecordNotFound error in Order#add_item)

threads = orders.map do |order|
Thread.new do

# Objects cannot be found in the database

true while wait_for_all_threads

item = order.add_item(product, 1)
order_items << item
end
end

wait_for_all_threads = false

threads.each(&:join)

# ...
# here comes all the expects ...
# not gonna post it because it's not what matters in this question
end
end

经过研究,我设置了这些:

DatabaseCleaner.strategy = :truncation
config.use_transactional_fixtures = false

但运气不好。

所以问题是,在新线程中,在数据库中找不到任何创建的对象。

我正在挖掘的一些线索:

  • 当我通过 sql 客户端直接连接时,数据库一直是空的。 (当我在测试中添加一个断点,并验证对象是 persisted?)
  • factory girl/rspec 是否真的将数据提交到数据库?如果不是,那么新线程将无法读取它,因为它仍在 bin 日志或内存中? (这提示我是因为我记得 after_commit 不会在 rspec 中运行)

最佳答案

使用 Rails 5 这对我很有效。放置在描述中:

self.use_transactional_tests = false

“use_transactional_fixtures”已被弃用。

我还必须通过以下方式绕过 DatabaseCleaner:

it 'tests concurrency', bypass_cleaner: true do

end

然后在 rails_helper(或者你有 DatabaseCleaner 设置的地方)

config.around(:each) do |example|
if example.metadata[:bypass_cleaner]
example.run
else
# database cleaner code
end
end

我希望这对您或其他人有所帮助:)

关于ruby-on-rails - 使用 Rspec + Factory Girl 在新线程/进程测试中数据库为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42544541/

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