accounts_statuses_cleanup_scheduler_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Scheduler::AccountsStatusesCleanupScheduler do
  4. subject { described_class.new }
  5. let!(:account_alice) { Fabricate(:account, domain: nil, username: 'alice') }
  6. let!(:account_bob) { Fabricate(:account, domain: nil, username: 'bob') }
  7. let!(:account_chris) { Fabricate(:account, domain: nil, username: 'chris') }
  8. let!(:account_dave) { Fabricate(:account, domain: nil, username: 'dave') }
  9. let!(:account_erin) { Fabricate(:account, domain: nil, username: 'erin') }
  10. let!(:remote) { Fabricate(:account) }
  11. let(:queue_size) { 0 }
  12. let(:queue_latency) { 0 }
  13. let(:process_set_stub) do
  14. [
  15. {
  16. 'concurrency' => 2,
  17. 'queues' => %w(push default),
  18. },
  19. ]
  20. end
  21. before do
  22. queue_stub = instance_double(Sidekiq::Queue, size: queue_size, latency: queue_latency)
  23. allow(Sidekiq::Queue).to receive(:new).and_return(queue_stub)
  24. allow(Sidekiq::ProcessSet).to receive(:new).and_return(process_set_stub)
  25. sidekiq_stats_stub = instance_double(Sidekiq::Stats)
  26. allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_stub)
  27. end
  28. describe '#under_load?' do
  29. context 'when nothing is queued' do
  30. it 'returns false' do
  31. expect(subject.under_load?).to be false
  32. end
  33. end
  34. context 'when numerous jobs are queued' do
  35. let(:queue_size) { 5 }
  36. let(:queue_latency) { 120 }
  37. it 'returns true' do
  38. expect(subject.under_load?).to be true
  39. end
  40. end
  41. end
  42. describe '#compute_budget' do
  43. context 'with a single thread' do
  44. let(:process_set_stub) { [{ 'concurrency' => 1, 'queues' => %w(push default) }] }
  45. it 'returns a low value' do
  46. expect(subject.compute_budget).to be < 10
  47. end
  48. end
  49. context 'with a lot of threads' do
  50. let(:process_set_stub) do
  51. [
  52. { 'concurrency' => 2, 'queues' => %w(push default) },
  53. { 'concurrency' => 2, 'queues' => ['push'] },
  54. { 'concurrency' => 2, 'queues' => ['push'] },
  55. { 'concurrency' => 2, 'queues' => ['push'] },
  56. ]
  57. end
  58. it 'returns a larger value' do
  59. expect(subject.compute_budget).to be > 10
  60. end
  61. end
  62. end
  63. describe '#perform' do
  64. around do |example|
  65. Timeout.timeout(30) do
  66. example.run
  67. end
  68. end
  69. before do
  70. # Policies for the accounts
  71. Fabricate(:account_statuses_cleanup_policy, account: account_alice)
  72. Fabricate(:account_statuses_cleanup_policy, account: account_chris)
  73. Fabricate(:account_statuses_cleanup_policy, account: account_dave, enabled: false)
  74. Fabricate(:account_statuses_cleanup_policy, account: account_erin)
  75. # Create a bunch of old statuses
  76. 4.times do
  77. Fabricate(:status, account: account_alice, created_at: 3.years.ago)
  78. Fabricate(:status, account: account_bob, created_at: 3.years.ago)
  79. Fabricate(:status, account: account_chris, created_at: 3.years.ago)
  80. Fabricate(:status, account: account_dave, created_at: 3.years.ago)
  81. Fabricate(:status, account: account_erin, created_at: 3.years.ago)
  82. Fabricate(:status, account: remote, created_at: 3.years.ago)
  83. end
  84. # Create a bunch of newer statuses
  85. Fabricate(:status, account: account_alice, created_at: 3.minutes.ago)
  86. Fabricate(:status, account: account_bob, created_at: 3.minutes.ago)
  87. Fabricate(:status, account: account_chris, created_at: 3.minutes.ago)
  88. Fabricate(:status, account: account_dave, created_at: 3.minutes.ago)
  89. Fabricate(:status, account: remote, created_at: 3.minutes.ago)
  90. end
  91. context 'when the budget is lower than the number of toots to delete' do
  92. it 'deletes the appropriate statuses' do
  93. expect(Status.count).to be > (subject.compute_budget) # Data check
  94. expect { subject.perform }
  95. .to change(Status, :count).by(-subject.compute_budget) # Cleanable statuses
  96. .and (not_change { account_bob.statuses.count }) # No cleanup policy for account
  97. .and(not_change { account_dave.statuses.count }) # Disabled cleanup policy
  98. end
  99. it 'eventually deletes every deletable toot given enough runs' do
  100. stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 4
  101. expect { 3.times { subject.perform } }.to change(Status, :count).by(-cleanable_statuses_count)
  102. end
  103. it 'correctly round-trips between users across several runs' do
  104. stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 3
  105. stub_const 'Scheduler::AccountsStatusesCleanupScheduler::PER_ACCOUNT_BUDGET', 2
  106. expect { 3.times { subject.perform } }
  107. .to change(Status, :count).by(-3 * 3)
  108. .and change { account_alice.statuses.count }
  109. .and change { account_chris.statuses.count }
  110. .and(change { account_erin.statuses.count })
  111. end
  112. context 'when given a big budget' do
  113. let(:process_set_stub) { [{ 'concurrency' => 400, 'queues' => %w(push default) }] }
  114. before do
  115. stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 400
  116. end
  117. it 'correctly handles looping in a single run' do
  118. expect(subject.compute_budget).to eq(400)
  119. expect { subject.perform }.to change(Status, :count).by(-cleanable_statuses_count)
  120. end
  121. end
  122. context 'when there is no work to be done' do
  123. let(:process_set_stub) { [{ 'concurrency' => 400, 'queues' => %w(push default) }] }
  124. before do
  125. stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 400
  126. subject.perform
  127. end
  128. it 'does not get stuck' do
  129. expect(subject.compute_budget).to eq(400)
  130. expect { subject.perform }.to_not change(Status, :count)
  131. end
  132. end
  133. def cleanable_statuses_count
  134. Status
  135. .where(account_id: [account_alice, account_chris, account_erin]) # Accounts with enabled policies
  136. .where(created_at: ...2.weeks.ago) # Policy defaults is 2.weeks
  137. .count
  138. end
  139. end
  140. end
  141. end