summaryrefslogtreecommitdiff
path: root/spec/support_specs/matchers/exceed_redis_call_limit_spec.rb
blob: 819f50e26b6f0426f2c521125b5de943095529c5 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'RedisCommand matchers', :use_clean_rails_redis_caching, feature_category: :source_code_management do
  let(:control) do
    RedisCommands::Recorder.new do
      Rails.cache.read('test')
      Rails.cache.read('test')
      Rails.cache.write('test', 1)
    end
  end

  before do
    Rails.cache.read('warmup')
  end

  it 'verifies maximum number of Redis calls' do
    expect(control).not_to exceed_redis_calls_limit(3)

    expect(control).not_to exceed_redis_command_calls_limit(:get, 2)
    expect(control).not_to exceed_redis_command_calls_limit(:set, 1)
  end

  it 'verifies minimum number of Redis calls' do
    expect(control).to exceed_redis_calls_limit(2)

    expect(control).to exceed_redis_command_calls_limit(:get, 1)
    expect(control).to exceed_redis_command_calls_limit(:set, 0)
  end

  context 'with Recorder matching only some Redis calls' do
    it 'counts only Redis calls captured by Recorder' do
      Rails.cache.write('ignored', 1)

      control = RedisCommands::Recorder.new do
        Rails.cache.read('recorded')
      end

      Rails.cache.write('also_ignored', 1)

      expect(control).not_to exceed_redis_calls_limit(1)
      expect(control).not_to exceed_redis_command_calls_limit(:set, 0)
      expect(control).not_to exceed_redis_command_calls_limit(:get, 1)
    end
  end

  context 'when expect part is a function' do
    it 'automatically enables RedisCommand::Recorder for it' do
      func = -> do
        Rails.cache.read('test')
        Rails.cache.read('test')
      end

      expect { func.call }.not_to exceed_redis_calls_limit(2)
      expect { func.call }.not_to exceed_redis_command_calls_limit(:get, 2)
    end
  end
end