~cytrogen/masto-fe

ref: 178e151019cc1b0d5a537543e7d2f6b4712b9fd4 masto-fe/spec/workers/scheduler/accounts_statuses_cleanup_scheduler_spec.rb -rw-r--r-- 6.0 KiB
178e1510 — Claire Merge commit '55e7c08a83547424024bac311d5459cb82cf6dae' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require 'rails_helper'

describe Scheduler::AccountsStatusesCleanupScheduler do
  subject { described_class.new }

  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) }

  let(:queue_size)       { 0 }
  let(:queue_latency)    { 0 }
  let(:process_set_stub) do
    [
      {
        'concurrency' => 2,
        'queues' => %w(push default),
      },
    ]
  end

  before do
    queue_stub = instance_double(Sidekiq::Queue, size: queue_size, latency: queue_latency)
    allow(Sidekiq::Queue).to receive(:new).and_return(queue_stub)
    allow(Sidekiq::ProcessSet).to receive(:new).and_return(process_set_stub)

    sidekiq_stats_stub = instance_double(Sidekiq::Stats)
    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

  describe '#compute_budget' do
    context 'with a single thread' do
      let(:process_set_stub) { [{ 'concurrency' => 1, 'queues' => %w(push default) }] }

      it 'returns a low value' do
        expect(subject.compute_budget).to be < 10
      end
    end

    context 'with a lot of threads' do
      let(:process_set_stub) do
        [
          { 'concurrency' => 2, 'queues' => %w(push default) },
          { '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
    around do |example|
      Timeout.timeout(30) do
        example.run
      end
    end

    before do
      # Policies for the accounts
      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)

      # Create a bunch of old statuses
      4.times do
        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)
        Fabricate(:status, account: remote, created_at: 3.years.ago)
      end

      # Create a bunch of newer statuses
      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)
      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

        expect { subject.perform }
          .to change(Status, :count).by(-subject.compute_budget) # Cleanable statuses
          .and (not_change { account_bob.statuses.count }) # No cleanup policy for account
          .and(not_change { account_dave.statuses.count }) # Disabled cleanup policy
      end

      it 'eventually deletes every deletable toot given enough runs' do
        stub_const 'Scheduler::AccountsStatusesCleanupScheduler::MAX_BUDGET', 4

        expect { 3.times { subject.perform } }.to change(Status, :count).by(-cleanable_statuses_count)
      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 } }
          .to change(Status, :count).by(-3 * 3)
          .and change { account_alice.statuses.count }
          .and change { account_chris.statuses.count }
          .and(change { account_erin.statuses.count })
      end

      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)
          expect { subject.perform }.to change(Status, :count).by(-cleanable_statuses_count)
        end
      end

      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)
          expect { subject.perform }.to_not change(Status, :count)
        end
      end

      def cleanable_statuses_count
        Status
          .where(account_id: [account_alice, account_chris, account_erin]) # Accounts with enabled policies
          .where('created_at < ?', 2.weeks.ago) # Policy defaults is 2.weeks
          .count
      end
    end
  end
end