Skip to content

Commit 2dd7820

Browse files
Add test to validate clearOnInit config
1 parent 42cf24d commit 2dd7820

File tree

1 file changed

+86
-10
lines changed

1 file changed

+86
-10
lines changed

src/__tests__/browserSuites/ready-from-cache.spec.js

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import sinon from 'sinon';
2+
import { nearlyEqual } from '../testUtils';
13
import { getStorageHash } from '@splitsoftware/splitio-commons/src/storages/KeyBuilder';
24
import { SplitFactory } from '../../';
35

46
import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
57
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';
68
import membershipsNicolas from '../mocks/memberships.nicolas@split.io.json';
79

8-
import { nearlyEqual } from '../testUtils';
910

1011
const DEFAULT_CACHE_EXPIRATION_IN_MILLIS = 864000000; // 10 days
1112

@@ -179,7 +180,6 @@ export default function (fetchMock, assert) {
179180
readyTimeout: 0.85
180181
},
181182
urls: testUrls,
182-
debug: true
183183
});
184184
const client = splitio.client();
185185
const client2 = splitio.client('nicolas2@split.io');
@@ -290,7 +290,6 @@ export default function (fetchMock, assert) {
290290
readyTimeout: 0.85
291291
},
292292
urls: testUrls,
293-
debug: true
294293
});
295294
const client = splitio.client();
296295
const client2 = splitio.client('nicolas2@split.io');
@@ -410,7 +409,6 @@ export default function (fetchMock, assert) {
410409
readyTimeout: 0.85
411410
},
412411
urls: testUrls,
413-
debug: true
414412
});
415413
const client = splitio.client();
416414
const client2 = splitio.client('nicolas2@split.io');
@@ -509,7 +507,6 @@ export default function (fetchMock, assert) {
509507
sync: {
510508
splitFilters: [{ type: 'byName', values: ['p2__split', 'p1__split'] }, { type: 'byName', values: ['p2__split', null] }]
511509
},
512-
debug: true
513510
});
514511
const client = splitio.client();
515512
const manager = splitio.manager();
@@ -555,7 +552,6 @@ export default function (fetchMock, assert) {
555552
sync: {
556553
splitFilters: [{ type: 'byName', values: ['p2__split', 'p1__split'] }, { type: 'byName', values: ['p2__split', null] }]
557554
},
558-
debug: true
559555
});
560556
const client = splitio.client();
561557
const manager = splitio.manager();
@@ -606,7 +602,6 @@ export default function (fetchMock, assert) {
606602
sync: {
607603
splitFilters: [{ type: 'byName', values: [undefined, true, 'p2__split'] }, { type: 'byPrefix', values: ['p1'] }, { type: 'byName', values: ['p2__split'] }]
608604
},
609-
debug: true
610605
});
611606
const client = splitio.client();
612607
const manager = splitio.manager();
@@ -659,7 +654,6 @@ export default function (fetchMock, assert) {
659654
sync: {
660655
splitFilters: [{ type: 'byPrefix', values: ['p2'] }, { type: 'byPrefix', values: ['p1', ''] }, { type: '', values: [] }, {}, { type: 'byPrefix' }]
661656
},
662-
debug: true
663657
});
664658
const client = splitio.client();
665659
const manager = splitio.manager();
@@ -722,7 +716,6 @@ export default function (fetchMock, assert) {
722716
},
723717
urls: testUrls,
724718
sync: syncParam,
725-
debug: true
726719
});
727720
const client = splitio.client();
728721
const manager = splitio.manager();
@@ -777,7 +770,6 @@ export default function (fetchMock, assert) {
777770
sync: {
778771
splitFilters: [{ type: 'byName', values: ['p3__split'] }, { type: 'byPrefix', values: [' p2', ' p2', ' p2', ' p2', 'no exist trim '] }, { type: 'byName', values: ['no_exist', ' no exist trim '] }]
779772
},
780-
debug: true
781773
});
782774
const client = splitio.client();
783775
const manager = splitio.manager();
@@ -796,4 +788,88 @@ export default function (fetchMock, assert) {
796788
});
797789
});
798790

791+
assert.test(async t => { // Testing clearOnInit config true
792+
sinon.spy(console, 'log');
793+
794+
const testUrls = {
795+
sdk: 'https://sdk.baseurl/readyFromCache_10',
796+
events: 'https://events.baseurl/readyFromCache_10'
797+
};
798+
const clearOnInitConfig = {
799+
...baseConfig,
800+
storage: {
801+
type: 'LOCALSTORAGE',
802+
prefix: 'readyFromCache_10',
803+
clearOnInit: true
804+
},
805+
urls: testUrls,
806+
debug: true
807+
};
808+
809+
// Start with cached data but without lastClear item (JS SDK below 11.1.0) -> cache cleanup
810+
localStorage.clear();
811+
localStorage.setItem('readyFromCache_10.SPLITIO.splits.till', 25);
812+
localStorage.setItem('readyFromCache_10.SPLITIO.split.p1__split', JSON.stringify(splitDeclarations.p1__split));
813+
localStorage.setItem('readyFromCache_10.SPLITIO.split.p2__split', JSON.stringify(splitDeclarations.p2__split));
814+
localStorage.setItem('readyFromCache_10.SPLITIO.hash', expectedHashNullFilter);
815+
816+
fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1', { status: 200, body: splitChangesMock1 });
817+
fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 });
818+
fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', { status: 200, body: { ms: {} } });
819+
820+
let splitio = SplitFactory(clearOnInitConfig);
821+
let client = splitio.client();
822+
let manager = splitio.manager();
823+
824+
t.true(console.log.calledWithMatch('clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache'), 'It should log a message about cleaning up cache');
825+
826+
client.once(client.Event.SDK_READY_FROM_CACHE, () => t.fail('It should not emit SDK_READY_FROM_CACHE because clearOnInit is true.'));
827+
828+
await client.ready();
829+
t.equal(manager.names().sort().length, 32, 'active splits should be present for evaluation');
830+
831+
await splitio.destroy();
832+
t.equal(localStorage.getItem('readyFromCache_10.SPLITIO.splits.till'), '1457552620999', 'splits.till must correspond to the till of the last successfully fetched Splits');
833+
t.equal(localStorage.getItem('readyFromCache_10.SPLITIO.hash'), expectedHashNullFilter, 'Storage hash must not be changed');
834+
t.true(nearlyEqual(parseInt(localStorage.getItem('readyFromCache_10.SPLITIO.lastClear')), Date.now()), 'lastClear timestamp must be set');
835+
836+
// Start again with cached data and lastClear item within the last 24 hours -> no cache cleanup
837+
console.log.resetHistory();
838+
fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 });
839+
840+
splitio = SplitFactory(clearOnInitConfig);
841+
client = splitio.client();
842+
manager = splitio.manager();
843+
844+
await new Promise(res => client.once(client.Event.SDK_READY_FROM_CACHE, res));
845+
846+
t.equal(manager.names().sort().length, 32, 'active splits should be present for evaluation');
847+
t.false(console.log.calledWithMatch('clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache'), 'It should log a message about cleaning up cache');
848+
849+
await splitio.destroy();
850+
851+
852+
// Start again with cached data and lastClear item older than 24 hours -> cache cleanup
853+
console.log.resetHistory();
854+
localStorage.setItem('readyFromCache_10.SPLITIO.lastClear', Date.now() - 25 * 60 * 60 * 1000); // 25 hours ago
855+
fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1', { status: 200, body: splitChangesMock1 });
856+
fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 });
857+
858+
splitio = SplitFactory(clearOnInitConfig);
859+
client = splitio.client();
860+
manager = splitio.manager();
861+
862+
client.once(client.Event.SDK_READY_FROM_CACHE, () => t.fail('It should not emit SDK_READY_FROM_CACHE because clearOnInit is true.'));
863+
864+
await new Promise(res => client.once(client.Event.SDK_READY, res));
865+
866+
t.equal(manager.names().sort().length, 32, 'active splits should be present for evaluation');
867+
t.true(console.log.calledWithMatch('clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache'), 'It should log a message about cleaning up cache');
868+
869+
await splitio.destroy();
870+
871+
console.log.restore();
872+
t.end();
873+
});
874+
799875
}

0 commit comments

Comments
 (0)