Commit b20afc7f authored by Alper Akgun's avatar Alper Akgun

Merge branch 'jj-restore-publish-behavior' into 'master'

Restore publishing behavior for Gon

See merge request gitlab-org/gitlab!56374
parents 23e37017 2ea64369
...@@ -9,11 +9,15 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp ...@@ -9,11 +9,15 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
Feature.get(feature_flag_name).state != :off # rubocop:disable Gitlab/AvoidFeatureGet Feature.get(feature_flag_name).state != :off # rubocop:disable Gitlab/AvoidFeatureGet
end end
def publish(_result) def publish(_result = nil)
track(:assignment) # track that we've assigned a variant for this context track(:assignment) # track that we've assigned a variant for this context
# push the experiment data to the client # push the experiment data to the client
Gon.push({ experiment: { name => signature } }, true) if in_request_cycle? begin
Gon.push({ experiment: { name => signature } }, true) # push the experiment data to the client
rescue NoMethodError
# means we're not in the request cycle, and can't add to Gon. Log a warning maybe?
end
end end
def track(action, **event_args) def track(action, **event_args)
...@@ -49,12 +53,6 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp ...@@ -49,12 +53,6 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
name.tr('/', '_') name.tr('/', '_')
end end
def in_request_cycle?
# Gon is only accessible when having a request. This will be fixed with
# https://gitlab.com/gitlab-org/gitlab/-/issues/323352
context.instance_variable_defined?(:@request)
end
def resolve_variant_name def resolve_variant_name
case rollout_strategy case rollout_strategy
when :round_robin when :round_robin
......
...@@ -61,38 +61,19 @@ RSpec.describe ApplicationExperiment, :experiment do ...@@ -61,38 +61,19 @@ RSpec.describe ApplicationExperiment, :experiment do
it "tracks the assignment" do it "tracks the assignment" do
expect(subject).to receive(:track).with(:assignment) expect(subject).to receive(:track).with(:assignment)
subject.publish(nil) subject.publish
end end
context "when inside a request cycle" do it "pushes the experiment knowledge into the client using Gon" do
before do expect(Gon).to receive(:push).with({ experiment: { 'namespaced/stub' => subject.signature } }, true)
subject.context.instance_variable_set(:@request, double('Request', headers: 'true'))
end
it "pushes the experiment knowledge into the client using Gon" do subject.publish
expect(Gon).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 end
context "when outside a request cycle" do it "handles when Gon raises exceptions (like when it can't be pushed into)" do
it "does not push to gon when outside request cycle" do expect(Gon).to receive(:push).and_raise(NoMethodError)
expect(Gon).not_to receive(:push)
subject.publish(nil) expect { subject.publish }.not_to raise_error
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