summaryrefslogtreecommitdiff
path: root/spec/experiments/application_experiment/cache_spec.rb
blob: 4caa91e6ac45d57177e2e36ab6995ffcc1b5d26b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ApplicationExperiment::Cache do
  let(:key_name) { 'experiment_name' }
  let(:field_name) { 'abc123' }
  let(:key_field) { [key_name, field_name].join(':') }
  let(:shared_state) { Gitlab::Redis::SharedState }

  around do |example|
    shared_state.with { |r| r.del(key_name) }
    example.run
    shared_state.with { |r| r.del(key_name) }
  end

  it "allows reading, writing and deleting", :aggregate_failures do
    # we test them all together because they are largely interdependent

    expect(subject.read(key_field)).to be_nil
    expect(shared_state.with { |r| r.hget(key_name, field_name) }).to be_nil

    subject.write(key_field, 'value')

    expect(subject.read(key_field)).to eq('value')
    expect(shared_state.with { |r| r.hget(key_name, field_name) }).to eq('value')

    subject.delete(key_field)

    expect(subject.read(key_field)).to be_nil
    expect(shared_state.with { |r| r.hget(key_name, field_name) }).to be_nil
  end

  it "handles the fetch with a block behavior (which is read/write)" do
    expect(subject.fetch(key_field) { 'value1' }).to eq('value1') # rubocop:disable Style/RedundantFetchBlock
    expect(subject.fetch(key_field) { 'value2' }).to eq('value1') # rubocop:disable Style/RedundantFetchBlock
  end

  it "can clear a whole experiment cache key" do
    subject.write(key_field, 'value')
    subject.clear(key: key_field)

    expect(shared_state.with { |r| r.get(key_name) }).to be_nil
  end

  it "doesn't allow clearing a key from the cache that's not a hash (definitely not an experiment)" do
    shared_state.with { |r| r.set(key_name, 'value') }

    expect { subject.clear(key: key_name) }.to raise_error(
      ArgumentError,
      'invalid call to clear a non-hash cache key'
    )
  end
end