1
0
مراية لـ https://github.com/postalserver/postal.git تم المزامنة 2025-12-01 05:43:04 +00:00
الملفات
postal/spec/models/worker_role_spec.rb
2024-02-23 22:51:34 +00:00

59 أسطر
1.8 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
RSpec.describe WorkerRole do
let(:locker_name) { "test" }
before do
allow(Postal).to receive(:locker_name).and_return(locker_name)
end
describe ".acquire" do
context "when there are no existing roles" do
it "returns :created" do
expect(WorkerRole.acquire("test")).to eq(:created)
end
end
context "when the current process holds a lock for a role" do
it "returns :renewed" do
create(:worker_role, role: "test", worker: "test", acquired_at: 1.minute.ago)
expect(WorkerRole.acquire("test")).to eq(:renewed)
end
end
context "when the role has become stale" do
it "returns :stolen" do
create(:worker_role, role: "test", worker: "another", acquired_at: 10.minute.ago)
expect(WorkerRole.acquire("test")).to eq(:stolen)
end
end
context "when the role is already locked by another worker" do
it "returns false" do
create(:worker_role, role: "test", worker: "another", acquired_at: 1.minute.ago)
expect(WorkerRole.acquire("test")).to eq(false)
end
end
end
describe ".release" do
context "when the role is locked by the current worker" do
it "deletes the role and returns true" do
role = create(:worker_role, role: "test", worker: "test")
expect(WorkerRole.release("test")).to eq(true)
expect(WorkerRole.find_by(id: role.id)).to be_nil
end
end
context "when the role is locked by another worker" do
it "does not delete the role and returns false" do
role = create(:worker_role, role: "test", worker: "another")
expect(WorkerRole.release("test")).to eq(false)
expect(WorkerRole.find_by(id: role.id)).to be_present
end
end
end
end