discourse/spec/jobs/scheduled/directory_refresh_spec.rb
Penar Musaraj 1f8af748eb
DEV: refresh user directory more often for sites with few users (#39852)
The main functional change is that DirectoryRefresh will now run every
hour on sites with less than 100 users (configurable). This is to ensure
activity is up to date more quickly on the directory for new sites.

This also consolidates the two jobs into one, easier to reason about.
2026-05-08 12:04:03 -04:00

69 lines
2.4 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Jobs::DirectoryRefresh do
before { Discourse.redis.del(described_class::OLDER_PERIODS_REFRESH_KEY) }
it "always refreshes the daily period" do
DirectoryItem.stubs(:refresh_period!)
DirectoryItem.expects(:refresh_period!).with(:daily).once
described_class.new.execute({})
end
context "when older periods are due for refresh" do
it "refreshes all non-daily periods" do
older_periods = DirectoryItem.period_types.keys - [:daily]
older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).once }
DirectoryItem.stubs(:refresh_period!).with(:daily)
described_class.new.execute({})
end
it "records the refresh time in Redis" do
DirectoryItem.stubs(:refresh_period!)
freeze_time do
described_class.new.execute({})
expect(Discourse.redis.get(described_class::OLDER_PERIODS_REFRESH_KEY).to_i).to eq(
Time.zone.now.to_i,
)
end
end
end
context "when older periods were recently refreshed" do
before { Discourse.redis.set(described_class::OLDER_PERIODS_REFRESH_KEY, 1.hour.ago.to_i) }
it "does not refresh older periods for a large site" do
SiteSetting.directory_hourly_refresh_max_users = 0
DirectoryItem.stubs(:refresh_period!).with(:daily)
older_periods = DirectoryItem.period_types.keys - [:daily]
older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).never }
described_class.new.execute({})
end
context "when user count is within the limit" do
fab!(:user)
before { SiteSetting.directory_hourly_refresh_max_users = 5 }
it "refreshes older periods for a small site" do
older_periods = DirectoryItem.period_types.keys - [:daily]
older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).once }
DirectoryItem.stubs(:refresh_period!).with(:daily)
described_class.new.execute({})
end
end
context "when user count exceeds the limit" do
fab!(:user_1, :user)
fab!(:user_2, :user)
before { SiteSetting.directory_hourly_refresh_max_users = 1 }
it "does not refresh older periods" do
DirectoryItem.stubs(:refresh_period!).with(:daily)
older_periods = DirectoryItem.period_types.keys - [:daily]
older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).never }
described_class.new.execute({})
end
end
end
end