Welcome to mirror list, hosted at ThFree Co, Russian Federation.

counter_attribute_shared_examples.rb « concerns « models « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f4d5ab3d5c6ed9da1e7c2988967ce52db8cd633e (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# frozen_string_literal: true
require 'spec_helper'

RSpec.shared_examples_for CounterAttribute do |counter_attributes|
  before do
    Gitlab::ApplicationContext.push(feature_category: 'test', caller_id: 'caller')
  end

  it 'defines a Redis counter_key' do
    expect(model.counter_key(:counter_name))
      .to eq("project:{#{model.project_id}}:counters:CounterAttributeModel:#{model.id}:counter_name")
  end

  it 'defines a method to store counters' do
    expect(model.class.counter_attributes.to_a).to eq(counter_attributes)
  end

  counter_attributes.each do |attribute|
    describe attribute do
      describe '#delayed_increment_counter', :redis do
        let(:increment) { 10 }

        subject { model.delayed_increment_counter(attribute, increment) }

        context 'when attribute is a counter attribute' do
          where(:increment) { [10, -3] }

          with_them do
            it 'increments the counter in Redis and logs it' do
              expect(Gitlab::AppLogger).to receive(:info).with(
                hash_including(
                  message: 'Increment counter attribute',
                  attribute: attribute,
                  project_id: model.project_id,
                  increment: increment,
                  new_counter_value: 0 + increment,
                  current_db_value: model.read_attribute(attribute),
                  'correlation_id' => an_instance_of(String),
                  'meta.feature_category' => 'test',
                  'meta.caller_id' => 'caller'
                )
              )

              subject

              Gitlab::Redis::SharedState.with do |redis|
                counter = redis.get(model.counter_key(attribute))
                expect(counter).to eq(increment.to_s)
              end
            end

            it 'does not increment the counter for the record' do
              expect { subject }.not_to change { model.reset.read_attribute(attribute) }
            end

            it 'schedules a worker to flush counter increments asynchronously' do
              expect(FlushCounterIncrementsWorker).to receive(:perform_in)
                .with(CounterAttribute::WORKER_DELAY, model.class.name, model.id, attribute)
                .and_call_original

              subject
            end
          end

          context 'when increment is 0' do
            let(:increment) { 0 }

            it 'does nothing' do
              expect(FlushCounterIncrementsWorker).not_to receive(:perform_in)
              expect(model).not_to receive(:update!)

              subject
            end
          end
        end

        context 'when attribute is not a counter attribute' do
          it 'delegates to ActiveRecord update!' do
            expect { model.delayed_increment_counter(:unknown_attribute, 10) }
              .to raise_error(ActiveModel::MissingAttributeError)
          end
        end
      end
    end
  end

  describe '.flush_increments_to_database!', :redis do
    let(:incremented_attribute) { counter_attributes.first }

    subject { model.flush_increments_to_database!(incremented_attribute) }

    it 'obtains an exclusive lease during processing' do
      expect(model)
        .to receive(:in_lock)
        .with(model.counter_lock_key(incremented_attribute), ttl: described_class::WORKER_LOCK_TTL)
        .and_call_original

      subject
    end

    context 'when there is a counter to flush' do
      before do
        model.delayed_increment_counter(incremented_attribute, 10)
        model.delayed_increment_counter(incremented_attribute, -3)
      end

      it 'updates the record and logs it' do
        expect(Gitlab::AppLogger).to receive(:info).with(
          hash_including(
            message: 'Flush counter attribute to database',
            attribute: incremented_attribute,
            project_id: model.project_id,
            increment: 7,
            previous_db_value: 0,
            new_db_value: 7,
            'correlation_id' => an_instance_of(String),
            'meta.feature_category' => 'test',
            'meta.caller_id' => 'caller'
          )
        )

        expect { subject }.to change { model.reset.read_attribute(incremented_attribute) }.by(7)
      end

      it 'removes the increment entry from Redis' do
        Gitlab::Redis::SharedState.with do |redis|
          key_exists = redis.exists(model.counter_key(incremented_attribute))
          expect(key_exists).to be_truthy
        end

        subject

        Gitlab::Redis::SharedState.with do |redis|
          key_exists = redis.exists(model.counter_key(incremented_attribute))
          expect(key_exists).to be_falsey
        end
      end
    end

    context 'when there are no counters to flush' do
      context 'when there are no counters in the relative :flushed key' do
        it 'does not change the record' do
          expect { subject }.not_to change { model.reset.attributes }
        end
      end

      # This can be the case where updating counters in the database fails with error
      # and retrying the worker will retry flushing the counters but the main key has
      # disappeared and the increment has been moved to the "<...>:flushed" key.
      context 'when there are counters in the relative :flushed key' do
        before do
          Gitlab::Redis::SharedState.with do |redis|
            redis.incrby(model.counter_flushed_key(incremented_attribute), 10)
          end
        end

        it 'updates the record' do
          expect { subject }.to change { model.reset.read_attribute(incremented_attribute) }.by(10)
        end

        it 'deletes the relative :flushed key' do
          subject

          Gitlab::Redis::SharedState.with do |redis|
            key_exists = redis.exists(model.counter_flushed_key(incremented_attribute))
            expect(key_exists).to be_falsey
          end
        end
      end
    end

    context 'when deleting :flushed key fails' do
      before do
        Gitlab::Redis::SharedState.with do |redis|
          redis.incrby(model.counter_flushed_key(incremented_attribute), 10)

          expect(redis).to receive(:del).and_raise('could not delete key')
        end
      end

      it 'does a rollback of the counter update' do
        expect { subject }.to raise_error('could not delete key')

        expect(model.reset.read_attribute(incremented_attribute)).to eq(0)
      end
    end
  end

  describe '#clear_counter!' do
    let(:attribute) { counter_attributes.first }

    before do
      model.increment_counter(attribute, 10)
    end

    it 'deletes the counter key for the given attribute and logs it' do
      expect(Gitlab::AppLogger).to receive(:info).with(
        hash_including(
          message: 'Clear counter attribute',
          attribute: attribute,
          project_id: model.project_id,
          'correlation_id' => an_instance_of(String),
          'meta.feature_category' => 'test',
          'meta.caller_id' => 'caller'
        )
      )

      model.clear_counter!(attribute)

      Gitlab::Redis::SharedState.with do |redis|
        key_exists = redis.exists(model.counter_key(attribute))
        expect(key_exists).to be_falsey
      end
    end
  end
end