Commit 2ea64369 authored by jejacks0n's avatar jejacks0n

Restore publishing behavior for Gon

parent d1b11f67
......@@ -9,11 +9,15 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
Feature.get(feature_flag_name).state != :off # rubocop:disable Gitlab/AvoidFeatureGet
end
def publish(_result)
def publish(_result = nil)
track(:assignment) # track that we've assigned a variant for this context
# 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
def track(action, **event_args)
......@@ -49,12 +53,6 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
name.tr('/', '_')
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
case rollout_strategy
when :round_robin
......
......@@ -61,38 +61,19 @@ RSpec.describe ApplicationExperiment, :experiment do
it "tracks the assignment" do
expect(subject).to receive(:track).with(:assignment)
subject.publish(nil)
subject.publish
end
context "when inside a request cycle" do
before do
subject.context.instance_variable_set(:@request, double('Request', headers: 'true'))
end
it "pushes the experiment knowledge into the client using Gon" do
expect(Gon).to receive(:push).with({ experiment: { 'namespaced/stub' => subject.signature } }, true)
it "pushes the experiment knowledge into the client using Gon" do
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
subject.publish
end
context "when outside a request cycle" do
it "does not push to gon when outside request cycle" do
expect(Gon).not_to receive(:push)
it "handles when Gon raises exceptions (like when it can't be pushed into)" do
expect(Gon).to receive(:push).and_raise(NoMethodError)
subject.publish(nil)
end
expect { subject.publish }.not_to raise_error
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