2019-10-01 14:06:28 -04:00
# frozen_string_literal: true
require 'spec_helper'
2020-10-30 02:08:54 -04:00
# As each associated, backwards-compatible experiment gets cleaned up and removed from the EXPERIMENTS list, its key will also get removed from this list. Once the list here is empty, we can remove the backwards compatibility code altogether.
# Originally created as part of https://gitlab.com/gitlab-org/gitlab/-/merge_requests/45733 for https://gitlab.com/gitlab-org/gitlab/-/issues/270858.
2020-10-28 02:08:59 -04:00
RSpec . describe Gitlab :: Experimentation :: EXPERIMENTS do
it 'temporarily ensures we know what experiments exist for backwards compatibility' do
2020-10-30 02:08:54 -04:00
expected_experiment_keys = [
2020-10-28 02:08:59 -04:00
:upgrade_link_in_user_menu_a ,
:invite_members_version_b ,
:invite_members_empty_group_version_a ,
2021-03-04 07:11:11 -05:00
:contact_sales_btn_in_app
2020-10-28 02:08:59 -04:00
]
2020-10-30 02:08:54 -04:00
backwards_compatible_experiment_keys = described_class . filter { | _ , v | v [ :use_backwards_compatible_subject_index ] } . keys
expect ( backwards_compatible_experiment_keys ) . not_to be_empty , " Oh, hey! Let's clean up that :use_backwards_compatible_subject_index stuff now :D "
expect ( backwards_compatible_experiment_keys ) . to match ( expected_experiment_keys )
2020-10-28 02:08:59 -04:00
end
end
2020-12-02 19:09:53 -05:00
RSpec . describe Gitlab :: Experimentation do
2021-01-19 13:11:04 -05:00
using RSpec :: Parameterized :: TableSyntax
2019-10-23 11:06:29 -04:00
before do
stub_const ( 'Gitlab::Experimentation::EXPERIMENTS' , {
2020-10-28 02:08:59 -04:00
backwards_compatible_test_experiment : {
tracking_category : 'Team' ,
use_backwards_compatible_subject_index : true
} ,
2019-10-23 11:06:29 -04:00
test_experiment : {
tracking_category : 'Team'
2021-01-19 13:11:04 -05:00
} ,
tabular_experiment : {
tracking_category : 'Team' ,
rollout_strategy : rollout_strategy
2019-10-23 11:06:29 -04:00
}
} )
2019-10-01 14:06:28 -04:00
2021-01-07 19:10:44 -05:00
skip_feature_flags_yaml_validation
skip_default_enabled_yaml_check
2020-10-28 02:08:59 -04:00
Feature . enable_percentage_of_time ( :backwards_compatible_test_experiment_experiment_percentage , enabled_percentage )
2020-06-02 08:08:33 -04:00
Feature . enable_percentage_of_time ( :test_experiment_experiment_percentage , enabled_percentage )
2020-11-23 04:09:13 -05:00
allow ( Gitlab ) . to receive ( :com? ) . and_return ( true )
2019-10-01 14:06:28 -04:00
end
2020-04-21 11:21:10 -04:00
let ( :enabled_percentage ) { 10 }
2021-01-19 13:11:04 -05:00
let ( :rollout_strategy ) { nil }
2019-10-23 11:06:29 -04:00
2020-12-02 19:09:53 -05:00
describe '.get_experiment' do
subject { described_class . get_experiment ( :test_experiment ) }
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
context 'returns experiment' do
it { is_expected . to be_instance_of ( Gitlab :: Experimentation :: Experiment ) }
end
context 'experiment is not defined' do
subject { described_class . get_experiment ( :missing_experiment ) }
it { is_expected . to be_nil }
end
end
describe '.active?' do
subject { described_class . active? ( :test_experiment ) }
context 'feature toggle is enabled' do
it { is_expected . to eq ( true ) }
2019-10-01 14:06:28 -04:00
end
describe 'experiment is not defined' do
it 'returns false' do
2020-12-02 19:09:53 -05:00
expect ( described_class . active? ( :missing_experiment ) ) . to eq ( false )
2019-10-01 14:06:28 -04:00
end
end
2020-04-21 11:21:10 -04:00
describe 'experiment is disabled' do
let ( :enabled_percentage ) { 0 }
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
it { is_expected . to eq ( false ) }
2019-10-01 14:06:28 -04:00
end
2019-10-23 11:06:29 -04:00
end
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
describe '.in_experiment_group?' do
context 'with new index calculation' do
let ( :enabled_percentage ) { 50 }
let ( :experiment_subject ) { 'z' } # Zlib.crc32('test_experimentz') % 100 = 33
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
subject { described_class . in_experiment_group? ( :test_experiment , subject : experiment_subject ) }
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
context 'when experiment is active' do
context 'when subject is part of the experiment' do
it { is_expected . to eq ( true ) }
end
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
context 'when subject is not part of the experiment' do
let ( :experiment_subject ) { 'a' } # Zlib.crc32('test_experimenta') % 100 = 61
2019-10-01 14:06:28 -04:00
2020-12-02 19:09:53 -05:00
it { is_expected . to eq ( false ) }
end
2019-10-23 11:06:29 -04:00
2020-12-02 19:09:53 -05:00
context 'when subject has a global_id' do
let ( :experiment_subject ) { double ( :subject , to_global_id : 'z' ) }
2019-10-01 14:06:28 -04:00
2021-01-18 13:10:54 -05:00
it { is_expected . to eq ( true ) }
2019-10-23 11:06:29 -04:00
end
2020-12-02 19:09:53 -05:00
context 'when subject is nil' do
let ( :experiment_subject ) { nil }
2019-10-23 11:06:29 -04:00
2020-12-02 19:09:53 -05:00
it { is_expected . to eq ( false ) }
2019-10-23 11:06:29 -04:00
end
2020-12-02 19:09:53 -05:00
context 'when subject is an empty string' do
let ( :experiment_subject ) { '' }
2019-10-23 11:06:29 -04:00
2020-12-02 19:09:53 -05:00
it { is_expected . to eq ( false ) }
2019-10-23 11:06:29 -04:00
end
2019-10-01 14:06:28 -04:00
end
2020-12-02 19:09:53 -05:00
context 'when experiment is not active' do
before do
allow ( described_class ) . to receive ( :active? ) . and_return ( false )
end
it { is_expected . to eq ( false ) }
end
2019-10-01 14:06:28 -04:00
end
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
context 'with backwards compatible index calculation' do
let ( :experiment_subject ) { 'abcd' } # Digest::SHA1.hexdigest('abcd').hex % 100 = 7
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
subject { described_class . in_experiment_group? ( :backwards_compatible_test_experiment , subject : experiment_subject ) }
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
context 'when experiment is active' do
before do
allow ( described_class ) . to receive ( :active? ) . and_return ( true )
end
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
context 'when subject is part of the experiment' do
it { is_expected . to eq ( true ) }
end
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
context 'when subject is not part of the experiment' do
let ( :experiment_subject ) { 'abc' } # Digest::SHA1.hexdigest('abc').hex % 100 = 17
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
it { is_expected . to eq ( false ) }
end
context 'when subject has a global_id' do
let ( :experiment_subject ) { double ( :subject , to_global_id : 'abcd' ) }
2021-01-18 13:10:54 -05:00
it { is_expected . to eq ( true ) }
2020-12-02 19:09:53 -05:00
end
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
context 'when subject is nil' do
let ( :experiment_subject ) { nil }
it { is_expected . to eq ( false ) }
end
context 'when subject is an empty string' do
let ( :experiment_subject ) { '' }
it { is_expected . to eq ( false ) }
end
end
context 'when experiment is not active' do
before do
allow ( described_class ) . to receive ( :active? ) . and_return ( false )
end
2020-09-24 05:09:35 -04:00
2020-12-02 19:09:53 -05:00
it { is_expected . to eq ( false ) }
2020-09-24 05:09:35 -04:00
end
end
end
2021-01-19 13:11:04 -05:00
describe '.log_invalid_rollout' do
subject { described_class . log_invalid_rollout ( :test_experiment , 1 ) }
before do
allow ( described_class ) . to receive ( :valid_subject_for_rollout_strategy? ) . and_return ( valid )
end
context 'subject is not valid for experiment' do
let ( :valid ) { false }
it 'logs a warning message' do
expect_next_instance_of ( Gitlab :: ExperimentationLogger ) do | logger |
expect ( logger )
. to receive ( :warn )
. with (
message : 'Subject must conform to the rollout strategy' ,
experiment_key : :test_experiment ,
subject : 'Integer' ,
rollout_strategy : :cookie
)
end
subject
end
end
context 'subject is valid for experiment' do
let ( :valid ) { true }
it 'does not log a warning message' do
expect ( Gitlab :: ExperimentationLogger ) . not_to receive ( :build )
subject
end
end
end
describe '.valid_subject_for_rollout_strategy?' do
subject { described_class . valid_subject_for_rollout_strategy? ( :tabular_experiment , experiment_subject ) }
where ( :rollout_strategy , :experiment_subject , :result ) do
:cookie | nil | true
nil | nil | true
:cookie | 'string' | true
nil | User . new | false
:user | User . new | true
:group | User . new | false
:group | Group . new | true
end
with_them do
it { is_expected . to be ( result ) }
end
end
2019-10-01 14:06:28 -04:00
end