2020-12-16 04:10:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-02-01 07:09:03 -05:00
|
|
|
RSpec.describe ApplicationExperiment, :experiment do
|
2021-11-23 16:10:02 -05:00
|
|
|
subject(:application_experiment) { described_class.new('namespaced/stub', **context) }
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
let(:context) { {} }
|
|
|
|
let(:feature_definition) { { name: 'namespaced_stub', type: 'experiment', default_enabled: false } }
|
2021-02-02 01:09:47 -05:00
|
|
|
|
|
|
|
around do |example|
|
2021-02-03 10:09:24 -05:00
|
|
|
Feature::Definition.definitions[:namespaced_stub] = Feature::Definition.new('namespaced_stub.yml', feature_definition)
|
2021-02-02 01:09:47 -05:00
|
|
|
example.run
|
2021-02-03 10:09:24 -05:00
|
|
|
Feature::Definition.definitions.delete(:namespaced_stub)
|
2021-02-02 01:09:47 -05:00
|
|
|
end
|
|
|
|
|
2021-02-01 07:09:03 -05:00
|
|
|
before do
|
2021-11-23 16:10:02 -05:00
|
|
|
allow(application_experiment).to receive(:enabled?).and_return(true)
|
2021-02-01 07:09:03 -05:00
|
|
|
end
|
|
|
|
|
2022-02-21 07:16:55 -05:00
|
|
|
it "registers a default control behavior for anonymous experiments" do
|
|
|
|
# This default control behavior is not inherited, intentionally, but it
|
|
|
|
# does provide anonymous experiments with a base control behavior to keep
|
|
|
|
# them optional there.
|
2021-05-12 14:10:35 -04:00
|
|
|
|
2022-02-21 07:16:55 -05:00
|
|
|
expect(experiment(:example)).to register_behavior(:control).with(nil)
|
|
|
|
expect { experiment(:example) { } }.not_to raise_error
|
2021-05-12 14:10:35 -04:00
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
describe "#publish" do
|
2021-08-17 05:10:02 -04:00
|
|
|
it "tracks the assignment", :snowplow do
|
2022-03-01 10:22:06 -05:00
|
|
|
expect(application_experiment).to receive(:track).with(:assignment)
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.publish
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
|
2022-03-01 10:22:06 -05:00
|
|
|
it "adds to the published experiments" do
|
|
|
|
# These are surfaced in the client layer by rendering them in the
|
|
|
|
# _published_experiments.html.haml partial.
|
|
|
|
application_experiment.publish
|
2021-08-17 05:10:02 -04:00
|
|
|
|
2022-03-01 10:22:06 -05:00
|
|
|
expect(ApplicationExperiment.published_experiments['namespaced/stub']).to include(
|
|
|
|
experiment: 'namespaced/stub',
|
|
|
|
excluded: false,
|
|
|
|
key: anything,
|
|
|
|
variant: 'control'
|
|
|
|
)
|
2021-05-26 11:10:57 -04:00
|
|
|
end
|
|
|
|
|
2021-08-17 05:10:02 -04:00
|
|
|
describe '#publish_to_database' do
|
2021-08-17 14:10:35 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2022-02-02 16:16:54 -05:00
|
|
|
let(:publish_to_database) { ActiveSupport::Deprecation.silence { application_experiment.publish_to_database } }
|
2021-11-23 16:10:02 -05:00
|
|
|
|
2021-08-17 05:10:02 -04:00
|
|
|
shared_examples 'does not record to the database' do
|
|
|
|
it 'does not create an experiment record' do
|
2021-11-23 16:10:02 -05:00
|
|
|
expect { publish_to_database }.not_to change(Experiment, :count)
|
2021-08-17 05:10:02 -04:00
|
|
|
end
|
2021-06-28 17:10:13 -04:00
|
|
|
|
2021-08-17 05:10:02 -04:00
|
|
|
it 'does not create an experiment subject record' do
|
2021-11-23 16:10:02 -05:00
|
|
|
expect { publish_to_database }.not_to change(ExperimentSubject, :count)
|
2021-08-17 05:10:02 -04:00
|
|
|
end
|
2021-05-26 11:10:57 -04:00
|
|
|
end
|
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
context 'when there is a usable subject' do
|
|
|
|
let(:context) { { context_key => context_value } }
|
2021-08-17 05:10:02 -04:00
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
where(:context_key, :context_value, :object_type) do
|
2021-11-23 16:10:02 -05:00
|
|
|
:namespace | build(:namespace, id: non_existing_record_id) | :namespace
|
|
|
|
:group | build(:namespace, id: non_existing_record_id) | :namespace
|
|
|
|
:project | build(:project, id: non_existing_record_id) | :project
|
|
|
|
:user | build(:user, id: non_existing_record_id) | :user
|
|
|
|
:actor | build(:user, id: non_existing_record_id) | :user
|
2021-06-28 17:10:13 -04:00
|
|
|
end
|
2021-05-26 11:10:57 -04:00
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
with_them do
|
|
|
|
it 'creates an experiment and experiment subject record' do
|
2021-11-23 16:10:02 -05:00
|
|
|
expect { publish_to_database }.to change(Experiment, :count).by(1)
|
2021-08-17 05:10:02 -04:00
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
expect(Experiment.last.name).to eq('namespaced/stub')
|
|
|
|
expect(ExperimentSubject.last.send(object_type)).to eq(context[context_key])
|
2021-08-17 05:10:02 -04:00
|
|
|
end
|
2021-06-28 17:10:13 -04:00
|
|
|
end
|
2021-08-17 14:10:35 -04:00
|
|
|
end
|
2021-05-26 11:10:57 -04:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
context "when experiment hasn't ran" do
|
|
|
|
let(:context) { { user: create(:user) } }
|
|
|
|
|
|
|
|
it 'sets a variant on the experiment subject' do
|
|
|
|
publish_to_database
|
|
|
|
|
|
|
|
expect(ExperimentSubject.last.variant).to eq('control')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
context 'when there is not a usable subject' do
|
|
|
|
let(:context) { { context_key => context_value } }
|
2021-05-26 11:10:57 -04:00
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
where(:context_key, :context_value) do
|
|
|
|
:namespace | nil
|
|
|
|
:foo | :bar
|
2021-05-26 11:10:57 -04:00
|
|
|
end
|
2021-08-17 05:10:02 -04:00
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
with_them do
|
2021-08-17 05:10:02 -04:00
|
|
|
include_examples 'does not record to the database'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-17 14:10:35 -04:00
|
|
|
context 'but we should not track' do
|
|
|
|
let(:should_track) { false }
|
|
|
|
|
2021-08-17 05:10:02 -04:00
|
|
|
include_examples 'does not record to the database'
|
2021-05-26 11:10:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
describe "#track", :snowplow do
|
2021-07-01 17:08:38 -04:00
|
|
|
let(:fake_context) do
|
|
|
|
SnowplowTracker::SelfDescribingJson.new('iglu:com.gitlab/fake/jsonschema/0-0-0', { data: '_data_' })
|
|
|
|
end
|
|
|
|
|
2021-02-01 07:09:03 -05:00
|
|
|
it "doesn't track if we shouldn't track" do
|
2021-11-23 16:10:02 -05:00
|
|
|
allow(application_experiment).to receive(:should_track?).and_return(false)
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.track(:action)
|
2020-12-16 04:10:26 -05:00
|
|
|
|
|
|
|
expect_no_snowplow_event
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tracks the event with the expected arguments and merged contexts" do
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.track(:action, property: '_property_', context: [fake_context])
|
2020-12-16 04:10:26 -05:00
|
|
|
|
|
|
|
expect_snowplow_event(
|
2021-02-03 10:09:24 -05:00
|
|
|
category: 'namespaced/stub',
|
2020-12-16 04:10:26 -05:00
|
|
|
action: 'action',
|
|
|
|
property: '_property_',
|
|
|
|
context: [
|
|
|
|
{
|
|
|
|
schema: 'iglu:com.gitlab/fake/jsonschema/0-0-0',
|
|
|
|
data: { data: '_data_' }
|
|
|
|
},
|
|
|
|
{
|
2021-03-25 08:09:19 -04:00
|
|
|
schema: 'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0',
|
2021-02-03 10:09:24 -05:00
|
|
|
data: { experiment: 'namespaced/stub', key: '86208ac54ca798e11f127e8b23ec396a', variant: 'control' }
|
2020-12-16 04:10:26 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2021-07-01 17:08:38 -04:00
|
|
|
|
2021-12-14 16:13:08 -05:00
|
|
|
context "when using known context resources" do
|
|
|
|
let(:user) { build(:user, id: non_existing_record_id) }
|
|
|
|
let(:project) { build(:project, id: non_existing_record_id) }
|
|
|
|
let(:namespace) { build(:namespace, id: non_existing_record_id) }
|
|
|
|
let(:group) { build(:group, id: non_existing_record_id) }
|
|
|
|
let(:actor) { user }
|
|
|
|
|
|
|
|
let(:context) { { user: user, project: project, namespace: namespace } }
|
|
|
|
|
|
|
|
it "includes those using the gitlab standard context" do
|
|
|
|
subject.track(:action)
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'namespaced/stub',
|
|
|
|
action: 'action',
|
|
|
|
user: user,
|
|
|
|
project: project,
|
|
|
|
namespace: namespace,
|
|
|
|
context: an_instance_of(Array)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "falls back to using the group key" do
|
|
|
|
subject.context(namespace: nil, group: group)
|
|
|
|
|
|
|
|
subject.track(:action)
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'namespaced/stub',
|
|
|
|
action: 'action',
|
|
|
|
user: user,
|
|
|
|
project: project,
|
|
|
|
namespace: group,
|
|
|
|
context: an_instance_of(Array)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the actor key" do
|
|
|
|
it "provides it to the tracking call as the user" do
|
|
|
|
subject.context(user: nil, actor: actor)
|
|
|
|
|
|
|
|
subject.track(:action)
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'namespaced/stub',
|
|
|
|
action: 'action',
|
|
|
|
user: actor,
|
|
|
|
project: project,
|
|
|
|
namespace: namespace,
|
|
|
|
context: an_instance_of(Array)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles when it's not a user record" do
|
|
|
|
subject.context(user: nil, actor: nil)
|
|
|
|
|
|
|
|
subject.track(:action)
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'namespaced/stub',
|
|
|
|
action: 'action',
|
|
|
|
project: project,
|
|
|
|
namespace: namespace,
|
|
|
|
context: an_instance_of(Array)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
describe "#key_for" do
|
|
|
|
it "generates MD5 hashes" do
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.key_for(foo: :bar)).to eq('6f9ac12afdb9b58c2f19a136d09f9153')
|
2021-06-28 17:10:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-23 14:10:06 -04:00
|
|
|
describe "#process_redirect_url" do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:url, :processed_url) do
|
|
|
|
'https://about.gitlab.com/' | 'https://about.gitlab.com/'
|
|
|
|
'https://gitlab.com/' | 'https://gitlab.com/'
|
|
|
|
'http://docs.gitlab.com' | 'http://docs.gitlab.com'
|
|
|
|
'https://docs.gitlab.com/some/path?foo=bar' | 'https://docs.gitlab.com/some/path?foo=bar'
|
|
|
|
'http://badgitlab.com' | nil
|
|
|
|
'https://gitlab.com.nefarious.net' | nil
|
|
|
|
'https://unknown.gitlab.com' | nil
|
|
|
|
"https://badplace.com\nhttps://gitlab.com" | nil
|
|
|
|
'https://gitlabbcom' | nil
|
|
|
|
'https://gitlabbcom/' | nil
|
2021-11-24 07:10:21 -05:00
|
|
|
'http://gdk.test/foo/bar' | 'http://gdk.test/foo/bar'
|
|
|
|
'http://localhost:3000/foo/bar' | 'http://localhost:3000/foo/bar'
|
2021-07-23 14:10:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it "returns the url or nil if invalid" do
|
2022-03-10 22:08:14 -05:00
|
|
|
allow(Gitlab).to receive(:com?).and_return(true)
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.process_redirect_url(url)).to eq(processed_url)
|
2021-07-23 14:10:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "considers all urls invalid when not on dev or com" do
|
2022-03-10 22:08:14 -05:00
|
|
|
allow(Gitlab).to receive(:com?).and_return(false)
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.process_redirect_url(url)).to be_nil
|
2021-07-23 14:10:06 -04:00
|
|
|
end
|
|
|
|
end
|
2021-07-30 17:10:15 -04:00
|
|
|
|
|
|
|
it "generates the correct urls based on where the engine was mounted" do
|
2021-11-23 16:10:02 -05:00
|
|
|
url = Rails.application.routes.url_helpers.experiment_redirect_url(application_experiment, url: 'https://docs.gitlab.com')
|
|
|
|
expect(url).to include("/-/experiment/namespaced%2Fstub:#{application_experiment.context.key}?https://docs.gitlab.com")
|
2021-07-30 17:10:15 -04:00
|
|
|
end
|
2021-07-23 14:10:06 -04:00
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
context "when resolving variants" do
|
2022-02-08 19:15:57 -05:00
|
|
|
before do
|
|
|
|
stub_feature_flags(namespaced_stub: true)
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
|
2022-02-08 19:15:57 -05:00
|
|
|
it "returns an assigned name" do
|
|
|
|
application_experiment.variant(:variant1) {}
|
|
|
|
application_experiment.variant(:variant2) {}
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2022-02-08 19:15:57 -05:00
|
|
|
expect(application_experiment.assigned.name).to eq('variant2')
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-07 16:14:42 -05:00
|
|
|
context "when nesting experiments" do
|
|
|
|
before do
|
|
|
|
stub_experiments(top: :control, nested: :control)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an exception" do
|
|
|
|
expect { experiment(:top) { |e| e.control { experiment(:nested) { } } } }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tracks an event", :snowplow do
|
|
|
|
experiment(:top) { |e| e.control { experiment(:nested) { } } }
|
|
|
|
|
|
|
|
expect(Gitlab::Tracking).to have_received(:event).with( # rubocop:disable RSpec/ExpectGitlabTracking
|
|
|
|
'top',
|
|
|
|
'nested',
|
|
|
|
hash_including(label: 'nested')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-16 04:10:26 -05:00
|
|
|
context "when caching" do
|
2021-02-23 04:10:45 -05:00
|
|
|
let(:cache) { Gitlab::Experiment::Configuration.cache }
|
2020-12-16 04:10:26 -05:00
|
|
|
|
|
|
|
before do
|
2021-02-23 04:10:45 -05:00
|
|
|
allow(Gitlab::Experiment::Configuration).to receive(:cache).and_call_original
|
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
cache.clear(key: application_experiment.name)
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2022-02-04 16:16:06 -05:00
|
|
|
application_experiment.control { }
|
|
|
|
application_experiment.candidate { }
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "caches the variant determined by the variant resolver" do
|
2022-03-02 07:15:38 -05:00
|
|
|
expect(application_experiment.assigned.name).to eq('candidate') # we should be in the experiment
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.run
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.cache.read).to eq('candidate')
|
2020-12-16 04:10:26 -05:00
|
|
|
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.
|
2021-02-03 10:09:24 -05:00
|
|
|
stub_feature_flags(namespaced_stub: false) # simulate being not rolled out
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2022-03-02 07:15:38 -05:00
|
|
|
expect(application_experiment.assigned.name).to eq('control') # if we ask, it should be control
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.run
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.cache.read).to be_nil
|
2020-12-16 04:10:26 -05:00
|
|
|
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.
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.run(:control)
|
2020-12-16 04:10:26 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.cache.read).to eq('control')
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "arbitrary attributes" do
|
|
|
|
before do
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.cache.store.clear(key: application_experiment.name + '_attrs')
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets and gets attributes about an experiment" do
|
2021-11-23 16:10:02 -05:00
|
|
|
application_experiment.cache.attr_set(:foo, :bar)
|
2021-02-23 04:10:45 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.cache.attr_get(:foo)).to eq('bar')
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "increments a value for an experiment" do
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.cache.attr_get(:foo)).to be_nil
|
2021-02-23 04:10:45 -05:00
|
|
|
|
2021-11-23 16:10:02 -05:00
|
|
|
expect(application_experiment.cache.attr_inc(:foo)).to eq(1)
|
|
|
|
expect(application_experiment.cache.attr_inc(:foo)).to eq(2)
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
end
|
2022-02-10 22:12:21 -05:00
|
|
|
|
|
|
|
context "with deprecation warnings" do
|
|
|
|
before do
|
|
|
|
Gitlab::Experiment::Configuration.instance_variable_set(:@__dep_versions, nil) # clear the internal memoization
|
|
|
|
|
|
|
|
allow(ActiveSupport::Deprecation).to receive(:new).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't warn on non dev/test environments" do
|
|
|
|
allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)
|
|
|
|
|
|
|
|
expect { experiment(:example) { |e| e.use { } } }.not_to raise_error
|
|
|
|
expect(ActiveSupport::Deprecation).not_to have_received(:new).with(anything, 'Gitlab::Experiment')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "warns on dev and test environments" do
|
|
|
|
allow(Gitlab).to receive(:dev_or_test_env?).and_return(true)
|
|
|
|
|
|
|
|
# This will eventually raise an ActiveSupport::Deprecation exception,
|
|
|
|
# it's ok to change it when that happens.
|
|
|
|
expect { experiment(:example) { |e| e.use { } } }.not_to raise_error
|
|
|
|
|
|
|
|
expect(ActiveSupport::Deprecation).to have_received(:new).with(anything, 'Gitlab::Experiment')
|
|
|
|
end
|
|
|
|
end
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|