2023-02-22 00:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-08-09 21:11:50 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Scheduler::AccountsStatusesCleanupScheduler do
|
|
|
|
subject { described_class.new }
|
|
|
|
|
2023-06-14 14:44:37 +00:00
|
|
|
let!(:account_alice) { Fabricate(:account, domain: nil, username: 'alice') }
|
|
|
|
let!(:account_bob) { Fabricate(:account, domain: nil, username: 'bob') }
|
|
|
|
let!(:account_chris) { Fabricate(:account, domain: nil, username: 'chris') }
|
|
|
|
let!(:account_dave) { Fabricate(:account, domain: nil, username: 'dave') }
|
|
|
|
let!(:account_erin) { Fabricate(:account, domain: nil, username: 'erin') }
|
|
|
|
let!(:remote) { Fabricate(:account) }
|
2021-08-09 21:11:50 +00:00
|
|
|
|
|
|
|
let(:queue_size) { 0 }
|
|
|
|
let(:queue_latency) { 0 }
|
|
|
|
let(:process_set_stub) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'concurrency' => 2,
|
2023-02-20 05:14:10 +00:00
|
|
|
'queues' => %w(push default),
|
2021-08-09 21:11:50 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2023-06-14 07:56:11 +00:00
|
|
|
queue_stub = instance_double(Sidekiq::Queue, size: queue_size, latency: queue_latency)
|
2021-08-09 21:11:50 +00:00
|
|
|
allow(Sidekiq::Queue).to receive(:new).and_return(queue_stub)
|
|
|
|
allow(Sidekiq::ProcessSet).to receive(:new).and_return(process_set_stub)
|
|
|
|
|
2023-06-14 07:56:11 +00:00
|
|
|
sidekiq_stats_stub = instance_double(Sidekiq::Stats)
|
2021-08-09 21:11:50 +00:00
|
|
|
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_stub)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#under_load?' do
|
|
|
|
context 'when nothing is queued' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(subject.under_load?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when numerous jobs are queued' do
|
|
|
|
let(:queue_size) { 5 }
|
|
|
|
let(:queue_latency) { 120 }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(subject.under_load?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-03 08:31:40 +00:00
|
|
|
describe '#compute_budget' do
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'with a single thread' do
|
2023-02-20 05:14:10 +00:00
|
|
|
let(:process_set_stub) { [{ 'concurrency' => 1, 'queues' => %w(push default) }] }
|
2021-08-09 21:11:50 +00:00
|
|
|
|
|
|
|
it 'returns a low value' do
|
|
|
|
expect(subject.compute_budget).to be < 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'with a lot of threads' do
|
2021-08-09 21:11:50 +00:00
|
|
|
let(:process_set_stub) do
|
|
|
|
[
|
2023-02-20 05:14:10 +00:00
|
|
|
{ 'concurrency' => 2, 'queues' => %w(push default) },
|
2021-08-09 21:11:50 +00:00
|
|
|
{ 'concurrency' => 2, 'queues' => ['push'] },
|
|
|
|
{ 'concurrency' => 2, 'queues' => ['push'] },
|
|
|
|
{ 'concurrency' => 2, 'queues' => ['push'] },
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a larger value' do
|
|
|
|
expect(subject.compute_budget).to be > 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#perform' do
|
2023-06-20 16:54:05 +00:00
|
|
|
around do |example|
|
|
|
|
Timeout.timeout(30) do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-14 07:56:11 +00:00
|
|
|
before do
|
|
|
|
# Policies for the accounts
|
2023-06-14 14:44:37 +00:00
|
|
|
Fabricate(:account_statuses_cleanup_policy, account: account_alice)
|
|
|
|
Fabricate(:account_statuses_cleanup_policy, account: account_chris)
|
|
|
|
Fabricate(:account_statuses_cleanup_policy, account: account_dave, enabled: false)
|
|
|
|
Fabricate(:account_statuses_cleanup_policy, account: account_erin)
|
2023-06-14 07:56:11 +00:00
|
|
|
|
|
|
|
# Create a bunch of old statuses
|
|
|
|
4.times do
|
2023-06-14 14:44:37 +00:00
|
|
|
Fabricate(:status, account: account_alice, created_at: 3.years.ago)
|
|
|
|
Fabricate(:status, account: account_bob, created_at: 3.years.ago)
|
|
|
|
Fabricate(:status, account: account_chris, created_at: 3.years.ago)
|
|
|
|
Fabricate(:status, account: account_dave, created_at: 3.years.ago)
|
|
|
|
Fabricate(:status, account: account_erin, created_at: 3.years.ago)
|
2023-06-14 07:56:11 +00:00
|
|
|
Fabricate(:status, account: remote, created_at: 3.years.ago)
|
2021-08-09 21:11:50 +00:00
|
|
|
end
|
|
|
|
|
2023-06-14 07:56:11 +00:00
|
|
|
# Create a bunch of newer statuses
|
2023-06-14 14:44:37 +00:00
|
|
|
Fabricate(:status, account: account_alice, created_at: 3.minutes.ago)
|
|
|
|
Fabricate(:status, account: account_bob, created_at: 3.minutes.ago)
|
|
|
|
Fabricate(:status, account: account_chris, created_at: 3.minutes.ago)
|
|
|
|
Fabricate(:status, account: account_dave, created_at: 3.minutes.ago)
|
2023-06-14 07:56:11 +00:00
|
|
|
Fabricate(:status, account: remote, created_at: 3.minutes.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the budget is lower than the number of toots to delete' do
|
|
|
|
it 'deletes the appropriate statuses' do
|
|
|
|
expect(Status.count).to be > (subject.compute_budget) # Data check
|
2021-08-09 21:11:50 +00:00
|
|
|
|
2023-06-14 07:56:11 +00:00
|
|
|
expect { subject.perform }
|
|
|
|
.to change(Status, :count).by(-subject.compute_budget) # Cleanable statuses
|
2023-06-14 14:44:37 +00:00
|
|
|
.and (not_change { account_bob.statuses.count }) # No cleanup policy for account
|
|
|
|
.and(not_change { account_dave.statuses.count }) # Disabled cleanup policy
|
2021-08-09 21:11:50 +00:00
|
|
|
end
|
|
|
|
|
2023-04-23 20:25:40 +00:00
|
|
|
it 'eventually deletes every deletable toot given enough runs' do
|
|
|
|
stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 4
|
|
|
|
|
2023-06-14 07:56:11 +00:00
|
|
|
expect { 3.times { subject.perform } }.to change(Status, :count).by(-cleanable_statuses_count)
|
2023-04-23 20:25:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly round-trips between users across several runs' do
|
|
|
|
stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 3
|
|
|
|
stub_const 'Scheduler::AccountsStatusesCleanupScheduler::PER_ACCOUNT_BUDGET', 2
|
|
|
|
|
|
|
|
expect { 3.times { subject.perform } }
|
2023-05-24 09:23:40 +00:00
|
|
|
.to change(Status, :count).by(-3 * 3)
|
2023-06-14 14:44:37 +00:00
|
|
|
.and change { account_alice.statuses.count }
|
|
|
|
.and change { account_chris.statuses.count }
|
|
|
|
.and(change { account_erin.statuses.count })
|
2021-08-09 21:11:50 +00:00
|
|
|
end
|
2023-05-03 08:31:40 +00:00
|
|
|
|
|
|
|
context 'when given a big budget' do
|
|
|
|
let(:process_set_stub) { [{ 'concurrency' => 400, 'queues' => %w(push default) }] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 400
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly handles looping in a single run' do
|
|
|
|
expect(subject.compute_budget).to eq(400)
|
2023-06-14 07:56:11 +00:00
|
|
|
expect { subject.perform }.to change(Status, :count).by(-cleanable_statuses_count)
|
2023-05-03 08:31:40 +00:00
|
|
|
end
|
|
|
|
end
|
2023-05-04 08:13:00 +00:00
|
|
|
|
|
|
|
context 'when there is no work to be done' do
|
|
|
|
let(:process_set_stub) { [{ 'concurrency' => 400, 'queues' => %w(push default) }] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 400
|
|
|
|
subject.perform
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not get stuck' do
|
|
|
|
expect(subject.compute_budget).to eq(400)
|
2023-05-24 09:23:40 +00:00
|
|
|
expect { subject.perform }.to_not change(Status, :count)
|
2023-05-04 08:13:00 +00:00
|
|
|
end
|
|
|
|
end
|
2023-06-14 07:56:11 +00:00
|
|
|
|
|
|
|
def cleanable_statuses_count
|
|
|
|
Status
|
2023-06-14 14:44:37 +00:00
|
|
|
.where(account_id: [account_alice, account_chris, account_erin]) # Accounts with enabled policies
|
2023-06-14 07:56:11 +00:00
|
|
|
.where('created_at < ?', 2.weeks.ago) # Policy defaults is 2.weeks
|
|
|
|
.count
|
|
|
|
end
|
2021-08-09 21:11:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|