Skip to content

fix(decision-service): Adds bucketing id in getVariationForRollout method. #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/optimizely-sdk/lib/core/decision_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at
};
}

var bucketingId = this._getBucketingId(userId, attributes);

// The end index is length - 1 because the last experiment is assumed to be
// "everyone else", which will be evaluated separately outside this loop
var endIndex = rollout.experiments.length - 1;
Expand All @@ -401,7 +403,7 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at
}

this.logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.USER_MEETS_CONDITIONS_FOR_TARGETING_RULE, MODULE_NAME, userId, index + 1));
bucketerParams = this.__buildBucketerParams(experiment.key, userId, userId);
bucketerParams = this.__buildBucketerParams(experiment.key, bucketingId, userId);
variationId = bucketer.bucket(bucketerParams);
variation = this.configObj.variationIdMap[variationId];
if (variation) {
Expand All @@ -419,7 +421,7 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at

var everyoneElseExperiment = this.configObj.experimentKeyMap[rollout.experiments[endIndex].key];
if (this.__checkIfUserIsInAudience(everyoneElseExperiment.key, userId, attributes)) {
bucketerParams = this.__buildBucketerParams(everyoneElseExperiment.key, userId, userId);
bucketerParams = this.__buildBucketerParams(everyoneElseExperiment.key, bucketingId, userId);
variationId = bucketer.bucket(bucketerParams);
variation = this.configObj.variationIdMap[variationId];
if (variation) {
Expand Down
42 changes: 42 additions & 0 deletions packages/optimizely-sdk/lib/core/decision_service/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1356,5 +1356,47 @@ describe('lib/core/decision_service', function() {
});
});
});

describe('_getVariationForRollout', function() {
var feature;
var configObj;
var decisionService;
var __buildBucketerParamsSpy;

beforeEach(function() {
configObj = projectConfig.createProjectConfig(testDataWithFeatures);
feature = configObj.featureKeyMap.test_feature;
decisionService = DecisionService.createDecisionService({
configObj: configObj,
logger: logger.createLogger({logLevel: LOG_LEVEL.INFO}),
});
__buildBucketerParamsSpy = sinon.spy(decisionService, '__buildBucketerParams');
});

afterEach(function() {
__buildBucketerParamsSpy.restore();
});

it('should call __buildBucketerParams with user Id when bucketing Id is not provided in the attributes', function () {
var attributes = { test_attribute: 'test_value' };
decisionService._getVariationForRollout(feature, 'testUser', attributes);

sinon.assert.callCount(__buildBucketerParamsSpy, 2);
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594031', 'testUser', 'testUser');
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594037', 'testUser', 'testUser');
});

it('should call __buildBucketerParams with bucketing Id when bucketing Id is provided in the attributes', function () {
var attributes = {
test_attribute: 'test_value',
$opt_bucketing_id: 'abcdefg'
};
decisionService._getVariationForRollout(feature, 'testUser', attributes);

sinon.assert.callCount(__buildBucketerParamsSpy, 2);
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594031', 'abcdefg', 'testUser');
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594037', 'abcdefg', 'testUser');
});
});
});
});