mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-20 21:15:01 +00:00
feat: add specs for race condition
This commit is contained in:
@@ -54,6 +54,30 @@ describe Messages::SendEmailNotificationService do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when handling concurrent requests' do
|
||||||
|
let(:inbox) { create(:inbox, account: account, channel: create(:channel_widget, account: account, continuity_via_email: true)) }
|
||||||
|
let(:conversation) { create(:conversation, account: account, inbox: inbox) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
conversation.contact.update!(email: 'test@example.com')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'prevents duplicate workers under race conditions' do
|
||||||
|
# Create 5 threads that simultaneously try to enqueue workers for the same conversation
|
||||||
|
threads = Array.new(5) do
|
||||||
|
Thread.new do
|
||||||
|
msg = create(:message, conversation: conversation, message_type: 'outgoing')
|
||||||
|
described_class.new(message: msg).perform
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
threads.each(&:join)
|
||||||
|
|
||||||
|
# Only ONE worker should be scheduled despite 5 concurrent attempts
|
||||||
|
expect(ConversationReplyEmailWorker.jobs.size).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when email notification should not be sent' do
|
context 'when email notification should not be sent' do
|
||||||
before do
|
before do
|
||||||
allow(ConversationReplyEmailWorker).to receive(:perform_in)
|
allow(ConversationReplyEmailWorker).to receive(:perform_in)
|
||||||
|
|||||||
Reference in New Issue
Block a user