debian-mirror-gitlab/spec/models/namespace/aggregation_schedule_spec.rb

96 lines
3 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespace::AggregationSchedule, :clean_gitlab_redis_shared_state, type: :model do
include ExclusiveLeaseHelpers
2023-07-09 08:55:56 +05:30
let(:namespace) { create(:namespace) }
let(:aggregation_schedule) { namespace.build_aggregation_schedule }
let(:default_timeout) { aggregation_schedule.default_lease_timeout }
2022-11-25 23:54:43 +05:30
2019-09-30 21:07:59 +05:30
it { is_expected.to belong_to :namespace }
2022-11-25 23:54:43 +05:30
describe "#default_lease_timeout" do
2023-07-09 08:55:56 +05:30
before do
aggregation_schedule.save!
end
2022-11-25 23:54:43 +05:30
2023-07-09 08:55:56 +05:30
context 'when reduce_aggregation_schedule_lease FF is enabled' do
it 'is 2 minutes' do
stub_feature_flags(reduce_aggregation_schedule_lease: true)
expect(aggregation_schedule.default_lease_timeout).to eq 2.minutes.to_i
2022-11-25 23:54:43 +05:30
end
2023-07-09 08:55:56 +05:30
end
2022-11-25 23:54:43 +05:30
2023-07-09 08:55:56 +05:30
context 'when reduce_aggregation_schedule_lease FF is disabled' do
it 'is 30 minutes' do
stub_feature_flags(reduce_aggregation_schedule_lease: false)
expect(aggregation_schedule.default_lease_timeout).to eq 30.minutes.to_i
end
2022-11-25 23:54:43 +05:30
end
end
2019-09-30 21:07:59 +05:30
describe '#schedule_root_storage_statistics' do
let(:lease_key) { "namespace:namespaces_root_statistics:#{namespace.id}" }
context "when we can't obtain the lease" do
it 'does not schedule the workers' do
2022-11-25 23:54:43 +05:30
stub_exclusive_lease_taken(lease_key, timeout: default_timeout)
2019-09-30 21:07:59 +05:30
expect(Namespaces::RootStatisticsWorker)
.not_to receive(:perform_async)
expect(Namespaces::RootStatisticsWorker)
.not_to receive(:perform_in)
aggregation_schedule.save!
end
end
context 'when we can obtain the lease' do
it 'schedules a root storage statistics after create' do
2022-11-25 23:54:43 +05:30
stub_exclusive_lease(lease_key, timeout: default_timeout)
2019-09-30 21:07:59 +05:30
expect(Namespaces::RootStatisticsWorker)
.to receive(:perform_async).once
expect(Namespaces::RootStatisticsWorker)
.to receive(:perform_in).once
2022-11-25 23:54:43 +05:30
.with(default_timeout, aggregation_schedule.namespace_id)
2019-09-30 21:07:59 +05:30
aggregation_schedule.save!
end
it 'does not release the lease' do
2022-11-25 23:54:43 +05:30
stub_exclusive_lease(lease_key, timeout: default_timeout)
2019-09-30 21:07:59 +05:30
aggregation_schedule.save!
exclusive_lease = aggregation_schedule.exclusive_lease
expect(exclusive_lease.exists?).to be_truthy
end
it 'only executes the workers once' do
# Avoid automatic deletion of Namespace::AggregationSchedule
# for testing purposes.
expect(Namespaces::RootStatisticsWorker)
.to receive(:perform_async).once
.and_return(nil)
expect(Namespaces::RootStatisticsWorker)
.to receive(:perform_in).once
2022-11-25 23:54:43 +05:30
.with(default_timeout, aggregation_schedule.namespace_id)
2019-09-30 21:07:59 +05:30
.and_return(nil)
# Scheduling workers for the first time
aggregation_schedule.schedule_root_storage_statistics
# Executing again, this time workers should not be scheduled
# due to the lease not been released.
aggregation_schedule.schedule_root_storage_statistics
end
end
end
end