2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe UserProjectAccessChangedService do
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#execute' do
|
|
|
|
it 'schedules the user IDs' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(AuthorizedProjectsWorker).to receive(:bulk_perform_and_wait)
|
|
|
|
.with([[1], [2]])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
described_class.new([1, 2]).execute
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'permits non-blocking operation' do
|
|
|
|
expect(AuthorizedProjectsWorker).to receive(:bulk_perform_async)
|
|
|
|
.with([[1], [2]])
|
|
|
|
|
|
|
|
described_class.new([1, 2]).execute(blocking: false)
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
it 'permits low-priority operation' do
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(AuthorizedProjectUpdate::UserRefreshFromReplicaWorker).to(
|
2020-06-23 00:09:42 +05:30
|
|
|
receive(:bulk_perform_in).with(
|
|
|
|
described_class::DELAY,
|
|
|
|
[[1], [2]],
|
|
|
|
{ batch_delay: 30.seconds, batch_size: 100 }
|
|
|
|
)
|
2020-05-24 23:13:21 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
described_class.new([1, 2]).execute(blocking: false,
|
|
|
|
priority: described_class::LOW_PRIORITY)
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it 'sets the current caller_id as related_class in the context of all the enqueued jobs' do
|
|
|
|
Gitlab::ApplicationContext.with_context(caller_id: 'Foo') do
|
|
|
|
described_class.new([1, 2]).execute(blocking: false,
|
|
|
|
priority: described_class::LOW_PRIORITY)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(AuthorizedProjectUpdate::UserRefreshFromReplicaWorker.jobs).to all(
|
|
|
|
include(Labkit::Context.log_key(:related_class) => 'Foo')
|
|
|
|
)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
context 'with load balancing enabled' do
|
|
|
|
let(:service) { UserProjectAccessChangedService.new([1, 2]) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
|
|
|
|
|
|
|
|
expect(AuthorizedProjectsWorker).to receive(:bulk_perform_and_wait)
|
|
|
|
.with([[1], [2]])
|
|
|
|
.and_return(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sticks all the updated users and returns the original result', :aggregate_failures do
|
|
|
|
expect(Gitlab::Database::LoadBalancing::Sticking).to receive(:bulk_stick).with(:user, [1, 2])
|
|
|
|
|
|
|
|
expect(service.execute).to eq(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N+1 cached queries', :use_sql_query_cache, :request_store do
|
|
|
|
# Run this once to establish a baseline
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
service = UserProjectAccessChangedService.new([1, 2, 3, 4, 5])
|
|
|
|
|
|
|
|
allow(AuthorizedProjectsWorker).to receive(:bulk_perform_and_wait)
|
|
|
|
.with([[1], [2], [3], [4], [5]])
|
|
|
|
.and_return(10)
|
|
|
|
|
|
|
|
expect { service.execute }.not_to exceed_all_query_limit(control_count.count)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|