Skip to content

Commit 9812af8

Browse files
mfahadahmedmikeproeng37
authored andcommitted
fix(decision-service): Adds bucketing id in getVariationForRollout method. (#200)
1 parent cfea9ce commit 9812af8

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

packages/optimizely-sdk/lib/core/decision_service/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at
384384
};
385385
}
386386

387+
var bucketingId = this._getBucketingId(userId, attributes);
388+
387389
// The end index is length - 1 because the last experiment is assumed to be
388390
// "everyone else", which will be evaluated separately outside this loop
389391
var endIndex = rollout.experiments.length - 1;
@@ -401,7 +403,7 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at
401403
}
402404

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

420422
var everyoneElseExperiment = this.configObj.experimentKeyMap[rollout.experiments[endIndex].key];
421423
if (this.__checkIfUserIsInAudience(everyoneElseExperiment.key, userId, attributes)) {
422-
bucketerParams = this.__buildBucketerParams(everyoneElseExperiment.key, userId, userId);
424+
bucketerParams = this.__buildBucketerParams(everyoneElseExperiment.key, bucketingId, userId);
423425
variationId = bucketer.bucket(bucketerParams);
424426
variation = this.configObj.variationIdMap[variationId];
425427
if (variation) {

packages/optimizely-sdk/lib/core/decision_service/index.tests.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,5 +1356,47 @@ describe('lib/core/decision_service', function() {
13561356
});
13571357
});
13581358
});
1359+
1360+
describe('_getVariationForRollout', function() {
1361+
var feature;
1362+
var configObj;
1363+
var decisionService;
1364+
var __buildBucketerParamsSpy;
1365+
1366+
beforeEach(function() {
1367+
configObj = projectConfig.createProjectConfig(testDataWithFeatures);
1368+
feature = configObj.featureKeyMap.test_feature;
1369+
decisionService = DecisionService.createDecisionService({
1370+
configObj: configObj,
1371+
logger: logger.createLogger({logLevel: LOG_LEVEL.INFO}),
1372+
});
1373+
__buildBucketerParamsSpy = sinon.spy(decisionService, '__buildBucketerParams');
1374+
});
1375+
1376+
afterEach(function() {
1377+
__buildBucketerParamsSpy.restore();
1378+
});
1379+
1380+
it('should call __buildBucketerParams with user Id when bucketing Id is not provided in the attributes', function () {
1381+
var attributes = { test_attribute: 'test_value' };
1382+
decisionService._getVariationForRollout(feature, 'testUser', attributes);
1383+
1384+
sinon.assert.callCount(__buildBucketerParamsSpy, 2);
1385+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594031', 'testUser', 'testUser');
1386+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594037', 'testUser', 'testUser');
1387+
});
1388+
1389+
it('should call __buildBucketerParams with bucketing Id when bucketing Id is provided in the attributes', function () {
1390+
var attributes = {
1391+
test_attribute: 'test_value',
1392+
$opt_bucketing_id: 'abcdefg'
1393+
};
1394+
decisionService._getVariationForRollout(feature, 'testUser', attributes);
1395+
1396+
sinon.assert.callCount(__buildBucketerParamsSpy, 2);
1397+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594031', 'abcdefg', 'testUser');
1398+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594037', 'abcdefg', 'testUser');
1399+
});
1400+
});
13591401
});
13601402
});

0 commit comments

Comments
 (0)