Commit 8ac305f5 authored by Sean McGivern's avatar Sean McGivern

Add specs for Gitlab::SidekiqConfig::CliMethods

By using `fast_spec_helper`, we also do a better job of testing that
these methods don't expect un-required files to be
available. `.worker_queues` is tested here and in the specs for
Gitlab::SidekiqConfig, as in the latter it's using the actual YAML
files, rather than stubbed ones as added here.
parent 4bfd95f6
......@@ -37,6 +37,12 @@ module Gitlab
[queue, *queues_set.grep(/\A#{queue}:/)]
end
end
def clear_memoization!
if instance_variable_defined?('@worker_queues')
remove_instance_variable('@worker_queues')
end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
end
end
......
# 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)
stub_contents(['queue_a'], ['queue_b'])
end
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
expect(described_class.worker_queues(dummy_root))
.to contain_exactly('queue_a', 'queue_b')
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
......@@ -24,27 +24,6 @@ describe Gitlab::SidekiqConfig do
end
end
describe '.expand_queues' do
it 'expands queue namespaces to concrete queue names' do
queues = described_class.expand_queues(%w[cronjob])
expect(queues).to include('cronjob:stuck_import_jobs')
expect(queues).to include('cronjob:stuck_merge_jobs')
end
it 'lets concrete queue names pass through' do
queues = described_class.expand_queues(%w[post_receive])
expect(queues).to include('post_receive')
end
it 'lets unknown queues pass through' do
queues = described_class.expand_queues(%w[unknown])
expect(queues).to include('unknown')
end
end
describe '.workers_for_all_queues_yml' do
it 'returns a tuple with FOSS workers first' do
expect(described_class.workers_for_all_queues_yml.first)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment