-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* upgrade robocop * Add SplitRegistry model with sampling weight support increase brittleness if env var retrieval impl changes * V2 split registries controller with sampling weight Document EXPERIENCE_SAMPLING_WEIGHT * Expand split representation * correct var name
- Loading branch information
Showing
13 changed files
with
184 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Api::V2::SplitRegistriesController < UnauthenticatedApiController | ||
include CorsSupport | ||
|
||
def show | ||
@split_registry = SplitRegistry.instance | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class SplitRegistry | ||
include Singleton | ||
|
||
def splits | ||
Split.active | ||
end | ||
|
||
def experience_sampling_weight | ||
@experience_sampling_weight ||= _experience_sampling_weight | ||
end | ||
|
||
private | ||
|
||
def _experience_sampling_weight | ||
Integer(ENV.fetch('EXPERIENCE_SAMPLING_WEIGHT', '1')).tap do |weight| | ||
raise <<~TEXT if weight.negative? | ||
EXPERIENCE_SAMPLING_WEIGHT, if specified, must be greater than or equal to 0. Use 0 to disable experience events. | ||
TEXT | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
json.splits do | ||
@split_registry.splits.each do |split| | ||
json.set! split.name do | ||
json.weights split.registry | ||
json.feature_gate split.feature_gate? | ||
end | ||
end | ||
json.merge!({}) | ||
end | ||
json.(@split_registry, :experience_sampling_weight) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
spec/controllers/api/v2/split_registries_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Api::V2::SplitRegistriesController, type: :controller do | ||
let(:split_1) { FactoryBot.create :split, name: "one", finished_at: Time.zone.now, registry: { all: 100 } } | ||
let(:split_2) { FactoryBot.create :split, name: "two", registry: { on: 50, off: 50 } } | ||
let(:split_3) { FactoryBot.create :split, name: "three_enabled", registry: { true: 99, false: 1 }, feature_gate: true } | ||
|
||
describe "#show" do | ||
before do | ||
allow(SplitRegistry.instance).to receive(:experience_sampling_weight).and_return(10) | ||
end | ||
|
||
it "includes sampling weight" do | ||
get :show | ||
expect(response).to have_http_status :ok | ||
expect(response_json['experience_sampling_weight']).to eq(10) | ||
end | ||
|
||
it "returns empty with no active splits" do | ||
get :show | ||
expect(response).to have_http_status :ok | ||
expect(response_json['splits']).to eq({}) | ||
end | ||
|
||
it "returns the full split registry" do | ||
expect(split_1).to be_finished | ||
expect(split_2).not_to be_finished | ||
expect(split_3).not_to be_finished | ||
|
||
get :show | ||
|
||
expect(response).to have_http_status :ok | ||
expect(response_json['splits']).to eq( | ||
"two" => { | ||
"weights" => { "on" => 50, "off" => 50 }, | ||
"feature_gate" => false | ||
}, | ||
"three_enabled" => { | ||
"weights" => { "true" => 99, "false" => 1 }, | ||
"feature_gate" => true | ||
} | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe SplitRegistry do | ||
subject { SplitRegistry.instance } | ||
|
||
describe "#splits" do | ||
it "doesn't cache the instance" do | ||
expect(subject.splits).to eq(subject.splits) | ||
expect(subject.splits).not_to eql(subject.splits) | ||
end | ||
|
||
it "returns active splits" do | ||
split = FactoryBot.create(:split) | ||
|
||
expect(subject.splits.all).to include(split) | ||
end | ||
|
||
it "doesn't return inactive splits" do | ||
split = FactoryBot.create(:split, finished_at: Time.zone.now) | ||
|
||
expect(subject.splits.all).not_to include(split) | ||
end | ||
end | ||
|
||
describe "#experience_sampling_weight" do | ||
context "bypassing singleton memoization" do | ||
subject { SplitRegistry.send(:new) } | ||
|
||
it "memoizes the env var fetch" do | ||
allow(ENV).to receive(:fetch).and_call_original | ||
|
||
subject.experience_sampling_weight | ||
subject.experience_sampling_weight | ||
|
||
expect(ENV).to have_received(:fetch).with('EXPERIENCE_SAMPLING_WEIGHT', any_args).exactly(:once) | ||
end | ||
|
||
it "returns 1 with no env var" do | ||
with_env EXPERIENCE_SAMPLING_WEIGHT: nil do | ||
expect(subject.experience_sampling_weight).to eq 1 | ||
end | ||
end | ||
|
||
it "returns an positive value specified" do | ||
with_env EXPERIENCE_SAMPLING_WEIGHT: '10' do | ||
expect(subject.experience_sampling_weight).to eq 10 | ||
end | ||
end | ||
|
||
it "returns zero when specified" do | ||
with_env EXPERIENCE_SAMPLING_WEIGHT: '0' do | ||
expect(subject.experience_sampling_weight).to eq 0 | ||
end | ||
end | ||
|
||
it "blows up on negative" do | ||
with_env EXPERIENCE_SAMPLING_WEIGHT: '-1' do | ||
expect { subject.experience_sampling_weight }.to raise_error(/greater than or equal/) | ||
end | ||
end | ||
end | ||
end | ||
end |