Skip to content

Commit d6c3276

Browse files
mfahadahmedoakbani
authored andcommitted
fix(decision-service): Adds bucketing id in getVariationForRollout method. (#200)
(cherry picked from commit 9812af8)
1 parent 661ca6b commit d6c3276

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
@@ -383,6 +383,8 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at
383383
};
384384
}
385385

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

402404
this.logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.USER_MEETS_CONDITIONS_FOR_TARGETING_RULE, MODULE_NAME, userId, index + 1));
403-
bucketerParams = this.__buildBucketerParams(experiment.key, userId, userId);
405+
bucketerParams = this.__buildBucketerParams(experiment.key, bucketingId, userId);
404406
variationId = bucketer.bucket(bucketerParams);
405407
variation = this.configObj.variationIdMap[variationId];
406408
if (variation) {
@@ -418,7 +420,7 @@ DecisionService.prototype._getVariationForRollout = function(feature, userId, at
418420

419421
var everyoneElseExperiment = this.configObj.experimentKeyMap[rollout.experiments[endIndex].key];
420422
if (this.__checkIfUserIsInAudience(everyoneElseExperiment.key, userId, attributes)) {
421-
bucketerParams = this.__buildBucketerParams(everyoneElseExperiment.key, userId, userId);
423+
bucketerParams = this.__buildBucketerParams(everyoneElseExperiment.key, bucketingId, userId);
422424
variationId = bucketer.bucket(bucketerParams);
423425
variation = this.configObj.variationIdMap[variationId];
424426
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
@@ -1355,5 +1355,47 @@ describe('lib/core/decision_service', function() {
13551355
});
13561356
});
13571357
});
1358+
1359+
describe('_getVariationForRollout', function() {
1360+
var feature;
1361+
var configObj;
1362+
var decisionService;
1363+
var __buildBucketerParamsSpy;
1364+
1365+
beforeEach(function() {
1366+
configObj = projectConfig.createProjectConfig(testDataWithFeatures);
1367+
feature = configObj.featureKeyMap.test_feature;
1368+
decisionService = DecisionService.createDecisionService({
1369+
configObj: configObj,
1370+
logger: logger.createLogger({logLevel: LOG_LEVEL.INFO}),
1371+
});
1372+
__buildBucketerParamsSpy = sinon.spy(decisionService, '__buildBucketerParams');
1373+
});
1374+
1375+
afterEach(function() {
1376+
__buildBucketerParamsSpy.restore();
1377+
});
1378+
1379+
it('should call __buildBucketerParams with user Id when bucketing Id is not provided in the attributes', function () {
1380+
var attributes = { test_attribute: 'test_value' };
1381+
decisionService._getVariationForRollout(feature, 'testUser', attributes);
1382+
1383+
sinon.assert.callCount(__buildBucketerParamsSpy, 2);
1384+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594031', 'testUser', 'testUser');
1385+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594037', 'testUser', 'testUser');
1386+
});
1387+
1388+
it('should call __buildBucketerParams with bucketing Id when bucketing Id is provided in the attributes', function () {
1389+
var attributes = {
1390+
test_attribute: 'test_value',
1391+
$opt_bucketing_id: 'abcdefg'
1392+
};
1393+
decisionService._getVariationForRollout(feature, 'testUser', attributes);
1394+
1395+
sinon.assert.callCount(__buildBucketerParamsSpy, 2);
1396+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594031', 'abcdefg', 'testUser');
1397+
sinon.assert.calledWithExactly(__buildBucketerParamsSpy, '594037', 'abcdefg', 'testUser');
1398+
});
1399+
});
13581400
});
13591401
});

0 commit comments

Comments
 (0)