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

store_spec.rb « rack_attack « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19b3f239d915f8c42c8cbeff428a5447d35fa415 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::RackAttack::Store, :clean_gitlab_redis_rate_limiting, feature_category: :scalability do
  let(:store) { described_class.new }
  let(:key) { 'foobar' }
  let(:namespaced_key) { "cache:gitlab:#{key}" }

  def with_redis(&block)
    Gitlab::Redis::RateLimiting.with(&block)
  end

  describe '#increment' do
    it 'increments without expiry' do
      5.times do |i|
        expect(store.increment(key, 1)).to eq(i + 1)

        with_redis do |redis|
          expect(redis.get(namespaced_key).to_i).to eq(i + 1)
          expect(redis.ttl(namespaced_key)).to eq(-1)
        end
      end
    end

    it 'rejects amounts other than 1' do
      expect { store.increment(key, 2) }.to raise_exception(described_class::InvalidAmount)
    end

    context 'with expiry' do
      it 'increments and sets expiry' do
        5.times do |i|
          expect(store.increment(key, 1, expires_in: 456)).to eq(i + 1)

          with_redis do |redis|
            expect(redis.get(namespaced_key).to_i).to eq(i + 1)
            expect(redis.ttl(namespaced_key)).to be_within(10).of(456)
          end
        end
      end
    end
  end

  describe '#read' do
    subject { store.read(key) }

    it 'reads the namespaced key' do
      with_redis { |r| r.set(namespaced_key, '123') }

      expect(subject).to eq('123')
    end
  end

  describe '#write' do
    subject { store.write(key, '123', options) }

    let(:options) { {} }

    it 'sets the key' do
      subject

      with_redis do |redis|
        expect(redis.get(namespaced_key)).to eq('123')
        expect(redis.ttl(namespaced_key)).to eq(-1)
      end
    end

    context 'with expiry' do
      let(:options) { { expires_in: 456 } }

      it 'sets the key with expiry' do
        subject

        with_redis do |redis|
          expect(redis.get(namespaced_key)).to eq('123')
          expect(redis.ttl(namespaced_key)).to be_within(10).of(456)
        end
      end
    end
  end

  describe '#delete' do
    subject { store.delete(key) }

    it { expect(subject).to eq(0) }

    context 'when the key exists' do
      before do
        with_redis { |r| r.set(namespaced_key, '123') }
      end

      it { expect(subject).to eq(1) }
    end
  end

  describe '#with' do
    subject { store.send(:with, &:ping) }

    it { expect(subject).to eq('PONG') }

    context 'when redis is unavailable' do
      before do
        broken_redis = Redis.new(
          url: 'redis://127.0.0.0:0',
          instrumentation_class: Gitlab::Redis::RateLimiting.instrumentation_class
        )
        allow(Gitlab::Redis::RateLimiting).to receive(:with).and_yield(broken_redis)
      end

      it { expect(subject).to eq(nil) }
    end
  end
end