accounts_statuses_cleanup_scheduler_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rails_helper'
  2. describe Scheduler::AccountsStatusesCleanupScheduler do
  3. subject { described_class.new }
  4. let!(:account1) { Fabricate(:account, domain: nil) }
  5. let!(:account2) { Fabricate(:account, domain: nil) }
  6. let!(:account3) { Fabricate(:account, domain: nil) }
  7. let!(:account4) { Fabricate(:account, domain: nil) }
  8. let!(:remote) { Fabricate(:account) }
  9. let!(:policy1) { Fabricate(:account_statuses_cleanup_policy, account: account1) }
  10. let!(:policy2) { Fabricate(:account_statuses_cleanup_policy, account: account3) }
  11. let!(:policy3) { Fabricate(:account_statuses_cleanup_policy, account: account4, enabled: false) }
  12. let(:queue_size) { 0 }
  13. let(:queue_latency) { 0 }
  14. let(:process_set_stub) do
  15. [
  16. {
  17. 'concurrency' => 2,
  18. 'queues' => ['push', 'default'],
  19. },
  20. ]
  21. end
  22. let(:retry_size) { 0 }
  23. before do
  24. queue_stub = double
  25. allow(queue_stub).to receive(:size).and_return(queue_size)
  26. allow(queue_stub).to receive(:latency).and_return(queue_latency)
  27. allow(Sidekiq::Queue).to receive(:new).and_return(queue_stub)
  28. allow(Sidekiq::ProcessSet).to receive(:new).and_return(process_set_stub)
  29. sidekiq_stats_stub = double
  30. allow(sidekiq_stats_stub).to receive(:retry_size).and_return(retry_size)
  31. allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_stub)
  32. # Create a bunch of old statuses
  33. 10.times do
  34. Fabricate(:status, account: account1, created_at: 3.years.ago)
  35. Fabricate(:status, account: account2, created_at: 3.years.ago)
  36. Fabricate(:status, account: account3, created_at: 3.years.ago)
  37. Fabricate(:status, account: account4, created_at: 3.years.ago)
  38. Fabricate(:status, account: remote, created_at: 3.years.ago)
  39. end
  40. # Create a bunch of newer statuses
  41. 5.times do
  42. Fabricate(:status, account: account1, created_at: 3.minutes.ago)
  43. Fabricate(:status, account: account2, created_at: 3.minutes.ago)
  44. Fabricate(:status, account: account3, created_at: 3.minutes.ago)
  45. Fabricate(:status, account: account4, created_at: 3.minutes.ago)
  46. Fabricate(:status, account: remote, created_at: 3.minutes.ago)
  47. end
  48. end
  49. describe '#under_load?' do
  50. context 'when nothing is queued' do
  51. it 'returns false' do
  52. expect(subject.under_load?).to be false
  53. end
  54. end
  55. context 'when numerous jobs are queued' do
  56. let(:queue_size) { 5 }
  57. let(:queue_latency) { 120 }
  58. it 'returns true' do
  59. expect(subject.under_load?).to be true
  60. end
  61. end
  62. context 'when there is a huge amount of jobs to retry' do
  63. let(:retry_size) { 1_000_000 }
  64. it 'returns true' do
  65. expect(subject.under_load?).to be true
  66. end
  67. end
  68. end
  69. describe '#get_budget' do
  70. context 'on a single thread' do
  71. let(:process_set_stub) { [ { 'concurrency' => 1, 'queues' => ['push', 'default'] } ] }
  72. it 'returns a low value' do
  73. expect(subject.compute_budget).to be < 10
  74. end
  75. end
  76. context 'on a lot of threads' do
  77. let(:process_set_stub) do
  78. [
  79. { 'concurrency' => 2, 'queues' => ['push', 'default'] },
  80. { 'concurrency' => 2, 'queues' => ['push'] },
  81. { 'concurrency' => 2, 'queues' => ['push'] },
  82. { 'concurrency' => 2, 'queues' => ['push'] },
  83. ]
  84. end
  85. it 'returns a larger value' do
  86. expect(subject.compute_budget).to be > 10
  87. end
  88. end
  89. end
  90. describe '#perform' do
  91. context 'when the budget is lower than the number of toots to delete' do
  92. it 'deletes as many statuses as the given budget' do
  93. expect { subject.perform }.to change { Status.count }.by(-subject.compute_budget)
  94. end
  95. it 'does not delete from accounts with no cleanup policy' do
  96. expect { subject.perform }.to_not change { account2.statuses.count }
  97. end
  98. it 'does not delete from accounts with disabled cleanup policies' do
  99. expect { subject.perform }.to_not change { account4.statuses.count }
  100. end
  101. it 'eventually deletes every deletable toot' do
  102. expect { subject.perform; subject.perform; subject.perform; subject.perform }.to change { Status.count }.by(-20)
  103. end
  104. end
  105. end
  106. end