summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/gitlab/looping_batcher_spec.rb71
-rw-r--r--spec/models/ci/pipeline_spec.rb4
-rw-r--r--spec/workers/expire_pipeline_cache_worker_spec.rb15
3 files changed, 87 insertions, 3 deletions
diff --git a/spec/lib/gitlab/looping_batcher_spec.rb b/spec/lib/gitlab/looping_batcher_spec.rb
new file mode 100644
index 00000000000..b03e969c1e7
--- /dev/null
+++ b/spec/lib/gitlab/looping_batcher_spec.rb
@@ -0,0 +1,71 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::LoopingBatcher, :use_clean_rails_memory_store_caching do
+ describe '#next_range!' do
+ let(:model_class) { LfsObject }
+ let(:key) { 'looping_batcher_spec' }
+ let(:batch_size) { 2 }
+
+ subject { described_class.new(model_class, key: key, batch_size: batch_size).next_range! }
+
+ context 'when there are no records' do
+ it { is_expected.to be_nil }
+ end
+
+ context 'when there are records' do
+ let!(:records) { create_list(model_class.underscore, 3) }
+
+ context 'when it has never been called before' do
+ it { is_expected.to be_a Range }
+
+ it 'starts from the beginning' do
+ expect(subject.first).to eq(1)
+ end
+
+ it 'ends at a full batch' do
+ expect(subject.last).to eq(records.second.id)
+ end
+
+ context 'when the batch size is greater than the number of records' do
+ let(:batch_size) { 5 }
+
+ it 'ends at the last ID' do
+ expect(subject.last).to eq(records.last.id)
+ end
+ end
+ end
+
+ context 'when it was called before' do
+ context 'when the previous batch included the end of the table' do
+ before do
+ described_class.new(model_class, key: key, batch_size: model_class.count).next_range!
+ end
+
+ it 'starts from the beginning' do
+ expect(subject).to eq(1..records.second.id)
+ end
+ end
+
+ context 'when the previous batch did not include the end of the table' do
+ before do
+ described_class.new(model_class, key: key, batch_size: model_class.count - 1).next_range!
+ end
+
+ it 'starts after the previous batch' do
+ expect(subject).to eq(records.last.id..records.last.id)
+ end
+ end
+
+ context 'if cache is cleared' do
+ it 'starts from the beginning' do
+ Rails.cache.clear
+
+ expect(subject).to eq(1..records.second.id)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index 1515da7eeca..86c3628216e 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -1117,6 +1117,10 @@ describe Ci::Pipeline, :mailer do
end
describe 'pipeline caching' do
+ before do
+ pipeline.config_source = 'repository_source'
+ end
+
it 'performs ExpirePipelinesCacheWorker' do
expect(ExpirePipelineCacheWorker).to receive(:perform_async).with(pipeline.id)
diff --git a/spec/workers/expire_pipeline_cache_worker_spec.rb b/spec/workers/expire_pipeline_cache_worker_spec.rb
index e162a227a66..8d898ffc13e 100644
--- a/spec/workers/expire_pipeline_cache_worker_spec.rb
+++ b/spec/workers/expire_pipeline_cache_worker_spec.rb
@@ -3,9 +3,9 @@
require 'spec_helper'
describe ExpirePipelineCacheWorker do
- let(:user) { create(:user) }
- let(:project) { create(:project) }
- let(:pipeline) { create(:ci_pipeline, project: project) }
+ let_it_be(:user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+ let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
subject { described_class.new }
@@ -22,5 +22,14 @@ describe ExpirePipelineCacheWorker do
subject.perform(617748)
end
+
+ it "doesn't do anything if the pipeline cannot be cached" do
+ allow_any_instance_of(Ci::Pipeline).to receive(:cacheable?).and_return(false)
+
+ expect_any_instance_of(Ci::ExpirePipelineCacheService).not_to receive(:execute)
+ expect_any_instance_of(Gitlab::EtagCaching::Store).not_to receive(:touch)
+
+ subject.perform(pipeline.id)
+ end
end
end