Files
nairobi/spec/spec_helper.rb
Javi Martín d2d517059d Fix race condition with ballot lines
With two concurrent requests, it's possible to create two ballot lines
when only one of them should be created.

The reason is the code validating the line is not thread safe:

```
if ballot.amount_available(investment.heading) < investment.price.to_i
  errors.add(:money, "insufficient funds")
end
```

If the second request executes this code after the first request has
executed it but before the first request has saved the record to the
database, both records will pass this validation and both will be saved
to the database.

So we need to introduce a lock. Now when the second request tries to
lock the ballot, it finds it's already locked by the first request, and
will wait for the transaction of the first request to finish before
checking whether there are sufficient funds.

Note we need to disable transactions during the test; otherwise the
second thread will wait for the first one to finish.

Also note that we need to update a couple of tests because records are
reloaded when they're locked.

In one case, reloading the ballot causes `ballot.user` to be `nil`,
since the user is hidden. So we hide the user after creating all its
associated records (which is the scenario that would take place in real
life).

In the other case, reloading the ballot causes `ballot.user` to reload
as well. So we need to reload the user object used in the test too so it
gets the updates done on `ballot.user`.

I haven't been able to reproduce this behavior in a system test. The
following test works with Rails 5.0, but it stopped working when we
moved to system tests in commit 9427f014. After that commit, for reasons
I haven't been able to debug (reintroducing truncation with
DatabaseClaner didn't seem to affect this test, and neither did
increasing the number of threads in Puma), the two AJAX requests
executed here are no longer simultaneous; the second request waits for
the first one to finish.

scenario "Race conditions with simultaneous requests", :js do
  allow_any_instance_of(Budget::Ballot::Line).to receive(:check_sufficient_funds) do |object|
    allow(object).to receive(:check_sufficient_funds).and_call_original
    object.check_sufficient_funds
    sleep 0.3
  end

  ["First", "Second"].each do |title|
    create(:budget_investment, :selected,
      heading: california,
      price:   california.price,
      title:   title
    )
  end

  login_as(user)
  visit budget_investments_path(budget, heading_id: california.id)

  within(".budget-investment", text: "First") { click_link "Vote" }
  within(".budget-investment", text: "Second") { click_link "Vote" }

  expect(page).to have_link "Remove vote"
  expect(Budget::Ballot::Line.count).to eq 1
end
2020-07-12 22:11:40 +02:00

151 lines
4.6 KiB
Ruby

require "factory_bot_rails"
require "email_spec"
require "devise"
require "knapsack_pro"
Dir["./spec/models/concerns/*.rb"].each { |f| require f }
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
Dir["./spec/shared/**/*.rb"].sort.each { |f| require f }
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.filter_run :focus
config.run_all_when_everything_filtered = true
config.include RequestSpecHelper, type: :request
config.include Devise::Test::ControllerHelpers, type: :controller
config.include FactoryBot::Syntax::Methods
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.include(CommonActions)
config.include(ActiveSupport::Testing::TimeHelpers)
config.before(:suite) do
Rails.application.load_seed
end
config.before do |example|
I18n.locale = :en
Globalize.set_fallbacks_to_all_available_locales
Setting["feature.user.skip_verification"] = nil
end
config.around(:each, :race_condition) do |example|
self.use_transactional_tests = false
example.run
self.use_transactional_tests = true
DatabaseCleaner.clean_with(:truncation)
Rails.application.load_seed
end
config.before(:each, type: :system) do
Capybara::Webmock.start
end
config.after(:suite) do
Capybara::Webmock.stop
end
config.after(:each, :page_driver) do
page.driver.reset!
end
config.before(:each, type: :system) do |example|
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by :headless_chrome
end
config.before(:each, type: :system) do
Bullet.start_request
allow(InvisibleCaptcha).to receive(:timestamp_threshold).and_return(0)
end
config.after(:each, type: :system) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
Bullet.end_request
end
config.before(:each, :delay_jobs) do
Delayed::Worker.delay_jobs = true
end
config.after(:each, :delay_jobs) do
Delayed::Worker.delay_jobs = false
end
config.before(:each, :remote_translations) do
allow(RemoteTranslations::Microsoft::AvailableLocales)
.to receive(:available_locales).and_return(I18n.available_locales.map(&:to_s))
end
config.before(:each, :with_frozen_time) do
travel_to Time.current # TODO: use `freeze_time` after migrating to Rails 5.2.
end
config.after(:each, :with_frozen_time) do
travel_back
end
config.before(:each, :application_zone_west_of_system_zone) do
application_zone = ActiveSupport::TimeZone.new("Quito")
system_zone = ActiveSupport::TimeZone.new("Madrid")
allow(Time).to receive(:zone).and_return(application_zone)
system_time_at_application_end_of_day = Date.current.end_of_day.in_time_zone(system_zone)
allow(Time).to receive(:now).and_return(system_time_at_application_end_of_day)
allow(Date).to receive(:today).and_return(system_time_at_application_end_of_day.to_date)
end
config.before(:each, :with_non_utc_time_zone) do
application_zone = ActiveSupport::TimeZone.new("Madrid")
allow(Time).to receive(:zone).and_return(application_zone)
end
config.before(:each, :spanish_search) do |example|
allow(SearchDictionarySelector).to receive(:call).and_return("spanish")
end
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options.
config.example_status_persistence_file_path = "spec/examples.txt"
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
# config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
config.expect_with(:rspec) { |c| c.syntax = :expect }
end
# Parallel build helper configuration for travis
KnapsackPro::Adapters::RSpecAdapter.bind