mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 09:08:10 +08:00
DEV: new lock
step for services (#30872)
That allows services to wrap steps within a Distributed lock (mutex).
This commit is contained in:
@ -167,6 +167,20 @@ RSpec.describe Service::Runner do
|
||||
end
|
||||
end
|
||||
|
||||
class LockService
|
||||
include Service::Base
|
||||
|
||||
params do
|
||||
attribute :post_id, :integer
|
||||
attribute :user_id, :integer
|
||||
end
|
||||
|
||||
lock(:post_id, :user_id) { step :locked_step }
|
||||
|
||||
def locked_step
|
||||
end
|
||||
end
|
||||
|
||||
describe ".call" do
|
||||
subject(:runner) { described_class.call(service, dependencies, &actions_block) }
|
||||
|
||||
@ -501,5 +515,36 @@ RSpec.describe Service::Runner do
|
||||
expect(runner).to eq :success
|
||||
end
|
||||
end
|
||||
|
||||
context "when aquiring a lock" do
|
||||
let(:service) { LockService }
|
||||
let(:dependencies) { { params: { post_id: 123, user_id: 456 } } }
|
||||
let(:actions) { <<-BLOCK }
|
||||
proc do
|
||||
on_success { :success }
|
||||
on_failure { :failure }
|
||||
end
|
||||
BLOCK
|
||||
|
||||
it "runs successfully" do
|
||||
expect(runner).to eq :success
|
||||
end
|
||||
end
|
||||
|
||||
context "when failing to acquire a lock" do
|
||||
let(:service) { LockService }
|
||||
let(:dependencies) { { params: { post_id: 123, user_id: 456 } } }
|
||||
let(:actions) { <<-BLOCK }
|
||||
proc do
|
||||
on_success { :success }
|
||||
on_failure { :failure }
|
||||
end
|
||||
BLOCK
|
||||
|
||||
it "fails the service" do
|
||||
DistributedMutex.stubs(:synchronize).returns
|
||||
expect(runner).to eq :failure
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user