feat: locking and retry in FB message parsing (#7701)

This commit is contained in:
Shivam Mishra
2023-08-23 09:48:17 +07:00
committed by GitHub
parent 18235d3fb5
commit 26ef21a243
8 changed files with 194 additions and 17 deletions

View File

@@ -3,7 +3,17 @@ require 'rails_helper'
RSpec.describe Webhooks::FacebookEventsJob do
subject(:job) { described_class.perform_later(params) }
let!(:params) { { test: 'test' } }
let(:params) { { test: 'test' } }
let(:parsed_response) { instance_double(Integrations::Facebook::MessageParser) }
let(:lock_key) { 'FB_MESSAGE_CREATE_LOCK::sender_id::recipient_id' } # Use a real format if needed
let(:lock_manager) { instance_double(Redis::LockManager) }
before do
allow(Integrations::Facebook::MessageParser).to receive(:new).and_return(parsed_response)
allow(parsed_response).to receive(:sender_id).and_return('sender_id')
allow(parsed_response).to receive(:recipient_id).and_return('recipient_id')
allow(Redis::LockManager).to receive(:new).and_return(lock_manager)
end
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
@@ -11,17 +21,44 @@ RSpec.describe Webhooks::FacebookEventsJob do
.on_queue('default')
end
context 'when called with params' do
it 'calls MessagePArsed and do message create' do
parser = double
creator = double
allow(Integrations::Facebook::MessageParser).to receive(:new).and_return(parser)
allow(Integrations::Facebook::MessageCreator).to receive(:new).and_return(creator)
allow(creator).to receive(:perform).and_return(true)
expect(Integrations::Facebook::MessageParser).to receive(:new).with(params)
expect(Integrations::Facebook::MessageCreator).to receive(:new).with(parser)
expect(creator).to receive(:perform)
described_class.perform_now(params)
describe 'job execution' do
context 'when the lock is already acquired' do
before do
allow(lock_manager).to receive(:locked?).and_return(true)
end
it 'raises a LockAcquisitionError' do
perform_enqueued_jobs do
expect { described_class.perform_now(params) }.to raise_error(Webhooks::FacebookEventsJob::LockAcquisitionError)
end
end
end
context 'when the lock is not acquired' do
let(:message_creator) { instance_double(Integrations::Facebook::MessageCreator) }
before do
allow(lock_manager).to receive(:locked?).and_return(false)
allow(lock_manager).to receive(:unlock)
allow(lock_manager).to receive(:lock)
allow(Integrations::Facebook::MessageCreator).to receive(:new).with(parsed_response).and_return(message_creator)
allow(message_creator).to receive(:perform)
end
it 'invokes the message parser and creator' do
expect(Integrations::Facebook::MessageParser).to receive(:new).with(params)
expect(Integrations::Facebook::MessageCreator).to receive(:new).with(parsed_response)
expect(message_creator).to receive(:perform)
described_class.perform_now(params)
end
it 'acquires and releases the lock' do
expect(lock_manager).to receive(:lock).with(lock_key)
expect(lock_manager).to receive(:unlock).with(lock_key)
described_class.perform_now(params)
end
end
end
end

View File

@@ -0,0 +1,48 @@
require 'rails_helper'
RSpec.describe Redis::LockManager do
let(:lock_manager) { described_class.new }
let(:lock_key) { 'test_lock' }
after do
# Cleanup: Ensure that the lock key is deleted after each test to avoid interference
Redis::Alfred.delete(lock_key)
end
describe '#lock' do
it 'acquires a lock and returns true' do
expect(lock_manager.lock(lock_key)).to be true
expect(lock_manager.locked?(lock_key)).to be true
end
it 'returns false if the lock is already acquired' do
lock_manager.lock(lock_key)
expect(lock_manager.lock(lock_key)).to be false
end
it 'can acquire a lock again after the timeout' do
lock_manager.lock(lock_key, 1) # 1-second timeout
sleep 2
expect(lock_manager.lock(lock_key)).to be true
end
end
describe '#unlock' do
it 'releases a lock' do
lock_manager.lock(lock_key)
lock_manager.unlock(lock_key)
expect(lock_manager.locked?(lock_key)).to be false
end
end
describe '#locked?' do
it 'returns true if a key is locked' do
lock_manager.lock(lock_key)
expect(lock_manager.locked?(lock_key)).to be true
end
it 'returns false if a key is not locked' do
expect(lock_manager.locked?(lock_key)).to be false
end
end
end

View File

@@ -73,6 +73,7 @@ RSpec.configure do |config|
config.include Devise::Test::IntegrationHelpers, type: :request
config.include ActiveSupport::Testing::TimeHelpers
config.include ActionCable::TestHelper
config.include ActiveJob::TestHelper
end
Shoulda::Matchers.configure do |config|