summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-12-04 16:58:44 +0100
committerBob Van Landuyt <bob@vanlanduyt.co>2017-12-04 17:43:48 +0100
commit3d4ba90c5007cca3e8619afe8f40675962a9bc73 (patch)
treec7964fda4b6b8b31fd0bacfa0b0fd2649328e40a
parent20f78421c82d626a3b43924146b7878fe4777ae8 (diff)
downloadgitlab-ce-bvl-limit-fork-queries-on-project-show.tar.gz
Count occurrences of a specific query in the query recorder.bvl-limit-fork-queries-on-project-show
-rw-r--r--spec/controllers/projects_controller_spec.rb10
-rw-r--r--spec/support/query_recorder.rb19
2 files changed, 18 insertions, 11 deletions
diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb
index 39ee5e8691b..e4b2bbb7c51 100644
--- a/spec/controllers/projects_controller_spec.rb
+++ b/spec/controllers/projects_controller_spec.rb
@@ -264,24 +264,22 @@ describe ProjectsController do
context 'when the project is forked and has a repository', :request_store do
let(:public_project) { create(:project, :public, :repository) }
+ let(:other_user) { create(:user) }
render_views
before do
# View the project as a user that does not have any rights
- sign_in(create(:user))
+ sign_in(other_user)
fork_project(public_project)
end
it 'does not increase the number of queries when the project is forked' do
- # When a project is part of a fork network, we check if the `current_user`
- # has a fork in their own namespace. We query this several times. Caching
- # the result in the RequestStore brings the number of queries for this
- # request down from 64 to 59.
+ expected_query = /#{public_project.fork_network.find_forks_in(other_user.namespace).to_sql}/
expect { get(:show, namespace_id: public_project.namespace, id: public_project) }
- .not_to exceed_query_limit(59)
+ .not_to exceed_query_limit(1).for_query(expected_query)
end
end
end
diff --git a/spec/support/query_recorder.rb b/spec/support/query_recorder.rb
index 369775db462..8cf8f45a8b2 100644
--- a/spec/support/query_recorder.rb
+++ b/spec/support/query_recorder.rb
@@ -41,7 +41,8 @@ RSpec::Matchers.define :exceed_query_limit do |expected|
supports_block_expectations
match do |block|
- query_count(&block) > expected_count + threshold
+ @subject_block = block
+ actual_count > expected_count + threshold
end
failure_message_when_negated do |actual|
@@ -55,6 +56,11 @@ RSpec::Matchers.define :exceed_query_limit do |expected|
self
end
+ def for_query(query)
+ @query = query
+ self
+ end
+
def threshold
@threshold.to_i
end
@@ -68,12 +74,15 @@ RSpec::Matchers.define :exceed_query_limit do |expected|
end
def actual_count
- @recorder.count
+ @actual_count ||= if @query
+ recorder.log.select { |recorded| recorded =~ @query }.size
+ else
+ recorder.count
+ end
end
- def query_count(&block)
- @recorder = ActiveRecord::QueryRecorder.new(&block)
- @recorder.count
+ def recorder
+ @recorder ||= ActiveRecord::QueryRecorder.new(&@subject_block)
end
def count_queries(queries)