Skip to content

Commit

Permalink
Add test for InMemoryStickyBucketService
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed Apr 12, 2024
1 parent 0e1ecef commit f7cdbb3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 1 addition & 4 deletions lib/growthbook/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,7 @@ def refresh_sticky_buckets(force: false)
return unless @sticky_bucket_service

attributes = _get_sticky_bucket_attributes
if !force && attributes == @sticky_bucket_attributes
logger.debug('Skipping refresh of sticky bucket assignments, no changes')
return
end
return if !force && attributes == @sticky_bucket_attributes

@sticky_bucket_attributes = attributes
@sticky_bucket_assignment_docs = @sticky_bucket_service.get_all_assignments(attributes)
Expand Down
77 changes: 77 additions & 0 deletions spec/growthbook/in_memory_sticky_bucket_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require_relative '../spec_helper'
require 'growthbook'
require 'json'

describe Growthbook::InMemoryStickyBucketService do
# Start forcing everyone to variation1
features = {
'feature' => {
'defaultValue' => 5,
'rules' => [{
'key' => 'exp',
'variations' => [0, 1],
'weights' => [0, 1],
'meta' => [
{ 'key' => 'control' },
{ 'key' => 'variation1' }
]
}]
}
}

it 'gets and saves assignments' do
service = described_class.new
gb = Growthbook::Context.new(
sticky_bucket_service: service,
attributes: {
'id' => '1'
},
features: features
)

expect(gb.feature_value('feature', -1)).to eq(1)
expect(service.get_assignments('id', '1')).to eq(
{
'attributeName' => 'id',
'attributeValue' => '1',
'assignments' => {
'exp__0' => 'variation1'
}
}
)

features['feature']['rules'][0]['weights'] = [1, 0]
gb.features = features
expect(gb.feature_value('feature', -1)).to eq(1)

gb2 = Growthbook::Context.new(
sticky_bucket_service: service,
attributes: {
'id' => '1'
},
features: features
)
expect(gb2.feature_value('feature', -1)).to eq(1)

gb.attributes = { 'id' => '2' }
expect(gb.feature_value('feature', -1)).to eq(0)

gb.attributes = { 'id' => '1' }
features['feature']['rules'][0]['bucketVersion'] = 1
gb.features = features
expect(gb.feature_value('feature', -1)).to eq(0)

expect(service.get_assignments('id', '1')).to eq(
{
'attributeName' => 'id',
'attributeValue' => '1',
'assignments' => {
'exp__0' => 'variation1',
'exp__1' => 'control'
}
}
)
end
end

0 comments on commit f7cdbb3

Please sign in to comment.