Commit d9c3a744 authored by Matthias Käppler's avatar Matthias Käppler

Merge branch 'dreedy-check-should_track-every-time-we-call-publish_to_client' into 'master'

Move extra publisher checks to their respective methods

See merge request gitlab-org/gitlab!67366
parents 61c3777c ef1962b0
...@@ -12,17 +12,22 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp ...@@ -12,17 +12,22 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
def publish(_result = nil) def publish(_result = nil)
super super
publish_to_client if should_track? # publish the experiment data to the client publish_to_client
publish_to_database if @record # publish the experiment context to the database publish_to_database
end end
def publish_to_client def publish_to_client
return unless should_track?
Gon.push({ experiment: { name => signature } }, true) Gon.push({ experiment: { name => signature } }, true)
rescue NoMethodError rescue NoMethodError
# means we're not in the request cycle, and can't add to Gon. Log a warning maybe? # means we're not in the request cycle, and can't add to Gon. Log a warning maybe?
end end
def publish_to_database def publish_to_database
return unless @record
return unless should_track?
# if the context contains a namespace, group, project, user, or actor # if the context contains a namespace, group, project, user, or actor
value = context.value value = context.value
subject = value[:namespace] || value[:group] || value[:project] || value[:user] || value[:actor] subject = value[:namespace] || value[:group] || value[:project] || value[:user] || value[:actor]
......
...@@ -57,35 +57,42 @@ RSpec.describe ApplicationExperiment, :experiment do ...@@ -57,35 +57,42 @@ RSpec.describe ApplicationExperiment, :experiment do
end end
describe "#publish" do describe "#publish" do
it "doesn't track or publish to the client or database if we can't track", :snowplow do let(:should_track) { true }
allow(subject).to receive(:should_track?).and_return(false)
expect(subject).not_to receive(:publish_to_client) before do
expect(subject).not_to receive(:publish_to_database) allow(subject).to receive(:should_track?).and_return(should_track)
end
it "tracks the assignment", :snowplow do
subject.publish subject.publish
expect_no_snowplow_event expect_snowplow_event(
category: 'namespaced/stub',
action: 'assignment',
context: [{ schema: anything, data: anything }]
)
end end
it "tracks the assignment" do it "publishes to the client" do
expect(subject).to receive(:track).with(:assignment) expect(subject).to receive(:publish_to_client)
subject.publish subject.publish
end end
it "publishes the to the client" do it "publishes to the database if we've opted for that" do
expect(subject).to receive(:publish_to_client) expect(subject).to receive(:publish_to_database)
subject.publish subject.publish
end end
it "publishes to the database if we've opted for that" do context 'when we should not track' do
subject.record! let(:should_track) { false }
expect(subject).to receive(:publish_to_database) it 'does not track an event to Snowplow', :snowplow do
subject.publish
subject.publish expect_no_snowplow_event
end
end end
describe "#publish_to_client" do describe "#publish_to_client" do
...@@ -101,50 +108,79 @@ RSpec.describe ApplicationExperiment, :experiment do ...@@ -101,50 +108,79 @@ RSpec.describe ApplicationExperiment, :experiment do
expect { subject.publish_to_client }.not_to raise_error expect { subject.publish_to_client }.not_to raise_error
end end
context 'when we should not track' do
let(:should_track) { false }
it 'returns early' do
expect(Gon).not_to receive(:push)
subject.publish_to_client
end
end
end end
describe "#publish_to_database" do describe '#publish_to_database' do
using RSpec::Parameterized::TableSyntax shared_examples 'does not record to the database' do
let(:context) { { context_key => context_value }} it 'does not create an experiment record' do
expect { subject.publish_to_database }.not_to change(Experiment, :count)
end
before do it 'does not create an experiment subject record' do
subject.record! expect { subject.publish_to_database }.not_to change(ExperimentSubject, :count)
end
end end
context "when there's a usable subject" do context 'when we explicitly request to record' do
where(:context_key, :context_value, :object_type) do using RSpec::Parameterized::TableSyntax
:namespace | build(:namespace) | :namespace
:group | build(:namespace) | :namespace before do
:project | build(:project) | :project subject.record!
:user | build(:user) | :user
:actor | build(:user) | :user
end end
with_them do context 'when there is a usable subject' do
it "creates an experiment and experiment subject record" do let(:context) { { context_key => context_value } }
expect { subject.publish_to_database }.to change(Experiment, :count).by(1)
expect(Experiment.last.name).to eq('namespaced/stub') where(:context_key, :context_value, :object_type) do
expect(ExperimentSubject.last.send(object_type)).to eq(context[context_key]) :namespace | build(:namespace) | :namespace
:group | build(:namespace) | :namespace
:project | build(:project) | :project
:user | build(:user) | :user
:actor | build(:user) | :user
end end
end
end
context "when there's not a usable subject" do with_them do
where(:context_key, :context_value) do it 'creates an experiment and experiment subject record' do
:namespace | nil expect { subject.publish_to_database }.to change(Experiment, :count).by(1)
:foo | :bar
expect(Experiment.last.name).to eq('namespaced/stub')
expect(ExperimentSubject.last.send(object_type)).to eq(context[context_key])
end
end
end end
with_them do context 'when there is not a usable subject' do
it "doesn't create an experiment record" do let(:context) { { context_key => context_value } }
expect { subject.publish_to_database }.not_to change(Experiment, :count)
where(:context_key, :context_value) do
:namespace | nil
:foo | :bar
end end
it "doesn't create an experiment subject record" do with_them do
expect { subject.publish_to_database }.not_to change(ExperimentSubject, :count) include_examples 'does not record to the database'
end end
end end
context 'but we should not track' do
let(:should_track) { false }
include_examples 'does not record to the database'
end
end
context 'when we have not explicitly requested to record' do
include_examples 'does not record to the database'
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment