diff --git a/spec/services/pfmp_manager_spec.rb b/spec/services/pfmp_manager_spec.rb index b16afd120..96ef43c90 100644 --- a/spec/services/pfmp_manager_spec.rb +++ b/spec/services/pfmp_manager_spec.rb @@ -24,6 +24,38 @@ let(:student) { create(:student, :with_all_asp_info) } let(:schooling) { create(:schooling, student: student, classe: classe) } + describe "concurrent updates" do + let(:pfmp1) { create(:pfmp, schooling: schooling, day_count: 2) } # rubocop:disable RSpec/IndexedLet + let(:pfmp2) { create(:pfmp, schooling: schooling, day_count: 3) } # rubocop:disable RSpec/IndexedLet + let(:manager1) { described_class.new(pfmp1) } # rubocop:disable RSpec/IndexedLet + let(:manager2) { described_class.new(pfmp2) } # rubocop:disable RSpec/IndexedLet + + it "prevents concurrent updates on the same schooling" do # rubocop:disable RSpec/ExampleLength + executed_second = false + + thread1 = Thread.new do + Pfmp.transaction do + manager1.update!(day_count: 5) + sleep(0.2) + end + end + + sleep(0.1) + + thread2 = Thread.new do + manager2.update!(day_count: 7) + executed_second = true + end + + thread1.join + thread2.join + + expect(executed_second).to be true + expect(pfmp1.reload.day_count).to eq 5 + expect(pfmp2.reload.day_count).to eq 7 + end + end + describe "#create_new_payment_request!" do context "when previous payment requests are inactive" do let(:pfmp) { create(:asp_payment_request, :rejected).pfmp }