Answer the question
In order to leave comments, you need to log in
How to write an rspec test to test a controller for race conditions?
Good afternoon everyone, I ask for help. How do I write a test for a RoR application in which I want to test for the absence of race conditions in a controller action. Read the article Testing race conditions in your Rails app by Robert Pankowecki. It literally came out like this:
describe Cabinet::Balance::BalanceController do
render_views
controller_methods = [{name: :prolongation, method: 'POST', params: {}}]
describe 'prolongation action' do
it 'ловим двойное списание средств' do
active_adv = FactoryBot.create :active_adv
active_adv.touch :expired_at
active_adv.save
user = active_adv.user
user.amount = PriceList.prolongation.price
user.save
# Логинимся, продлеваем объявление
login_user user
threads = 2.times.map do |i|
Thread.new do
request.env["HTTP_ACCEPT"] = 'application/json'
post :prolongation, :params => {id: active_adv.id}
expect(response).to have_http_status :ok
end
end
threads.each(&:join)
end
end
end
The blog post was written for testing models/service objects. I am not sure if controllers tests are thread-safe in Rails. If there are many requests which request/response do you expect to be testing? It might be easier for you to test this logic if you extract non-http related concerns to a service and test that without doing HTTP requests. – Robert Pankowecki
Answer the question
In order to leave comments, you need to log in
In the article, he tests the race condition on the example of database access.
This line is very important, it is just about the pool of connections to the database.
expect(ActiveRecord::Base.connection.pool.size).to eq(5)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question