Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/banzai/filter/references')
-rw-r--r--spec/lib/banzai/filter/references/label_reference_filter_spec.rb68
-rw-r--r--spec/lib/banzai/filter/references/reference_cache_spec.rb7
-rw-r--r--spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb4
3 files changed, 75 insertions, 4 deletions
diff --git a/spec/lib/banzai/filter/references/label_reference_filter_spec.rb b/spec/lib/banzai/filter/references/label_reference_filter_spec.rb
index db7dda96cad..b18d68c8dd4 100644
--- a/spec/lib/banzai/filter/references/label_reference_filter_spec.rb
+++ b/spec/lib/banzai/filter/references/label_reference_filter_spec.rb
@@ -702,4 +702,72 @@ RSpec.describe Banzai::Filter::References::LabelReferenceFilter do
expect(result.css('a').first.text).to eq "#{label.name} in #{project.full_name}"
end
end
+
+ context 'checking N+1' do
+ let_it_be(:group) { create(:group) }
+ let_it_be(:group2) { create(:group) }
+ let_it_be(:project) { create(:project, :public, namespace: group) }
+ let_it_be(:project2) { create(:project, :public, namespace: group2) }
+ let_it_be(:project3) { create(:project, :public) }
+ let_it_be(:project_label) { create(:label, project: project) }
+ let_it_be(:project_label2) { create(:label, project: project) }
+ let_it_be(:project2_label) { create(:label, project: project2) }
+ let_it_be(:group2_label) { create(:group_label, group: group2, color: '#00ff00') }
+ let_it_be(:project_reference) { "#{project_label.to_reference}" }
+ let_it_be(:project_reference2) { "#{project_label2.to_reference}" }
+ let_it_be(:project2_reference) { "#{project2_label.to_reference}" }
+ let_it_be(:group2_reference) { "#{project2.full_path}~#{group2_label.name}" }
+
+ it 'does not have N+1 per multiple references per project', :use_sql_query_cache do
+ markdown = "#{project_reference}"
+ control_count = 1
+
+ expect do
+ reference_filter(markdown)
+ end.not_to exceed_all_query_limit(control_count)
+
+ markdown = "#{project_reference} ~qwert ~werty ~ertyu ~rtyui #{project_reference2}"
+
+ expect do
+ reference_filter(markdown)
+ end.not_to exceed_all_query_limit(control_count)
+ end
+
+ it 'has N+1 for multiple unique project/group references', :use_sql_query_cache do
+ # reference to already loaded project, only one query
+ markdown = "#{project_reference}"
+ control_count = 1
+
+ expect do
+ reference_filter(markdown, project: project)
+ end.not_to exceed_all_query_limit(control_count)
+
+ # Since we're not batching label queries across projects/groups,
+ # queries increase when a new project/group is added.
+ # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359
+ # first reference to already loaded project (1),
+ # second reference requires project and namespace (2), and label (1)
+ markdown = "#{project_reference} #{group2_reference}"
+ max_count = control_count + 3
+
+ expect do
+ reference_filter(markdown)
+ end.not_to exceed_all_query_limit(max_count)
+
+ # third reference to already queried project/namespace, nothing extra (no N+1 here)
+ markdown = "#{project_reference} #{group2_reference} #{project2_reference}"
+
+ expect do
+ reference_filter(markdown)
+ end.not_to exceed_all_query_limit(max_count)
+
+ # last reference needs another namespace and label query (2)
+ markdown = "#{project_reference} #{group2_reference} #{project2_reference} #{project3.full_path}~test_label"
+ max_count += 2
+
+ expect do
+ reference_filter(markdown)
+ end.not_to exceed_all_query_limit(max_count)
+ end
+ end
end
diff --git a/spec/lib/banzai/filter/references/reference_cache_spec.rb b/spec/lib/banzai/filter/references/reference_cache_spec.rb
index 9e2a6f35910..c9404c381d3 100644
--- a/spec/lib/banzai/filter/references/reference_cache_spec.rb
+++ b/spec/lib/banzai/filter/references/reference_cache_spec.rb
@@ -55,11 +55,12 @@ RSpec.describe Banzai::Filter::References::ReferenceCache do
cache_single.load_records_per_parent
end.count
+ expect(control_count).to eq 1
+
# Since this is an issue filter that is not batching issue queries
# across projects, we have to account for that.
- # 1 for both projects, 1 for issues in each project == 3
- # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359
- max_count = control_count + 1
+ # 1 for original issue, 2 for second route/project, 1 for other issue
+ max_count = control_count + 3
expect do
cache.load_references_per_parent(filter.nodes)
diff --git a/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb b/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb
index 7ab3b24b1c2..2e324669870 100644
--- a/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb
+++ b/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb
@@ -233,13 +233,15 @@ RSpec.describe Banzai::Filter::References::SnippetReferenceFilter do
reference_filter(markdown)
end.count
+ expect(control_count).to eq 1
+
markdown = "#{reference} $9999990 $9999991 $9999992 $9999993 #{reference2} something/cool$12"
# Since we're not batching snippet queries across projects,
# we have to account for that.
# 1 for both projects, 1 for snippets in each project == 3
# TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359
- max_count = control_count + 1
+ max_count = control_count + 2
expect do
reference_filter(markdown)