Skip to content

Commit

Permalink
Add tests for lazy init
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Oct 5, 2024
1 parent 58f5594 commit 0107c1c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
10.29.0 (September XX, 2024)
- Added `factory.getState()` method for standalone server-side SDKs, which returns the rollout plan snapshot from the storage.
- Added `preloadedData` configuration option for standalone client-side SDKs, which allows preloading the SDK storage with a snapshot of the rollout plan.
- Added `factory.destroy()` method, which invokes the `destroy` method on all SDK clients created by the factory.
- Updated @splitsoftware/splitio-commons package to version 1.18.0 that includes minor updates:
- Added support for targeting rules based on large segments for browsers.
Expand Down
89 changes: 89 additions & 0 deletions src/__tests__/nodeSuites/lazy-init.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { SplitFactory as SplitFactorySS } from '../../factory/node';
import { SplitFactory as SplitFactoryCS } from '../../factory/browser';

// Tests should finish without dangling timers or requests
export default function (settings, fetchMock, t) {

t.test('Server-side', async (assert) => {
let splitio;

for (let i = 0; i < 100; i++) {
splitio = SplitFactorySS({
core: {
authorizationKey: 'fake-token-' + i,
},
urls: {
sdk: 'https://not-called/api',
events: 'https://not-called/api',
auth: 'https://not-called/api',
}
}, (modules) => {
modules.lazyInit = true;
});

const manager = splitio.manager();
assert.deepEqual(manager.names(), [], 'We should not have done any request yet');

const client = splitio.client();
assert.equal(client.getTreatment('user-1', 'split_test'), 'control', 'We should get control');
assert.equal(client.track('user-1', 'user', 'my_event'), true, 'We should track the event');
}

fetchMock.getOnce('https://not-called/api/splitChanges?s=1.1&since=-1', { status: 200, body: { splits: [], since: -1, till: 1457552620999 } });
fetchMock.getOnce('https://not-called/api/splitChanges?s=1.1&since=1457552620999', { status: 200, body: { splits: [], since: 1457552620999, till: 1457552620999 } });
fetchMock.postOnce('https://not-called/api/testImpressions/bulk', 200);
fetchMock.postOnce('https://not-called/api/events/bulk', 200);

splitio.init();
await splitio.client().ready();
assert.true(splitio.client().__getStatus().isReady, 'Split SDK is ready');
await splitio.destroy();

assert.end();
});

t.test('Client-side', async (assert) => {
let splitio;

for (let i = 0; i < 100; i++) {
splitio = SplitFactoryCS({
core: {
authorizationKey: 'fake-token-' + i,
key: 'user-' + i,
},
urls: {
sdk: 'https://not-called/api',
events: 'https://not-called/api',
auth: 'https://not-called/api',
}
}, (modules) => {
modules.lazyInit = true;
});

const manager = splitio.manager();
assert.deepEqual(manager.names(), [], 'We should not have done any request yet');

const client = splitio.client();
assert.equal(client.getTreatment('split_test'), 'control', 'We should get control');
assert.equal(client.track('user', 'my_event'), true, 'We should track the event');

const otherClient = splitio.client('other-user');
assert.equal(otherClient.getTreatment('split_test'), 'control', 'We should get control');
assert.equal(otherClient.track('user', 'my_event'), true, 'We should track the event');
}

fetchMock.getOnce('https://not-called/api/splitChanges?s=1.2&since=-1', { status: 200, body: { splits: [], since: -1, till: 1457552620999 } });
fetchMock.getOnce('https://not-called/api/splitChanges?s=1.2&since=1457552620999', { status: 200, body: { splits: [], since: 1457552620999, till: 1457552620999 } });
fetchMock.getOnce('https://not-called/api/memberships/user-99', { status: 200, body: {} });
fetchMock.getOnce('https://not-called/api/memberships/other-user', { status: 200, body: {} });
fetchMock.postOnce('https://not-called/api/testImpressions/bulk', 200);
fetchMock.postOnce('https://not-called/api/events/bulk', 200);

splitio.init();
await splitio.client().ready();
assert.true(splitio.client().__getStatus().isReady, 'Split SDK is ready');
await splitio.destroy();

assert.end();
});
}
9 changes: 6 additions & 3 deletions src/__tests__/online/node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import fetchMock from '../testUtils/nodeFetchMock';
import { url } from '../testUtils';
import { settingsFactory } from '../../settings/node';

import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';

import evaluationsSuite from '../nodeSuites/evaluations.spec';
import evaluationsSemverSuite from '../nodeSuites/evaluations-semver.spec';
import eventsSuite from '../nodeSuites/events.spec';
Expand All @@ -18,10 +21,8 @@ import ipAddressesSettingDebug from '../nodeSuites/ip-addresses-setting.debug.sp
import readinessSuite from '../nodeSuites/readiness.spec';
import readyPromiseSuite from '../nodeSuites/ready-promise.spec';
import { fetchSpecificSplits, fetchSpecificSplitsForFlagSets } from '../nodeSuites/fetch-specific-splits.spec';

import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';
import flagSets from '../nodeSuites/flag-sets.spec';
import lazyInitSuite from '../nodeSuites/lazy-init.spec';

const config = {
core: {
Expand Down Expand Up @@ -94,5 +95,7 @@ tape('## Node JS - E2E CI Tests ##', async function (assert) {
/* Validate flag sets */
assert.test('E2E / Flag sets', flagSets.bind(null, fetchMock));

assert.test('E2E / SplitFactory with lazy init', lazyInitSuite.bind(null, settings, fetchMock));

assert.end();
});

0 comments on commit 0107c1c

Please sign in to comment.