From 294ba9f3d1595fe75e9221556e21b9344d0a0b42 Mon Sep 17 00:00:00 2001 From: ASalbenblatt Date: Sun, 28 Jun 2026 09:48:24 -0600 Subject: [PATCH 1/2] AO3-4412 Adding a test to ensure there are not N+1 issues when loading inbox commets on the homepage --- .../homepage_inbox_n_plus_one_spec.rb | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 spec/requests/homepage_inbox_n_plus_one_spec.rb diff --git a/spec/requests/homepage_inbox_n_plus_one_spec.rb b/spec/requests/homepage_inbox_n_plus_one_spec.rb new file mode 100644 index 00000000000..8642e84357a --- /dev/null +++ b/spec/requests/homepage_inbox_n_plus_one_spec.rb @@ -0,0 +1,49 @@ +require "spec_helper" + +describe "n+1 queries in the inbox controller for the homepage" do + include LoginMacros + + describe "#index", n_plus_one: true do + context "when displaying a user's unread messages on the homepage" do + let!(:user) { create(:user) } + + subject do + proc do + get '/' + end + end + + before do + fake_login_known_user(user) + end + + context "when all works are cached" do + populate do |n| + create_list(:inbox_comment, n) + subject.call # this caches the comments + end + + it "produces a constant number of queries" do + expect { subject.call } + .to perform_constant_number_of_queries + end + end + + context "when nothing is cached" do + populate do |n| + create_list(:inbox_comment, n) + end + + it "performs around 9 queries per message" do + # TODO: Ideally, we'd like the uncached inbox listings to also have a + # constant number of queries, instead of the linear number of queries + # we're checking for here. But we also don't want to put too much + # unnecessary load on the databases when we have a bunch of cache hits, + # so this requires some care & tuning. + expect { subject.call } + .to perform_linear_number_of_queries(slope: 9).with_warming_up + end + end + end + end +end \ No newline at end of file From f50d2d62d0c87b506f6f46ea2b58ba3f1695ca0e Mon Sep 17 00:00:00 2001 From: ASalbenblatt Date: Sun, 28 Jun 2026 18:54:50 -0600 Subject: [PATCH 2/2] AO3-4412 Fixing Rubocop's suggestions --- spec/requests/homepage_inbox_n_plus_one_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/requests/homepage_inbox_n_plus_one_spec.rb b/spec/requests/homepage_inbox_n_plus_one_spec.rb index 8642e84357a..bd974b1edcd 100644 --- a/spec/requests/homepage_inbox_n_plus_one_spec.rb +++ b/spec/requests/homepage_inbox_n_plus_one_spec.rb @@ -9,7 +9,7 @@ subject do proc do - get '/' + get "/" end end @@ -46,4 +46,4 @@ end end end -end \ No newline at end of file +end