Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec/requests/homepage_inbox_n_plus_one_spec.rb
Original file line number Diff line number Diff line change
@@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
create_list(:inbox_comment, n)
create_list(:inbox_comment, n, user: user)

also for the other create_list, otherwise this doesn't have any comments on the homepage for the user

To make sure the test fails in the future when it's not displaying more than one comment, could you add an expect for the number of comments after the subject.call, similar to how it's done in the work tests with expect(response.body.scan('<li id="work_').size).to eq(current_scale.to_i) ?

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.
Comment on lines +40 to +42

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is the case for work blurbs, it's not the case for homepage inbox comments which aren't cached like work blurbs are. There are still a number of N+1 queries that can be avoided here and that we'd indeed like to avoid. You can refer to the very similar case of #5890 for some pointers of what queries to avoid (e.g. the role queries also show up in my logs for the homepage) and what kind of variants would be good to cover in the test

expect { subject.call }
.to perform_linear_number_of_queries(slope: 9).with_warming_up
end
end
end
end
end
Loading