debian-mirror-gitlab/spec/workers/gitlab_service_ping_worker_spec.rb

51 lines
1.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2021-09-30 23:02:18 +05:30
RSpec.describe GitlabServicePingWorker, :clean_gitlab_redis_shared_state do
2020-10-24 23:57:45 +05:30
before do
2021-09-30 23:02:18 +05:30
allow_next_instance_of(ServicePing::SubmitService) { |service| allow(service).to receive(:execute) }
2020-10-24 23:57:45 +05:30
allow(subject).to receive(:sleep)
end
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
it 'does not run for GitLab.com' do
allow(Gitlab).to receive(:com?).and_return(true)
2021-09-30 23:02:18 +05:30
expect(ServicePing::SubmitService).not_to receive(:new)
2021-02-22 17:27:13 +05:30
subject.perform
end
2021-09-30 23:02:18 +05:30
it 'delegates to ServicePing::SubmitService' do
expect_next_instance_of(ServicePing::SubmitService) { |service| expect(service).to receive(:execute) }
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
subject.perform
end
it "obtains a #{described_class::LEASE_TIMEOUT} second exclusive lease" do
expect(Gitlab::ExclusiveLeaseHelpers::SleepingLock)
.to receive(:new)
.with(described_class::LEASE_KEY, hash_including(timeout: described_class::LEASE_TIMEOUT))
.and_call_original
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
subject.perform
2017-08-17 22:00:37 +05:30
end
2020-10-24 23:57:45 +05:30
it 'sleeps for between 0 and 60 seconds' do
expect(subject).to receive(:sleep).with(0..60)
subject.perform
end
context 'when lease is not obtained' do
before do
Gitlab::ExclusiveLease.new(described_class::LEASE_KEY, timeout: described_class::LEASE_TIMEOUT).try_obtain
end
2021-09-30 23:02:18 +05:30
it 'does not invoke ServicePing::SubmitService' do
allow_next_instance_of(ServicePing::SubmitService) { |service| expect(service).not_to receive(:execute) }
2020-10-24 23:57:45 +05:30
expect { subject.perform }.to raise_error(Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError)
end
end
2017-08-17 22:00:37 +05:30
end