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

application_experiment_spec.rb « experiments « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 501d344e920a52020cbffc4a8394de0c52ac19e3 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ApplicationExperiment, :experiment do
  subject { described_class.new('namespaced/stub') }

  let(:feature_definition) do
    { name: 'namespaced_stub', type: 'experiment', group: 'group::adoption', default_enabled: false }
  end

  around do |example|
    Feature::Definition.definitions[:namespaced_stub] = Feature::Definition.new('namespaced_stub.yml', feature_definition)
    example.run
    Feature::Definition.definitions.delete(:namespaced_stub)
  end

  before do
    allow(subject).to receive(:enabled?).and_return(true)
  end

  it "naively assumes a 1x1 relationship to feature flags for tests" do
    expect(Feature).to receive(:persist_used!).with('namespaced_stub')

    described_class.new('namespaced/stub')
  end

  describe "enabled" do
    before do
      allow(subject).to receive(:enabled?).and_call_original

      allow(Feature::Definition).to receive(:get).and_return('_instance_')
      allow(Gitlab).to receive(:dev_env_or_com?).and_return(true)
      allow(Feature).to receive(:get).and_return(double(state: :on))
    end

    it "is enabled when all criteria are met" do
      expect(subject).to be_enabled
    end

    it "isn't enabled if the feature definition doesn't exist" do
      expect(Feature::Definition).to receive(:get).with('namespaced_stub').and_return(nil)

      expect(subject).not_to be_enabled
    end

    it "isn't enabled if we're not in dev or dotcom environments" do
      expect(Gitlab).to receive(:dev_env_or_com?).and_return(false)

      expect(subject).not_to be_enabled
    end

    it "isn't enabled if the feature flag state is :off" do
      expect(Feature).to receive(:get).with('namespaced_stub').and_return(double(state: :off))

      expect(subject).not_to be_enabled
    end
  end

  describe "publishing results" do
    it "tracks the assignment" do
      expect(subject).to receive(:track).with(:assignment)

      subject.publish(nil)
    end

    it "pushes the experiment knowledge into the client using Gon.global" do
      expect(Gon.global).to receive(:push).with(
        {
          experiment: {
            'namespaced/stub' => { # string key because it can be namespaced
              experiment: 'namespaced/stub',
              key: '86208ac54ca798e11f127e8b23ec396a',
              variant: 'control'
            }
          }
        },
        true
      )

      subject.publish(nil)
    end
  end

  describe "tracking events", :snowplow do
    it "doesn't track if we shouldn't track" do
      allow(subject).to receive(:should_track?).and_return(false)

      subject.track(:action)

      expect_no_snowplow_event
    end

    it "tracks the event with the expected arguments and merged contexts" do
      subject.track(:action, property: '_property_', context: [
        SnowplowTracker::SelfDescribingJson.new('iglu:com.gitlab/fake/jsonschema/0-0-0', { data: '_data_' })
      ])

      expect_snowplow_event(
        category: 'namespaced/stub',
        action: 'action',
        property: '_property_',
        context: [
          {
            schema: 'iglu:com.gitlab/fake/jsonschema/0-0-0',
            data: { data: '_data_' }
          },
          {
            schema: 'iglu:com.gitlab/gitlab_experiment/jsonschema/0-3-0',
            data: { experiment: 'namespaced/stub', key: '86208ac54ca798e11f127e8b23ec396a', variant: 'control' }
          }
        ]
      )
    end
  end

  describe "variant resolution" do
    context "when using the default feature flag percentage rollout" do
      it "uses the default value as specified in the yaml" do
        expect(Feature).to receive(:enabled?).with('namespaced_stub', subject, type: :experiment, default_enabled: :yaml)

        expect(subject.variant.name).to eq('control')
      end

      it "returns nil when not rolled out" do
        stub_feature_flags(namespaced_stub: false)

        expect(subject.variant.name).to eq('control')
      end

      context "when rolled out to 100%" do
        it "returns the first variant name" do
          subject.try(:variant1) {}
          subject.try(:variant2) {}

          expect(subject.variant.name).to eq('variant1')
        end
      end
    end

    context "when using the round_robin strategy", :clean_gitlab_redis_shared_state do
      context "when variants aren't supplied" do
        subject :inheriting_class do
          Class.new(described_class) do
            def rollout_strategy
              :round_robin
            end
          end.new('namespaced/stub')
        end

        it "raises an error" do
          expect { inheriting_class.variants }.to raise_error(NotImplementedError)
        end
      end

      context "when variants are supplied" do
        let(:inheriting_class) do
          Class.new(described_class) do
            def rollout_strategy
              :round_robin
            end

            def variants
              %i[variant1 variant2 control]
            end
          end
        end

        it "proves out round robin in variant selection", :aggregate_failures do
          instance_1 = inheriting_class.new('namespaced/stub')
          allow(instance_1).to receive(:enabled?).and_return(true)
          instance_2 = inheriting_class.new('namespaced/stub')
          allow(instance_2).to receive(:enabled?).and_return(true)
          instance_3 = inheriting_class.new('namespaced/stub')
          allow(instance_3).to receive(:enabled?).and_return(true)

          instance_1.try {}

          expect(instance_1.variant.name).to eq('variant2')

          instance_2.try {}

          expect(instance_2.variant.name).to eq('control')

          instance_3.try {}

          expect(instance_3.variant.name).to eq('variant1')
        end
      end
    end
  end

  context "when caching" do
    let(:cache) { ApplicationExperiment::Cache.new }

    before do
      cache.clear(key: subject.name)

      subject.use { } # setup the control
      subject.try { } # setup the candidate

      allow(Gitlab::Experiment::Configuration).to receive(:cache).and_return(cache)
    end

    it "caches the variant determined by the variant resolver" do
      expect(subject.variant.name).to eq('candidate') # we should be in the experiment

      subject.run

      expect(cache.read(subject.cache_key)).to eq('candidate')
    end

    it "doesn't cache a variant if we don't explicitly provide one" do
      # by not caching "empty" variants, we effectively create a mostly
      # optimal combination of caching and rollout flexibility. If we cached
      # every control variant assigned, we'd inflate the cache size and
      # wouldn't be able to roll out to subjects that we'd already assigned to
      # the control.
      stub_feature_flags(namespaced_stub: false) # simulate being not rolled out

      expect(subject.variant.name).to eq('control') # if we ask, it should be control

      subject.run

      expect(cache.read(subject.cache_key)).to be_nil
    end

    it "caches a control variant if we assign it specifically" do
      # by specifically assigning the control variant here, we're guaranteeing
      # that this context will always get the control variant unless we delete
      # the field from the cache (or clear the entire experiment cache) -- or
      # write code that would specify a different variant.
      subject.run(:control)

      expect(cache.read(subject.cache_key)).to eq('control')
    end
  end
end