Skip to content

Commit 69dbde1

Browse files
authored
fix (datafile management): Clear timeout created in onReady method when it's no longer needed (#272)
Summary: onReady sets a timeout that fulfills a timeout promise. The timeout promise is only used to race against the ready promise provided by ProjectConfigManager. So, as soon as ProjectConfigManager's ready promise is fulfilled, the timeout can be cleared. Before this change, the timeout would only be cleared in the close method. Test plan: Manual & added unit test
1 parent 057a219 commit 69dbde1

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

packages/optimizely-sdk/CHANGELOG.MD

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
Changes that have landed but are not yet released.
99

10+
### Bug Fixes:
11+
- Clear timeout created in onReady call for timeout promise as soon as project config manager's ready promise fulfills
12+
1013
### New Features
1114
- Added 60 second timeout for all datafile requests
1215

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

+8
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,14 @@ Optimizely.prototype.onReady = function(options) {
936936
onClose: onClose,
937937
};
938938

939+
this.__readyPromise.then(function() {
940+
clearTimeout(readyTimeout);
941+
delete this.__readyTimeouts[timeoutId];
942+
resolveTimeoutPromise({
943+
success: true,
944+
});
945+
}.bind(this));
946+
939947
return Promise.race([this.__readyPromise, timeoutPromise]);
940948
};
941949

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

+32
Original file line numberDiff line numberDiff line change
@@ -4831,11 +4831,17 @@ describe('lib/optimizely', function() {
48314831

48324832
describe('onReady method', function() {
48334833
var clock;
4834+
var setTimeoutSpy;
4835+
var clearTimeoutSpy;
48344836
beforeEach(function() {
48354837
clock = sinon.useFakeTimers(new Date().getTime());
4838+
setTimeoutSpy = sinon.spy(clock, 'setTimeout');
4839+
clearTimeoutSpy = sinon.spy(clock, 'clearTimeout');
48364840
});
48374841

48384842
afterEach(function() {
4843+
setTimeoutSpy.restore();
4844+
clearTimeoutSpy.restore();
48394845
clock.restore();
48404846
});
48414847

@@ -4944,6 +4950,32 @@ describe('lib/optimizely', function() {
49444950
return readyPromise3;
49454951
});
49464952
});
4953+
4954+
it('clears the timeout when the project config manager ready promise fulfills', function() {
4955+
projectConfigManager.ProjectConfigManager.callsFake(function(config) {
4956+
return {
4957+
stop: sinon.stub(),
4958+
getConfig: sinon.stub().returns(null),
4959+
onUpdate: sinon.stub().returns(function() {}),
4960+
onReady: sinon.stub().returns(Promise.resolve({ success: true })),
4961+
};
4962+
});
4963+
optlyInstance = new Optimizely({
4964+
clientEngine: 'node-sdk',
4965+
errorHandler: errorHandler,
4966+
eventDispatcher: eventDispatcher,
4967+
jsonSchemaValidator: jsonSchemaValidator,
4968+
logger: createdLogger,
4969+
sdkKey: '12345',
4970+
isValidInstance: true,
4971+
});
4972+
return optlyInstance.onReady().then(function() {
4973+
sinon.assert.calledOnce(clock.setTimeout);
4974+
var timeout = clock.setTimeout.getCall(0).returnValue;
4975+
sinon.assert.calledOnce(clock.clearTimeout);
4976+
sinon.assert.calledWithExactly(clock.clearTimeout, timeout);
4977+
});
4978+
});
49474979
});
49484980

49494981
describe('project config updates', function() {

0 commit comments

Comments
 (0)