Skip to content
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

[FSSDK-11113] make Optimizely class instance of Service interface #999

Merged
merged 9 commits into from
Feb 5, 2025
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
4 changes: 2 additions & 2 deletions lib/event_processor/forwarding_event_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { buildLogEvent } from './event_builder/log_event';
import { BaseService, ServiceState } from '../service';
import { EventEmitter } from '../utils/event_emitter/event_emitter';
import { Consumer, Fn } from '../utils/type';
import { SERVICE_STOPPED_BEFORE_IT_WAS_STARTED } from 'error_message';
import { SERVICE_STOPPED_BEFORE_RUNNING } from 'error_message';
import { OptimizelyError } from '../error/optimizly_error';
class ForwardingEventProcessor extends BaseService implements EventProcessor {
private dispatcher: EventDispatcher;
Expand Down Expand Up @@ -56,7 +56,7 @@ class ForwardingEventProcessor extends BaseService implements EventProcessor {
}

if (this.isNew()) {
this.startPromise.reject(new OptimizelyError(SERVICE_STOPPED_BEFORE_IT_WAS_STARTED));
this.startPromise.reject(new OptimizelyError(SERVICE_STOPPED_BEFORE_RUNNING));
}

this.state = ServiceState.Terminated;
Expand Down
2 changes: 1 addition & 1 deletion lib/message/error_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const DATAFILE_MANAGER_STOPPED = 'Datafile manager stopped before it coul
export const FAILED_TO_FETCH_DATAFILE = 'Failed to fetch datafile';
export const NO_SDKKEY_OR_DATAFILE = 'At least one of sdkKey or datafile must be provided';
export const RETRY_CANCELLED = 'Retry cancelled';
export const SERVICE_STOPPED_BEFORE_IT_WAS_STARTED = 'Service stopped before it was started';
export const SERVICE_STOPPED_BEFORE_RUNNING = 'Service stopped before running';
export const ONLY_POST_REQUESTS_ARE_SUPPORTED = 'Only POST requests are supported';
export const SEND_BEACON_FAILED = 'sendBeacon failed';
export const FAILED_TO_DISPATCH_EVENTS = 'Failed to dispatch events'
Expand Down
57 changes: 26 additions & 31 deletions lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,15 @@ import {
FEATURE_NOT_ENABLED_FOR_USER,
INVALID_CLIENT_ENGINE,
INVALID_DEFAULT_DECIDE_OPTIONS,
INVALID_OBJECT,
NOT_ACTIVATING_USER,
USER_HAS_NO_FORCED_VARIATION,
USER_HAS_NO_FORCED_VARIATION_FOR_EXPERIMENT,
USER_MAPPED_TO_FORCED_VARIATION,
USER_RECEIVED_DEFAULT_VARIABLE_VALUE,
VALID_USER_PROFILE_SERVICE,
VARIATION_REMOVED_FOR_USER,
} from 'log_message';
import {
EXPERIMENT_KEY_NOT_IN_DATAFILE,
INVALID_ATTRIBUTES,
NOT_TRACKING_USER,
EVENT_KEY_NOT_FOUND,
INVALID_EXPERIMENT_KEY,
INVALID_INPUT_FORMAT,
NO_VARIATION_FOR_EXPERIMENT_KEY,
USER_NOT_IN_FORCED_VARIATION,
INSTANCE_CLOSED,
ONREADY_TIMEOUT,
SERVICE_STOPPED_BEFORE_RUNNING
} from 'error_message';

import {
Expand All @@ -77,6 +66,7 @@ import {
} from '../core/decision_service';

import { USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP } from '../core/bucketer';
import { resolvablePromise } from '../utils/promise/resolvablePromise';

var LOG_LEVEL = enums.LOG_LEVEL;
var DECISION_SOURCES = enums.DECISION_SOURCES;
Expand Down Expand Up @@ -9253,10 +9243,10 @@ describe('lib/optimizely', function() {
});
});

it('returns a promise that fulfills with a successful result object', function() {
return optlyInstance.close().then(function(result) {
assert.deepEqual(result, { success: true });
});
it('returns a promise that resolves', function() {
return optlyInstance.close().then().catch(() => {
assert.fail();
})
});
});

Expand Down Expand Up @@ -9291,13 +9281,11 @@ describe('lib/optimizely', function() {
});
});

it('returns a promise that fulfills with an unsuccessful result object', function() {
return optlyInstance.close().then(function(result) {
// assert.deepEqual(result, {
// success: false,
// reason: 'Error: Failed to stop',
// });
assert.isFalse(result.success);
it('returns a promise that rejects', function() {
return optlyInstance.close().then(() => {
assert.fail('promnise should reject')
}).catch(() => {

});
});
});
Expand Down Expand Up @@ -9465,7 +9453,7 @@ describe('lib/optimizely', function() {
var readyPromise = optlyInstance.onReady();
clock.tick(300001);
return readyPromise.then(() => {
return Promise.reject(new Error(PROMISE_SHOULD_NOT_HAVE_RESOLVED));
return Promise.reject(new Error('PROMISE_SHOULD_NOT_HAVE_RESOLVED'));
}, (err) => {
assert.equal(err.baseMessage, ONREADY_TIMEOUT);
assert.deepEqual(err.params, [ 30000 ]);
Expand All @@ -9487,18 +9475,25 @@ describe('lib/optimizely', function() {
eventProcessor,
});
var readyPromise = optlyInstance.onReady({ timeout: 100 });

optlyInstance.close();

return readyPromise.then(() => {
return Promise.reject(new Error(PROMISE_SHOULD_NOT_HAVE_RESOLVED));
return Promise.reject(new Error('PROMISE_SHOULD_NOT_HAVE_RESOLVED'));
}, (err) => {
assert.equal(err.baseMessage, INSTANCE_CLOSED);
assert.equal(err.baseMessage, SERVICE_STOPPED_BEFORE_RUNNING);
});
});

it('can be called several times with different timeout values and the returned promises behave correctly', function() {
const onRunning = resolvablePromise();

optlyInstance = new Optimizely({
clientEngine: 'node-sdk',
projectConfigManager: getMockProjectConfigManager(),
projectConfigManager: getMockProjectConfigManager({
onRunning: onRunning.promise,
}),

eventProcessor,
jsonSchemaValidator: jsonSchemaValidator,
logger: createdLogger,
Expand All @@ -9512,16 +9507,16 @@ describe('lib/optimizely', function() {
var readyPromise3 = optlyInstance.onReady({ timeout: 300 });
clock.tick(101);
return readyPromise1
.then(function() {
.catch(function() {
clock.tick(100);
return readyPromise2;
})
.then(function() {
.catch(function() {
// readyPromise3 has not resolved yet because only 201 ms have elapsed.
// Calling close on the instance should resolve readyPromise3
optlyInstance.close();
optlyInstance.close().catch(() => {});
return readyPromise3;
});
}).catch(() => {});
});

it('clears the timeout when the project config manager ready promise fulfills', function() {
Expand Down
Loading
Loading