summaryrefslogtreecommitdiff
path: root/spec/experiments/strategy/round_robin_spec.rb
blob: f837a4701b287d244f06ddbfc97619af5a9a4c55 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Strategy::RoundRobin, :clean_gitlab_redis_shared_state do
  subject(:round_robin) { described_class.new('_key_', %i[variant1 variant2]) }

  describe "execute" do
    context "when there are 2 variants" do
      it "proves out round robin in selection", :aggregate_failures do
        expect(round_robin.execute).to eq :variant2
        expect(round_robin.execute).to eq :variant1
        expect(round_robin.execute).to eq :variant2
      end
    end

    context "when there are more than 2 variants" do
      subject(:round_robin) { described_class.new('_key_', %i[variant1 variant2 variant3]) }

      it "proves out round robin in selection", :aggregate_failures do
        expect(round_robin.execute).to eq :variant2
        expect(round_robin.execute).to eq :variant3
        expect(round_robin.execute).to eq :variant1

        expect(round_robin.execute).to eq :variant2
        expect(round_robin.execute).to eq :variant3
        expect(round_robin.execute).to eq :variant1
      end
    end

    context "when writing to cache fails" do
      subject(:round_robin) { described_class.new('_key_', []) }

      it "raises an error and logs" do
        allow(Gitlab::Redis::SharedState).to receive(:with).and_raise(Strategy::RoundRobin::CacheError)
        expect(Gitlab::AppLogger).to receive(:warn)

        expect { round_robin.execute }.to raise_error(Strategy::RoundRobin::CacheError)
      end
    end
  end

  describe "#counter_expires_in" do
    it 'displays the expiration time in seconds' do
      round_robin.execute

      expect(round_robin.counter_expires_in).to be_between(0, described_class::COUNTER_EXPIRE_TIME)
    end
  end

  describe '#value' do
    it 'get the count' do
      expect(round_robin.counter_value).to eq(0)

      round_robin.execute

      expect(round_robin.counter_value).to eq(1)
    end
  end

  describe '#reset!' do
    it 'resets the count down to zero' do
      3.times { round_robin.execute }

      expect { round_robin.reset! }.to change { round_robin.counter_value }.from(3).to(0)
    end
  end
end