Skip to content

Commit

Permalink
CCOL-2039: Add retries spec for mass update and deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Pereira committed Dec 6, 2023
1 parent 1ea3069 commit 1e5d3a5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
42 changes: 42 additions & 0 deletions spec/active_record_batch_consumer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,48 @@ def publish_batch(messages)
]
)
end

it 'should handle deletes with deadlock retries' do
allow(Deimos::Utils::DeadlockRetry).to receive(:sleep)
allow(instance_double(ActiveRecord::Relation)).to receive(:delete_all).and_raise(
ActiveRecord::Deadlocked.new('Lock wait timeout exceeded')
).twice.ordered

Widget.create!(id: 1, test_id: 'abc', some_int: 2)

publish_batch(
[
{ key: 1,
payload: nil },
{ key: 1,
payload: nil }
]
)

expect(all_widgets).to be_empty
end

it 'should not delete after multiple deadlock retries' do
allow(Deimos::Utils::DeadlockRetry).to receive(:sleep)
allow(instance_double(ActiveRecord::Relation)).to receive(:delete_all).and_raise(
ActiveRecord::Deadlocked.new('Lock wait timeout exceeded')
).exactly(3).times

Widget.create!(id: 1, test_id: 'abc', some_int: 2)

publish_batch(
[
{ key: 1,
payload: nil },
{ key: 1,
payload: nil }
]
)

expect(Widget.count).to eq(0)

end

end
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/active_record_consume/mass_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,40 @@
expect(Widget.last.detail).not_to be_nil
end

context 'with deadlock retries' do
before(:each) do
allow(Deimos::Utils::DeadlockRetry).to receive(:sleep)
end

it 'should upsert rows after deadlocks' do
allow(Widget).to receive(:import!).and_raise(
ActiveRecord::Deadlocked.new('Lock wait timeout exceeded')
).twice.ordered
allow(Widget).to receive(:import!).and_raise(
ActiveRecord::Deadlocked.new('Lock wait timeout exceeded')
).once.and_call_original

results = described_class.new(Widget, bulk_import_id_generator: bulk_id_generator).mass_update(batch)
expect(results.count).to eq(2)
expect(results.map(&:test_id)).to match(%w(id1 id2))
expect(Widget.count).to eq(2)
expect(Detail.count).to eq(2)
expect(Widget.first.detail).not_to be_nil
expect(Widget.last.detail).not_to be_nil
end

it 'should not upsert after encountering multiple deadlocks' do
allow(Widget).to receive(:import!).and_raise(
ActiveRecord::Deadlocked.new('Lock wait timeout exceeded')
).exactly(3).times
expect {
described_class.new(Widget, bulk_import_id_generator: bulk_id_generator).mass_update(batch)
}.to raise_error(ActiveRecord::Deadlocked)
expect(Widget.count).to eq(0)
expect(Detail.count).to eq(0)
end

end

end
end

0 comments on commit 1e5d3a5

Please sign in to comment.