Skip to content

Commit 21522ca

Browse files
authored
[FSSDK-11113] make Optimizely class instance of Service interface (#999)
1 parent bc49e3c commit 21522ca

File tree

8 files changed

+125
-162
lines changed

8 files changed

+125
-162
lines changed

lib/event_processor/forwarding_event_processor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { buildLogEvent } from './event_builder/log_event';
2323
import { BaseService, ServiceState } from '../service';
2424
import { EventEmitter } from '../utils/event_emitter/event_emitter';
2525
import { Consumer, Fn } from '../utils/type';
26-
import { SERVICE_STOPPED_BEFORE_IT_WAS_STARTED } from 'error_message';
26+
import { SERVICE_STOPPED_BEFORE_RUNNING } from 'error_message';
2727
import { OptimizelyError } from '../error/optimizly_error';
2828
class ForwardingEventProcessor extends BaseService implements EventProcessor {
2929
private dispatcher: EventDispatcher;
@@ -56,7 +56,7 @@ class ForwardingEventProcessor extends BaseService implements EventProcessor {
5656
}
5757

5858
if (this.isNew()) {
59-
this.startPromise.reject(new OptimizelyError(SERVICE_STOPPED_BEFORE_IT_WAS_STARTED));
59+
this.startPromise.reject(new OptimizelyError(SERVICE_STOPPED_BEFORE_RUNNING));
6060
}
6161

6262
this.state = ServiceState.Terminated;

lib/message/error_message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const DATAFILE_MANAGER_STOPPED = 'Datafile manager stopped before it coul
9595
export const FAILED_TO_FETCH_DATAFILE = 'Failed to fetch datafile';
9696
export const NO_SDKKEY_OR_DATAFILE = 'At least one of sdkKey or datafile must be provided';
9797
export const RETRY_CANCELLED = 'Retry cancelled';
98-
export const SERVICE_STOPPED_BEFORE_IT_WAS_STARTED = 'Service stopped before it was started';
98+
export const SERVICE_STOPPED_BEFORE_RUNNING = 'Service stopped before running';
9999
export const ONLY_POST_REQUESTS_ARE_SUPPORTED = 'Only POST requests are supported';
100100
export const SEND_BEACON_FAILED = 'sendBeacon failed';
101101
export const FAILED_TO_DISPATCH_EVENTS = 'Failed to dispatch events'

lib/optimizely/index.tests.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,15 @@ import {
3737
FEATURE_NOT_ENABLED_FOR_USER,
3838
INVALID_CLIENT_ENGINE,
3939
INVALID_DEFAULT_DECIDE_OPTIONS,
40-
INVALID_OBJECT,
4140
NOT_ACTIVATING_USER,
42-
USER_HAS_NO_FORCED_VARIATION,
43-
USER_HAS_NO_FORCED_VARIATION_FOR_EXPERIMENT,
44-
USER_MAPPED_TO_FORCED_VARIATION,
45-
USER_RECEIVED_DEFAULT_VARIABLE_VALUE,
4641
VALID_USER_PROFILE_SERVICE,
47-
VARIATION_REMOVED_FOR_USER,
4842
} from 'log_message';
4943
import {
50-
EXPERIMENT_KEY_NOT_IN_DATAFILE,
51-
INVALID_ATTRIBUTES,
5244
NOT_TRACKING_USER,
5345
EVENT_KEY_NOT_FOUND,
5446
INVALID_EXPERIMENT_KEY,
55-
INVALID_INPUT_FORMAT,
56-
NO_VARIATION_FOR_EXPERIMENT_KEY,
57-
USER_NOT_IN_FORCED_VARIATION,
58-
INSTANCE_CLOSED,
5947
ONREADY_TIMEOUT,
48+
SERVICE_STOPPED_BEFORE_RUNNING
6049
} from 'error_message';
6150

6251
import {
@@ -77,6 +66,7 @@ import {
7766
} from '../core/decision_service';
7867

7968
import { USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP } from '../core/bucketer';
69+
import { resolvablePromise } from '../utils/promise/resolvablePromise';
8070

8171
var LOG_LEVEL = enums.LOG_LEVEL;
8272
var DECISION_SOURCES = enums.DECISION_SOURCES;
@@ -9253,10 +9243,10 @@ describe('lib/optimizely', function() {
92539243
});
92549244
});
92559245

9256-
it('returns a promise that fulfills with a successful result object', function() {
9257-
return optlyInstance.close().then(function(result) {
9258-
assert.deepEqual(result, { success: true });
9259-
});
9246+
it('returns a promise that resolves', function() {
9247+
return optlyInstance.close().then().catch(() => {
9248+
assert.fail();
9249+
})
92609250
});
92619251
});
92629252

@@ -9291,13 +9281,11 @@ describe('lib/optimizely', function() {
92919281
});
92929282
});
92939283

9294-
it('returns a promise that fulfills with an unsuccessful result object', function() {
9295-
return optlyInstance.close().then(function(result) {
9296-
// assert.deepEqual(result, {
9297-
// success: false,
9298-
// reason: 'Error: Failed to stop',
9299-
// });
9300-
assert.isFalse(result.success);
9284+
it('returns a promise that rejects', function() {
9285+
return optlyInstance.close().then(() => {
9286+
assert.fail('promnise should reject')
9287+
}).catch(() => {
9288+
93019289
});
93029290
});
93039291
});
@@ -9465,7 +9453,7 @@ describe('lib/optimizely', function() {
94659453
var readyPromise = optlyInstance.onReady();
94669454
clock.tick(300001);
94679455
return readyPromise.then(() => {
9468-
return Promise.reject(new Error(PROMISE_SHOULD_NOT_HAVE_RESOLVED));
9456+
return Promise.reject(new Error('PROMISE_SHOULD_NOT_HAVE_RESOLVED'));
94699457
}, (err) => {
94709458
assert.equal(err.baseMessage, ONREADY_TIMEOUT);
94719459
assert.deepEqual(err.params, [ 30000 ]);
@@ -9487,18 +9475,25 @@ describe('lib/optimizely', function() {
94879475
eventProcessor,
94889476
});
94899477
var readyPromise = optlyInstance.onReady({ timeout: 100 });
9478+
94909479
optlyInstance.close();
9480+
94919481
return readyPromise.then(() => {
9492-
return Promise.reject(new Error(PROMISE_SHOULD_NOT_HAVE_RESOLVED));
9482+
return Promise.reject(new Error('PROMISE_SHOULD_NOT_HAVE_RESOLVED'));
94939483
}, (err) => {
9494-
assert.equal(err.baseMessage, INSTANCE_CLOSED);
9484+
assert.equal(err.baseMessage, SERVICE_STOPPED_BEFORE_RUNNING);
94959485
});
94969486
});
94979487

94989488
it('can be called several times with different timeout values and the returned promises behave correctly', function() {
9489+
const onRunning = resolvablePromise();
9490+
94999491
optlyInstance = new Optimizely({
95009492
clientEngine: 'node-sdk',
9501-
projectConfigManager: getMockProjectConfigManager(),
9493+
projectConfigManager: getMockProjectConfigManager({
9494+
onRunning: onRunning.promise,
9495+
}),
9496+
95029497
eventProcessor,
95039498
jsonSchemaValidator: jsonSchemaValidator,
95049499
logger: createdLogger,
@@ -9512,16 +9507,16 @@ describe('lib/optimizely', function() {
95129507
var readyPromise3 = optlyInstance.onReady({ timeout: 300 });
95139508
clock.tick(101);
95149509
return readyPromise1
9515-
.then(function() {
9510+
.catch(function() {
95169511
clock.tick(100);
95179512
return readyPromise2;
95189513
})
9519-
.then(function() {
9514+
.catch(function() {
95209515
// readyPromise3 has not resolved yet because only 201 ms have elapsed.
95219516
// Calling close on the instance should resolve readyPromise3
9522-
optlyInstance.close();
9517+
optlyInstance.close().catch(() => {});
95239518
return readyPromise3;
9524-
});
9519+
}).catch(() => {});
95259520
});
95269521

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

0 commit comments

Comments
 (0)