diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 18:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 18:08:36 +0300 |
commit | fedf978f9aa1909ed7bb3fad767ad120a1c6bd7b (patch) | |
tree | 1bd0f0b301ad96feda1910abe34eb89c46cc55cd /spec/lib/gitlab/sidekiq_config | |
parent | db24ab2b72dbff24c201410a0561e929ae7e8061 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/sidekiq_config')
-rw-r--r-- | spec/lib/gitlab/sidekiq_config/cli_methods_spec.rb | 116 | ||||
-rw-r--r-- | spec/lib/gitlab/sidekiq_config/worker_spec.rb | 41 |
2 files changed, 150 insertions, 7 deletions
diff --git a/spec/lib/gitlab/sidekiq_config/cli_methods_spec.rb b/spec/lib/gitlab/sidekiq_config/cli_methods_spec.rb new file mode 100644 index 00000000000..60d946db744 --- /dev/null +++ b/spec/lib/gitlab/sidekiq_config/cli_methods_spec.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +describe Gitlab::SidekiqConfig::CliMethods do + let(:dummy_root) { '/tmp/' } + + describe '.worker_queues' do + def expand_path(path) + File.join(dummy_root, path) + end + + def stub_exists(exists: true) + ['app/workers/all_queues.yml', 'ee/app/workers/all_queues.yml'].each do |path| + allow(File).to receive(:exist?).with(expand_path(path)).and_return(exists) + end + end + + def stub_contents(foss_queues, ee_queues) + allow(YAML).to receive(:load_file) + .with(expand_path('app/workers/all_queues.yml')) + .and_return(foss_queues) + + allow(YAML).to receive(:load_file) + .with(expand_path('ee/app/workers/all_queues.yml')) + .and_return(ee_queues) + end + + before do + described_class.clear_memoization! + end + + context 'when the file exists' do + before do + stub_exists(exists: true) + end + + shared_examples 'valid file contents' do + it 'memoizes the result' do + result = described_class.worker_queues(dummy_root) + + stub_exists(exists: false) + + expect(described_class.worker_queues(dummy_root)).to eq(result) + end + + it 'flattens and joins the contents' do + expected_queues = %w[queue_a queue_b] + expected_queues = expected_queues.first(1) unless Gitlab.ee? + + expect(described_class.worker_queues(dummy_root)) + .to match_array(expected_queues) + end + end + + context 'when the file contains an array of strings' do + before do + stub_contents(['queue_a'], ['queue_b']) + end + + include_examples 'valid file contents' + end + + context 'when the file contains an array of hashes' do + before do + stub_contents([{ name: 'queue_a' }], [{ name: 'queue_b' }]) + end + + include_examples 'valid file contents' + end + end + + context 'when the file does not exist' do + before do + stub_exists(exists: false) + end + + it 'returns an empty array' do + expect(described_class.worker_queues(dummy_root)).to be_empty + end + end + end + + describe '.expand_queues' do + let(:all_queues) do + ['cronjob:stuck_import_jobs', 'cronjob:stuck_merge_jobs', 'post_receive'] + end + + it 'defaults the value of the second argument to .worker_queues' do + allow(described_class).to receive(:worker_queues).and_return([]) + + expect(described_class.expand_queues(['cronjob'])) + .to contain_exactly('cronjob') + + allow(described_class).to receive(:worker_queues).and_return(all_queues) + + expect(described_class.expand_queues(['cronjob'])) + .to contain_exactly('cronjob', 'cronjob:stuck_import_jobs', 'cronjob:stuck_merge_jobs') + end + + it 'expands queue namespaces to concrete queue names' do + expect(described_class.expand_queues(['cronjob'], all_queues)) + .to contain_exactly('cronjob', 'cronjob:stuck_import_jobs', 'cronjob:stuck_merge_jobs') + end + + it 'lets concrete queue names pass through' do + expect(described_class.expand_queues(['post_receive'], all_queues)) + .to contain_exactly('post_receive') + end + + it 'lets unknown queues pass through' do + expect(described_class.expand_queues(['unknown'], all_queues)) + .to contain_exactly('unknown') + end + end +end diff --git a/spec/lib/gitlab/sidekiq_config/worker_spec.rb b/spec/lib/gitlab/sidekiq_config/worker_spec.rb index ba6760f38b5..38edd0f5eeb 100644 --- a/spec/lib/gitlab/sidekiq_config/worker_spec.rb +++ b/spec/lib/gitlab/sidekiq_config/worker_spec.rb @@ -3,9 +3,17 @@ require 'fast_spec_helper' describe Gitlab::SidekiqConfig::Worker do - def create_worker(queue:, weight: 0) + def create_worker(queue:, **attributes) namespace = queue.include?(':') && queue.split(':').first - inner_worker = double(queue: queue, queue_namespace: namespace, get_weight: weight) + inner_worker = double( + queue: queue, + queue_namespace: namespace, + get_feature_category: attributes[:feature_category], + get_weight: attributes[:weight], + get_worker_resource_boundary: attributes[:resource_boundary], + latency_sensitive_worker?: attributes[:latency_sensitive], + worker_has_external_dependencies?: attributes[:has_external_dependencies] + ) described_class.new(inner_worker, ee: false) end @@ -75,13 +83,32 @@ describe Gitlab::SidekiqConfig::Worker do end describe 'YAML encoding' do - it 'encodes the worker in YAML as a string of the queue' do - worker_a = create_worker(queue: 'a') - worker_b = create_worker(queue: 'b') + it 'encodes the worker in YAML as a hash of the queue' do + attributes_a = { + feature_category: :source_code_management, + has_external_dependencies: false, + latency_sensitive: false, + resource_boundary: :memory, + weight: 2 + } + + attributes_b = { + feature_category: :not_owned, + has_external_dependencies: true, + latency_sensitive: true, + resource_boundary: :unknown, + weight: 1 + } + + worker_a = create_worker(queue: 'a', **attributes_a) + worker_b = create_worker(queue: 'b', **attributes_b) + + expect(YAML.dump(worker_a)) + .to eq(YAML.dump(attributes_a.reverse_merge(name: 'a'))) - expect(YAML.dump(worker_a)).to eq(YAML.dump('a')) expect(YAML.dump([worker_a, worker_b])) - .to eq(YAML.dump(%w[a b])) + .to eq(YAML.dump([attributes_a.reverse_merge(name: 'a'), + attributes_b.reverse_merge(name: 'b')])) end end |