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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.8 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
2022-07-23 23:45:48 +05:30
let(:payload) { { recorded_at: Time.current.rfc3339 } }
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) }
2022-07-23 23:45:48 +05:30
allow_next_instance_of(ServicePing::BuildPayload) do |service|
allow(service).to receive(:execute).and_return(payload)
end
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
2022-08-13 15:12:31 +05:30
it 'delegates to ServicePing::SubmitService' do
expect_next_instance_of(ServicePing::SubmitService, payload: payload) do |service|
expect(service).to receive(:execute)
2022-07-23 23:45:48 +05:30
end
2022-08-13 15:12:31 +05:30
subject.perform
2022-07-23 23:45:48 +05:30
end
context 'payload computation' do
it 'creates RawUsageData entry when there is NO entry with the same recorded_at timestamp' do
expect { subject.perform }.to change { RawUsageData.count }.by(1)
end
it 'updates RawUsageData entry when there is entry with the same recorded_at timestamp' do
record = create(:raw_usage_data, payload: { some_metric: 123 }, recorded_at: payload[:recorded_at])
expect { subject.perform }.to change { record.reload.payload }
.from("some_metric" => 123).to(payload.stringify_keys)
end
it 'reports errors and continue on execution' do
error = StandardError.new('some error')
allow(::ServicePing::BuildPayload).to receive(:new).and_raise(error)
expect(::Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(error)
expect_next_instance_of(::ServicePing::SubmitService, payload: nil) do |service|
expect(service).to receive(:execute)
end
subject.perform
end
2020-10-24 23:57:45 +05:30
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