Skip to content

fix (datafile management): Clear timeout created in onReady method when it's no longer needed #272

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 4 commits into from
May 16, 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
3 changes: 3 additions & 0 deletions packages/optimizely-sdk/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
Changes that have landed but are not yet released.

### Bug Fixes:
- Clear timeout created in onReady call for timeout promise as soon as project config manager's ready promise fulfills

### New Features
- Added 60 second timeout for all datafile requests

Expand Down
8 changes: 8 additions & 0 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,14 @@ Optimizely.prototype.onReady = function(options) {
onClose: onClose,
};

this.__readyPromise.then(function() {
clearTimeout(readyTimeout);
delete this.__readyTimeouts[timeoutId];
resolveTimeoutPromise({
success: true,
});
}.bind(this));

return Promise.race([this.__readyPromise, timeoutPromise]);
};

Expand Down
32 changes: 32 additions & 0 deletions packages/optimizely-sdk/lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4831,11 +4831,17 @@ describe('lib/optimizely', function() {

describe('onReady method', function() {
var clock;
var setTimeoutSpy;
var clearTimeoutSpy;
beforeEach(function() {
clock = sinon.useFakeTimers(new Date().getTime());
setTimeoutSpy = sinon.spy(clock, 'setTimeout');
clearTimeoutSpy = sinon.spy(clock, 'clearTimeout');
});

afterEach(function() {
setTimeoutSpy.restore();
clearTimeoutSpy.restore();
clock.restore();
});

Expand Down Expand Up @@ -4944,6 +4950,32 @@ describe('lib/optimizely', function() {
return readyPromise3;
});
});

it('clears the timeout when the project config manager ready promise fulfills', function() {
projectConfigManager.ProjectConfigManager.callsFake(function(config) {
return {
stop: sinon.stub(),
getConfig: sinon.stub().returns(null),
onUpdate: sinon.stub().returns(function() {}),
onReady: sinon.stub().returns(Promise.resolve({ success: true })),
};
});
optlyInstance = new Optimizely({
clientEngine: 'node-sdk',
errorHandler: errorHandler,
eventDispatcher: eventDispatcher,
jsonSchemaValidator: jsonSchemaValidator,
logger: createdLogger,
sdkKey: '12345',
isValidInstance: true,
});
return optlyInstance.onReady().then(function() {
sinon.assert.calledOnce(clock.setTimeout);
var timeout = clock.setTimeout.getCall(0).returnValue;
sinon.assert.calledOnce(clock.clearTimeout);
sinon.assert.calledWithExactly(clock.clearTimeout, timeout);
});
});
});

describe('project config updates', function() {
Expand Down