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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Experiment::Rollout::Feature, :experiment, feature_category: :acquisition do
subject { described_class.new(subject_experiment) }
let(:subject_experiment) { experiment('namespaced/stub') }
describe "#enabled?", :saas do
before do
stub_feature_flags(gitlab_experiment: true)
allow(subject).to receive(:feature_flag_defined?).and_return(true)
allow(subject).to receive(:feature_flag_instance).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(subject).to receive(:feature_flag_defined?).and_return(false)
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(:com?).and_return(false)
expect(subject).not_to be_enabled
end
it "isn't enabled if the feature flag state is :off" do
expect(subject).to receive(:feature_flag_instance).and_return(double(state: :off))
expect(subject).not_to be_enabled
end
it "isn't enabled if the gitlab_experiment feature flag is false" do
stub_feature_flags(gitlab_experiment: false)
expect(subject).not_to be_enabled
end
end
describe "#execute_assignment" do
let(:variants) do
->(e) do
# rubocop:disable Lint/EmptyBlock
e.control {}
e.variant(:red) {}
e.variant(:blue) {}
# rubocop:enable Lint/EmptyBlock
end
end
let(:subject_experiment) { experiment('namespaced/stub', &variants) }
before do
allow(Feature).to receive(:enabled?).with('namespaced_stub', any_args).and_return(true)
end
it "uses the default value as specified in the yaml" do
expect(Feature).to receive(:enabled?).with(
'namespaced_stub',
subject,
type: :experiment
).and_return(false)
expect(subject.execute_assignment).to be_nil
end
it "returns an assigned name" do
expect(subject.execute_assignment).to eq(:blue)
end
context "when there are no behaviors" do
let(:variants) { ->(e) { e.control {} } } # rubocop:disable Lint/EmptyBlock
it "does not raise an error" do
expect { subject.execute_assignment }.not_to raise_error
end
end
context "for even rollout to non-control", :saas do
let(:counts) { Hash.new(0) }
let(:subject_experiment) { experiment('namespaced/stub') }
before do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:enabled?).and_return(true)
end
subject_experiment.variant(:variant1) {} # rubocop:disable Lint/EmptyBlock
subject_experiment.variant(:variant2) {} # rubocop:disable Lint/EmptyBlock
end
it "rolls out relatively evenly to 2 behaviors" do
100.times { |i| run_cycle(subject_experiment, value: i) }
expect(counts).to eq(variant1: 54, variant2: 46)
end
it "rolls out relatively evenly to 3 behaviors" do
subject_experiment.variant(:variant3) {} # rubocop:disable Lint/EmptyBlock
100.times { |i| run_cycle(subject_experiment, value: i) }
expect(counts).to eq(variant1: 31, variant2: 29, variant3: 40)
end
context "when distribution is specified as an array" do
before do
subject_experiment.rollout(described_class, distribution: [32, 25, 43])
end
it "rolls out with the expected distribution" do
subject_experiment.variant(:variant3) {} # rubocop:disable Lint/EmptyBlock
100.times { |i| run_cycle(subject_experiment, value: i) }
expect(counts).to eq(variant1: 39, variant2: 24, variant3: 37)
end
end
context "when distribution is specified as a hash" do
before do
subject_experiment.rollout(described_class, distribution: { variant1: 90, variant2: 10 })
end
it "rolls out with the expected distribution" do
100.times { |i| run_cycle(subject_experiment, value: i) }
expect(counts).to eq(variant1: 95, variant2: 5)
end
end
def run_cycle(experiment, **context)
experiment.instance_variable_set(:@_assigned_variant_name, nil)
experiment.context(context) if context
begin
experiment.cache.delete
rescue StandardError
nil
end
counts[experiment.assigned.name] += 1
end
end
end
describe "#flipper_id" do
it "returns the expected flipper id if the experiment doesn't provide one" do
subject.instance_variable_set(:@experiment, double(id: '__id__'))
expect(subject.flipper_id).to eq('Experiment;__id__')
end
it "lets the experiment provide a flipper id so it can override the default" do
allow(subject_experiment).to receive(:flipper_id).and_return('_my_overridden_id_')
expect(subject.flipper_id).to eq('_my_overridden_id_')
end
end
end
|