Commit f95fa714 authored by Timothy Andrew's avatar Timothy Andrew

Write a spec covering the race condition during group deletion.

- Use multiple threads / database connections to:

  1. Escape the transaction the spec seems to be running
     in (`config.use_transactional_fixtures` is off, but
     `ActiveRecord::Base.connection.open_transactions` is not empty
     at the beginning of the spec.

  2. Simulate a Sidekiq worker performing the hard delete outside of the
     soft-delete transaction.

- The spec is a little clunky, but it was the smallest thing I could get
  working - and even this took a couple of hours. Let me know if you
  have any suggestions to improve it!
parent bf9ab0f3
require 'spec_helper' require 'spec_helper'
describe DestroyGroupService, services: true do describe DestroyGroupService, services: true do
include DatabaseConnectionHelpers
let!(:user) { create(:user) } let!(:user) { create(:user) }
let!(:group) { create(:group) } let!(:group) { create(:group) }
let!(:project) { create(:project, namespace: group) } let!(:project) { create(:project, namespace: group) }
...@@ -50,6 +52,44 @@ describe DestroyGroupService, services: true do ...@@ -50,6 +52,44 @@ describe DestroyGroupService, services: true do
describe 'asynchronous delete' do describe 'asynchronous delete' do
it_behaves_like 'group destruction', true it_behaves_like 'group destruction', true
context 'potential race conditions' do
context "when the `GroupDestroyWorker` task runs immediately" do
it "deletes the group" do
# Commit the contents of this spec's transaction so far
# so subsequent db connections can see it.
#
# DO NOT REMOVE THIS LINE, even if you see a WARNING with "No
# transaction is currently in progress". Without this, this
# spec will always be green, since the group created in setup
# cannot be seen by any other connections / threads in this spec.
Group.connection.commit_db_transaction
group_record = run_with_new_database_connection do |conn|
conn.execute("SELECT * FROM namespaces WHERE id = #{group.id}").first
end
expect(group_record).not_to be_nil
# Execute the contents of `GroupDestroyWorker` in a separate thread, to
# simulate data manipulation by the Sidekiq worker (different database
# connection / transaction).
expect(GroupDestroyWorker).to receive(:perform_async).and_wrap_original do |m, group_id, user_id|
Thread.new { m[group_id, user_id] }.join(5)
end
# Kick off the initial group destroy in a new thread, so that
# it doesn't share this spec's database transaction.
Thread.new { DestroyGroupService.new(group, user).async_execute }.join(5)
group_record = run_with_new_database_connection do |conn|
conn.execute("SELECT * FROM namespaces WHERE id = #{group.id}").first
end
expect(group_record).to be_nil
end
end
end
end end
describe 'synchronous delete' do describe 'synchronous delete' do
......
module DatabaseConnectionHelpers
def run_with_new_database_connection
pool = ActiveRecord::Base.connection_pool
conn = pool.checkout
yield conn
ensure
pool.checkin(conn)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment