diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 57a5464ff..7621d4b33 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -19,8 +19,7 @@ permissions: jobs: build: name: Build - # @TODO rollback to ubuntu-latest eventually. ATM, `npm run test-browser` fails when using ubuntu-latest (ubuntu-22.04) with "ERROR [launcher]: Cannot start ChromeHeadless" - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index 57497ea78..a38349dbf 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -12,7 +12,7 @@ on: jobs: build: name: Build - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/.github/workflows/update-license-year.yml b/.github/workflows/update-license-year.yml index 199a0ef78..7e0a945f9 100644 --- a/.github/workflows/update-license-year.yml +++ b/.github/workflows/update-license-year.yml @@ -10,7 +10,7 @@ permissions: jobs: test: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 diff --git a/CHANGES.txt b/CHANGES.txt index 070dc76c4..fdbf0d362 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,10 @@ +11.2.0 (March 28, 2025) + - Added a new optional argument to the client `getTreatment` methods to allow passing additional evaluation options, such as a map of properties to append to the generated impressions sent to Split backend. Read more in our docs. + - Added two new configuration options for the SDK's `LOCALSTORAGE` storage type to control the behavior of the persisted rollout plan cache in the browser: + - `storage.expirationDays` to specify the validity period of the rollout plan cache in days. + - `storage.clearOnInit` to clear the rollout plan cache on SDK initialization. + - Updated SDK_READY_FROM_CACHE event when using the `LOCALSTORAGE` storage type to be emitted alongside the SDK_READY event if it has not already been emitted. + 11.1.0 (January 17, 2025) - Added support for the new impressions tracking toggle available on feature flags, both respecting the setting and including the new field being returned on `SplitView` type objects. Read more in our docs. - Updated @splitsoftware/splitio-commons package to version 2.1.0. diff --git a/README.md b/README.md index 77ad5b5fa..10a4f8e21 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Split has built and maintains SDKs for: * .NET [Github](https://github.com/splitio/dotnet-client) [Docs](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK) * Android [Github](https://github.com/splitio/android-client) [Docs](https://help.split.io/hc/en-us/articles/360020343291-Android-SDK) * Angular [Github](https://github.com/splitio/angular-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/6495326064397-Angular-utilities) +* Elixir thin-client [Github](https://github.com/splitio/elixir-thin-client) [Docs](https://help.split.io/hc/en-us/articles/26988707417869-Elixir-Thin-Client-SDK) * Flutter [Github](https://github.com/splitio/flutter-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/8096158017165-Flutter-plugin) * GO [Github](https://github.com/splitio/go-client) [Docs](https://help.split.io/hc/en-us/articles/360020093652-Go-SDK) * iOS [Github](https://github.com/splitio/ios-client) [Docs](https://help.split.io/hc/en-us/articles/360020401491-iOS-SDK) diff --git a/karma/config.js b/karma/config.js index fd7aea269..6789fbd1f 100644 --- a/karma/config.js +++ b/karma/config.js @@ -15,9 +15,14 @@ module.exports = { ], // Run on Chrome Headless - browsers: [ - 'ChromeHeadless' - ], + customLaunchers: { + ChromeHeadlessNoSandbox: { + base: 'ChromeHeadless', + // Flags required to run in ubuntu-22.04 or above (https://chromium.googlesource.com/chromium/src/+/master/docs/linux/suid_sandbox_development.md) + flags: ['--no-sandbox', '--disable-setuid-sandbox'] + } + }, + browsers: ['ChromeHeadlessNoSandbox'], // list of files / patterns to load in the browser files: [ diff --git a/package-lock.json b/package-lock.json index 9c46f2f66..a5142e979 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@splitsoftware/splitio", - "version": "11.1.0", + "version": "11.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@splitsoftware/splitio", - "version": "11.1.0", + "version": "11.2.0", "license": "Apache-2.0", "dependencies": { - "@splitsoftware/splitio-commons": "2.1.0", + "@splitsoftware/splitio-commons": "2.2.0", "bloom-filters": "^3.0.4", "ioredis": "^4.28.0", "js-yaml": "^3.13.1", @@ -351,9 +351,9 @@ "dev": true }, "node_modules/@splitsoftware/splitio-commons": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-2.1.0.tgz", - "integrity": "sha512-7SJRBia0Pi72s76drH8kG2cVnCqkjMHMJQWJSFnG+rE/UOx9AROmuviOkY6tv6qYPJFqFQQGHGX6lXjxZhYzkw==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-2.2.0.tgz", + "integrity": "sha512-ywWDh2fM4/EqJ1AByjXM13gAal+z/WSGiBQ5OZmjpL/iqFLENy3yo/GwsxR/ataOi27XbRQTeQbE/eD7HVnWiA==", "dependencies": { "@types/ioredis": "^4.28.0", "tslib": "^2.3.1" @@ -2087,9 +2087,9 @@ "dev": true }, "node_modules/elliptic": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.6.0.tgz", - "integrity": "sha512-dpwoQcLc/2WLQvJvLRHKZ+f9FgOdjnq11rurqwekGQygGPsYSK29OMMD2WalatiqQ+XGFDglTNixpPfI+lpaAA==", + "version": "6.6.1", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.6.1.tgz", + "integrity": "sha512-RaddvvMatK2LJHqFJ+YA4WysVN5Ita9E35botqIYspQ4TkRAlCicdzKOjlyv/1Za5RyTNn7di//eEV0uTAfe3g==", "dev": true, "dependencies": { "bn.js": "^4.11.9", @@ -7537,9 +7537,9 @@ "dev": true }, "@splitsoftware/splitio-commons": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-2.1.0.tgz", - "integrity": "sha512-7SJRBia0Pi72s76drH8kG2cVnCqkjMHMJQWJSFnG+rE/UOx9AROmuviOkY6tv6qYPJFqFQQGHGX6lXjxZhYzkw==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio-commons/-/splitio-commons-2.2.0.tgz", + "integrity": "sha512-ywWDh2fM4/EqJ1AByjXM13gAal+z/WSGiBQ5OZmjpL/iqFLENy3yo/GwsxR/ataOi27XbRQTeQbE/eD7HVnWiA==", "requires": { "@types/ioredis": "^4.28.0", "tslib": "^2.3.1" @@ -8965,9 +8965,9 @@ "dev": true }, "elliptic": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.6.0.tgz", - "integrity": "sha512-dpwoQcLc/2WLQvJvLRHKZ+f9FgOdjnq11rurqwekGQygGPsYSK29OMMD2WalatiqQ+XGFDglTNixpPfI+lpaAA==", + "version": "6.6.1", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.6.1.tgz", + "integrity": "sha512-RaddvvMatK2LJHqFJ+YA4WysVN5Ita9E35botqIYspQ4TkRAlCicdzKOjlyv/1Za5RyTNn7di//eEV0uTAfe3g==", "dev": true, "requires": { "bn.js": "^4.11.9", diff --git a/package.json b/package.json index efc19f4d8..c3ac42b76 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@splitsoftware/splitio", - "version": "11.1.0", + "version": "11.2.0", "description": "Split SDK", "files": [ "README.md", @@ -38,7 +38,7 @@ "node": ">=14.0.0" }, "dependencies": { - "@splitsoftware/splitio-commons": "2.1.0", + "@splitsoftware/splitio-commons": "2.2.0", "bloom-filters": "^3.0.4", "ioredis": "^4.28.0", "js-yaml": "^3.13.1", diff --git a/src/__tests__/browserSuites/impressions-listener.spec.js b/src/__tests__/browserSuites/impressions-listener.spec.js index ca47995c2..9907eab87 100644 --- a/src/__tests__/browserSuites/impressions-listener.spec.js +++ b/src/__tests__/browserSuites/impressions-listener.spec.js @@ -46,7 +46,7 @@ export default function (assert) { const testAttrs = { is_test: true }; // Impression listener is shared across all client instances and does not get affected by configurations. - client.getTreatment('hierarchical_splits_test'); + client.getTreatment('hierarchical_splits_test', undefined, { properties: { prop1: 'prop-value' } }); client2.getTreatment('qc_team'); client2.getTreatmentWithConfig('qc_team'); // Validate that the impression is the same. client3.getTreatment('qc_team', testAttrs); @@ -58,7 +58,8 @@ export default function (assert) { treatment: 'no', bucketingKey: 'impr_bucketing_2', label: 'default rule', - pt: undefined + pt: undefined, + properties: undefined }; assert.equal(listener.logImpression.callCount, 4, 'Impression listener logImpression method should be called after we call client.getTreatment, once per each impression generated.'); @@ -71,7 +72,7 @@ export default function (assert) { bucketingKey: undefined, label: 'expected label', changeNumber: 2828282828, - pt: undefined + properties: '{"prop1":"prop-value"}' }, attributes: undefined, ...metaData diff --git a/src/__tests__/browserSuites/impressions.debug.spec.js b/src/__tests__/browserSuites/impressions.debug.spec.js index 5f90b7004..887d2f4fd 100644 --- a/src/__tests__/browserSuites/impressions.debug.spec.js +++ b/src/__tests__/browserSuites/impressions.debug.spec.js @@ -63,6 +63,14 @@ export default function (fetchMock, assert) { k: 'facundo@split.io', t: 'o.n', m: data[0].i[1].m, c: 828282828282, r: 'another expected label', pt: data[0].i[0].m, }, { k: 'facundo@split.io', t: 'o.n', m: data[0].i[2].m, c: 828282828282, r: 'another expected label', pt: data[0].i[1].m + }, { + k: 'facundo@split.io', t: 'o.n', m: data[0].i[3].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value1"}' + }, { + k: 'facundo@split.io', t: 'o.n', m: data[0].i[4].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value2"}' + }, { + k: 'facundo@split.io', t: 'o.n', m: data[0].i[5].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value3"}' + }, { + k: 'facundo@split.io', t: 'o.n', m: data[0].i[6].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value4"}' }] }]); @@ -75,7 +83,7 @@ export default function (fetchMock, assert) { fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => { assert.deepEqual(JSON.parse(opts.body), { - pf: [{ f: 'always_on_track_impressions_false', m: truncatedTimeFrame, rc: 1 }] + pf: [{ f: 'always_on_impressions_disabled_true', m: truncatedTimeFrame, rc: 1 }] }, 'We should generate impression count for the feature with track impressions disabled.'); return 200; @@ -83,7 +91,7 @@ export default function (fetchMock, assert) { fetchMock.postOnce(url(settings, '/v1/keys/cs'), (url, opts) => { assert.deepEqual(JSON.parse(opts.body), { - keys: [{ fs: ['always_on_track_impressions_false'], k: 'facundo@split.io' }] + keys: [{ fs: ['always_on_impressions_disabled_true'], k: 'facundo@split.io' }] }, 'We should track unique keys for the feature with track impressions disabled.'); return 200; @@ -95,6 +103,12 @@ export default function (fetchMock, assert) { client.getTreatment('split_with_config'); client.getTreatment('split_with_config'); client.getTreatment('split_with_config'); - assert.equal(client.getTreatment('always_on_track_impressions_false'), 'on'); + assert.equal(client.getTreatment('always_on_impressions_disabled_true'), 'on'); + + // with properties + assert.equal(client.getTreatment('split_with_config', undefined, { properties: { prop1: 'value1' } }), 'o.n'); + assert.equal(client.getTreatments(['split_with_config'], undefined, { properties: { prop1: 'value2' } }).split_with_config, 'o.n'); + assert.equal(client.getTreatmentWithConfig('split_with_config', undefined, { properties: { prop1: 'value3' } }).treatment, 'o.n'); + assert.equal(client.getTreatmentsWithConfig(['split_with_config'], undefined, { properties: { prop1: 'value4' } }).split_with_config.treatment, 'o.n'); }); } diff --git a/src/__tests__/browserSuites/impressions.none.spec.js b/src/__tests__/browserSuites/impressions.none.spec.js index 2294c9695..e75a9ea87 100644 --- a/src/__tests__/browserSuites/impressions.none.spec.js +++ b/src/__tests__/browserSuites/impressions.none.spec.js @@ -59,7 +59,7 @@ export default async function (fetchMock, assert) { { f: 'split_with_config', m: truncatedTimeFrame, rc: 2 }, { f: 'always_off', m: truncatedTimeFrame, rc: 4 }, { f: 'always_on', m: truncatedTimeFrame, rc: 2 }, - { f: 'always_on_track_impressions_false', m: truncatedTimeFrame, rc: 1 } + { f: 'always_on_impressions_disabled_true', m: truncatedTimeFrame, rc: 1 } ] }); return 200; @@ -76,7 +76,7 @@ export default async function (fetchMock, assert) { }, { k: 'emma@split.io', - fs: ['always_off', 'always_on', 'always_on_track_impressions_false'] + fs: ['always_off', 'always_on', 'always_on_impressions_disabled_true'] } ] }, 'We performed evaluations for two keys, so we should have 2 item total.'); @@ -94,7 +94,7 @@ export default async function (fetchMock, assert) { client.getTreatment('always_on'); client.getTreatment('always_off'); client.getTreatment('split_with_config'); - sharedClient.getTreatment('always_on_track_impressions_false'); + sharedClient.getTreatment('always_on_impressions_disabled_true'); client.destroy().then(() => { assert.end(); diff --git a/src/__tests__/browserSuites/impressions.spec.js b/src/__tests__/browserSuites/impressions.spec.js index 33e60106a..f4c800468 100644 --- a/src/__tests__/browserSuites/impressions.spec.js +++ b/src/__tests__/browserSuites/impressions.spec.js @@ -47,32 +47,21 @@ export default function (fetchMock, assert) { const client = splitio.client(); const assertPayload = req => { - const resp = JSON.parse(req.body); - - assert.equal(resp.length, 2, 'We performed evaluations for 3 features, but one with `impressionsDisabled` true, so we should have 2 items total'); - - const dependencyChildImpr = resp.filter(e => e.f === 'hierarchical_splits_test')[0]; - const splitWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0]; - const alwaysOnWithTrackImpressionsFalse = resp.filter(e => e.f === 'always_on_track_impressions_false'); - - assert.true(dependencyChildImpr, 'Split we wanted to evaluate should be present on the impressions.'); - assert.false(resp.some(e => e.f === 'hierarchical_dep_always_on'), 'Parent split evaluations should not result in impressions.'); - assert.false(resp.some(e => e.f === 'hierarchical_dep_hierarchical'), 'No matter how deep is the chain.'); - assert.true(splitWithConfigImpr, 'Split evaluated with config should have generated an impression too.'); - assert.false(Object.prototype.hasOwnProperty.call(splitWithConfigImpr.i[0], 'configuration'), 'Impressions do not change with configuration evaluations.'); - assert.false(Object.prototype.hasOwnProperty.call(splitWithConfigImpr.i[0], 'config'), 'Impressions do not change with configuration evaluations.'); - assert.equal(alwaysOnWithTrackImpressionsFalse.length, 0); - - const { - k, - r, - t - } = dependencyChildImpr.i[0]; - - assert.equal(k, 'facundo@split.io', 'Present impression should have the correct key.'); - // The label present on the mock. - assert.equal(r, 'expected label', 'Present impression should have the correct label.'); - assert.equal(t, 'on', 'Present impression should have the correct treatment.'); + const reqBody = JSON.parse(req.body); + + assert.deepEqual(reqBody, [{ + f: 'hierarchical_splits_test', + i: [{ + k: 'facundo@split.io', t: 'on', m: reqBody[0].i[0].m, c: 2828282828, r: 'expected label' + }] + }, { + f: 'split_with_config', + i: [{ + k: 'facundo@split.io', t: 'o.n', m: reqBody[1].i[0].m, c: 828282828282, r: 'another expected label' + }, { + k: 'facundo@split.io', t: 'o.n', m: reqBody[1].i[1].m, c: 828282828282, r: 'another expected label', properties: '{"some":"value2"}' + }] + }], 'We performed evaluations for 3 features, but one with `impressionsDisabled` true, so we should have 2 items total'); }; fetchMock.postOnce(url(settings, '/testImpressions/bulk'), (url, req) => { @@ -93,28 +82,23 @@ export default function (fetchMock, assert) { return 200; }); - fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => { - const data = JSON.parse(opts.body); + fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, req) => { + const reqBody = JSON.parse(req.body); - assert.equal(data.pf.length, 2, 'We should generate impressions count for 2 features.'); - - // finding these validate the feature names collection too - const splitWithConfigImpr = data.pf.filter(e => e.f === 'split_with_config')[0]; - const alwaysOnWithTrackImpressionsFalse = data.pf.filter(e => e.f === 'always_on_track_impressions_false')[0]; - - assert.equal(splitWithConfigImpr.rc, 2); - assert.equal(typeof splitWithConfigImpr.m, 'number'); - assert.equal(splitWithConfigImpr.m, truncatedTimeFrame); - assert.equal(alwaysOnWithTrackImpressionsFalse.rc, 1); - assert.equal(typeof alwaysOnWithTrackImpressionsFalse.m, 'number'); - assert.equal(alwaysOnWithTrackImpressionsFalse.m, truncatedTimeFrame); + assert.deepEqual(reqBody, { + pf: [{ + f: 'split_with_config', m: truncatedTimeFrame, rc: 2 + }, { + f: 'always_on_impressions_disabled_true', m: truncatedTimeFrame, rc: 1 + }] + }, 'We should generate impressions count for 2 features.'); return 200; }); fetchMock.postOnce(url(settings, '/v1/keys/cs'), (url, opts) => { assert.deepEqual(JSON.parse(opts.body), { - keys: [{ fs: [ 'always_on_track_impressions_false' ], k: 'facundo@split.io' }] + keys: [{ fs: ['always_on_impressions_disabled_true'], k: 'facundo@split.io' }] }, 'We should only track unique keys for features flags with track impressions disabled.'); return 200; @@ -129,9 +113,12 @@ export default function (fetchMock, assert) { config: '{"color":"brown","dimensions":{"height":12,"width":14},"text":{"inner":"click me"}}' }, 'We should get an evaluation as always.'); client.getTreatmentWithConfig('split_with_config'); - client.getTreatmentWithConfig('split_with_config'); + client.getTreatmentWithConfig('split_with_config', undefined, { properties: { /* empty properties are ignored */ } }); + + // Impression should not be tracked (passed properties will not be submitted) + assert.equal(client.getTreatment('always_on_impressions_disabled_true'), 'on', undefined, { properties: { some: 'value1' } }); - // Impression should not be tracked - assert.equal(client.getTreatment('always_on_track_impressions_false'), 'on'); + // Tracked impression with properties should be handled in DEBUG mode (doesn't increase `rc` count but adds an impression) + assert.equal(client.getTreatment('split_with_config', undefined, { properties: { some: 'value2' } }), 'o.n'); }); } diff --git a/src/__tests__/browserSuites/ready-from-cache.spec.js b/src/__tests__/browserSuites/ready-from-cache.spec.js index 0e9cdd90f..289b34809 100644 --- a/src/__tests__/browserSuites/ready-from-cache.spec.js +++ b/src/__tests__/browserSuites/ready-from-cache.spec.js @@ -1,3 +1,5 @@ +import sinon from 'sinon'; +import { nearlyEqual } from '../testUtils'; import { getStorageHash } from '@splitsoftware/splitio-commons/src/storages/KeyBuilder'; import { SplitFactory } from '../../'; @@ -5,7 +7,6 @@ import splitChangesMock1 from '../mocks/splitchanges.since.-1.json'; import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json'; import membershipsNicolas from '../mocks/memberships.nicolas@split.io.json'; -import { nearlyEqual } from '../testUtils'; const DEFAULT_CACHE_EXPIRATION_IN_MILLIS = 864000000; // 10 days @@ -94,7 +95,7 @@ export default function (fetchMock, assert) { events: 'https://events.baseurl/readyFromCacheEmpty' }; localStorage.clear(); - t.plan(3); + t.plan(4); fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=-1', { status: 200, body: splitChangesMock1 }); fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); @@ -123,18 +124,17 @@ export default function (fetchMock, assert) { t.end(); }); client.once(client.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE if there is no cache.'); - t.end(); + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client.on(client.Event.SDK_READY, () => { - t.pass('It should emit SDK_READY alone, since there was no cache.'); + t.true(client.__getStatus().isReadyFromCache, 'Client should emit SDK_READY and it should be ready from cache'); }); client2.on(client.Event.SDK_READY, () => { - t.pass('It should emit SDK_READY alone, since there was no cache.'); + t.true(client2.__getStatus().isReadyFromCache, 'Non-default client should emit SDK_READY and it should be ready from cache'); }); client3.on(client.Event.SDK_READY, () => { - t.pass('It should emit SDK_READY alone, since there was no cache.'); + t.true(client2.__getStatus().isReadyFromCache, 'Non-default client should emit SDK_READY and it should be ready from cache'); }); }); @@ -147,17 +147,17 @@ export default function (fetchMock, assert) { localStorage.clear(); t.plan(12 * 2 + 3); - fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=25', function () { + fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=25', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: { ...splitChangesMock1, since: 25 }, headers: {} }), 200); }); // 400ms is how long it'll take to reply with Splits, no SDK_READY should be emitted before that. }); fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', function () { + fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: membershipsNicolas, headers: {} }), 400); }); // First client gets segments before splits. No segment cache loading (yet) }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas2%40split.io', function () { + fetchMock.get(testUrls.sdk + '/memberships/nicolas2%40split.io', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), 700); }); // Second client gets segments after 700ms }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas3%40split.io', function () { + fetchMock.get(testUrls.sdk + '/memberships/nicolas3%40split.io', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), 1000); }); // Third client memberships will come after 1s }); fetchMock.postOnce(testUrls.events + '/testImpressions/bulk', 200); @@ -179,7 +179,6 @@ export default function (fetchMock, assert) { readyTimeout: 0.85 }, urls: testUrls, - debug: true }); const client = splitio.client(); const client2 = splitio.client('nicolas2@split.io'); @@ -247,7 +246,7 @@ export default function (fetchMock, assert) { }); }); - assert.test(t => { // Testing when we start with cached data and not expired (lastUpdate item higher than expirationTimestamp) + assert.test(t => { // Testing when we start with cached data and not expired (lastUpdate timestamp higher than default (10) expirationDays ago) const testUrls = { sdk: 'https://sdk.baseurl/readyFromCacheWithData3', events: 'https://events.baseurl/readyFromCacheWithData3' @@ -255,18 +254,18 @@ export default function (fetchMock, assert) { localStorage.clear(); t.plan(12 * 2 + 5); - fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=25', function () { + fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=25', () => { t.equal(localStorage.getItem('readyFromCache_3.SPLITIO.split.always_on'), alwaysOnSplitInverted, 'feature flags must not be cleaned from cache'); return new Promise(res => { setTimeout(() => res({ status: 200, body: { ...splitChangesMock1, since: 25 }, headers: {} }), 200); }); // 400ms is how long it'll take to reply with Splits, no SDK_READY should be emitted before that. }); fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', function () { + fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: membershipsNicolas, headers: {} }), 400); }); // First client gets segments before splits. No segment cache loading (yet) }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas2%40split.io', function () { + fetchMock.get(testUrls.sdk + '/memberships/nicolas2%40split.io', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), 700); }); // Second client gets segments after 700ms }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas3%40split.io', function () { + fetchMock.get(testUrls.sdk + '/memberships/nicolas3%40split.io', () => { return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), 1000); }); // Third client memberships will come after 1s }); fetchMock.get(testUrls.sdk + '/memberships/nicolas4%40split.io', { 'ms': {} }); @@ -290,7 +289,6 @@ export default function (fetchMock, assert) { readyTimeout: 0.85 }, urls: testUrls, - debug: true }); const client = splitio.client(); const client2 = splitio.client('nicolas2@split.io'); @@ -365,35 +363,38 @@ export default function (fetchMock, assert) { }); }); - assert.test(t => { // Testing when we start with cached data but expired (lastUpdate item lower than expirationTimestamp) + assert.test(t => { // Testing when we start with cached data but expired (lastUpdate timestamp lower than custom (1) expirationDays ago) + const CLIENT_READY_MS = 400, CLIENT2_READY_MS = 700, CLIENT3_READY_MS = 1000; + const testUrls = { sdk: 'https://sdk.baseurl/readyFromCacheWithData4', events: 'https://events.baseurl/readyFromCacheWithData4' }; localStorage.clear(); - fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=-1', function () { + fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=-1', () => { t.equal(localStorage.getItem('some_user_item'), 'user_item', 'user items at localStorage must not be changed'); t.equal(localStorage.getItem('readyFromCache_4.SPLITIO.hash'), expectedHashNullFilter, 'storage hash must not be changed'); - t.equal(localStorage.length, 2, 'feature flags cache data must be cleaned from localStorage'); + t.true(nearlyEqual(parseInt(localStorage.getItem('readyFromCache_4.SPLITIO.lastClear'), 10), Date.now()), 'storage lastClear timestamp must be updated'); + t.equal(localStorage.length, 3, 'feature flags cache data must be cleaned from localStorage'); return { status: 200, body: splitChangesMock1 }; }); fetchMock.get(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', function () { - return new Promise(res => { setTimeout(() => res({ status: 200, body: membershipsNicolas, headers: {} }), 400); }); // First client gets segments before splits. No segment cache loading (yet) + fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', () => { + return new Promise(res => { setTimeout(() => res({ status: 200, body: membershipsNicolas, headers: {} }), CLIENT_READY_MS); }); // First client gets segments before splits. No segment cache loading (yet) }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas2%40split.io', function () { - return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), 700); }); // Second client gets segments after 700ms + fetchMock.get(testUrls.sdk + '/memberships/nicolas2%40split.io', () => { + return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), CLIENT2_READY_MS); }); // Second client gets segments after 700ms }); - fetchMock.get(testUrls.sdk + '/memberships/nicolas3%40split.io', function () { - return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), 1000); }); // Third client memberships will come after 1s + fetchMock.get(testUrls.sdk + '/memberships/nicolas3%40split.io', () => { + return new Promise(res => { setTimeout(() => res({ status: 200, body: { 'ms': {} }, headers: {} }), CLIENT3_READY_MS); }); // Third client memberships will come after 1s }); fetchMock.postOnce(testUrls.events + '/testImpressions/bulk', 200); fetchMock.postOnce(testUrls.events + '/testImpressions/count', 200); localStorage.setItem('some_user_item', 'user_item'); localStorage.setItem('readyFromCache_4.SPLITIO.splits.till', 25); - localStorage.setItem('readyFromCache_4.SPLITIO.splits.lastUpdated', Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS - 1); // -1 to ensure having an expired lastUpdated item + localStorage.setItem('readyFromCache_4.SPLITIO.splits.lastUpdated', Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS / 10 - 1); // -1 to ensure having an expired lastUpdated item localStorage.setItem('readyFromCache_4.SPLITIO.split.always_on', alwaysOnSplitInverted); localStorage.setItem('readyFromCache_4.SPLITIO.hash', expectedHashNullFilter); @@ -402,13 +403,13 @@ export default function (fetchMock, assert) { ...baseConfig, storage: { type: 'LOCALSTORAGE', - prefix: 'readyFromCache_4' + prefix: 'readyFromCache_4', + expirationDays: 1, }, startup: { readyTimeout: 0.85 }, urls: testUrls, - debug: true }); const client = splitio.client(); const client2 = splitio.client('nicolas2@split.io'); @@ -423,37 +424,34 @@ export default function (fetchMock, assert) { }); client.once(client.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE if there is expired cache.'); - t.end(); + t.true(nearlyEqual(Date.now() - startTime, CLIENT_READY_MS), 'It should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client2.once(client2.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE if there is expired cache.'); - t.end(); + t.true(nearlyEqual(Date.now() - startTime, CLIENT2_READY_MS), 'It should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client3.once(client3.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE if there is expired cache.'); - t.end(); + t.true(nearlyEqual(Date.now() - startTime, CLIENT3_READY_MS), 'It should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client.on(client.Event.SDK_READY, () => { - t.true(Date.now() - startTime >= 400, 'It should emit SDK_READY after syncing with the cloud.'); + t.true(nearlyEqual(Date.now() - startTime, CLIENT_READY_MS), 'It should emit SDK_READY after syncing with the cloud.'); t.equal(client.getTreatment('always_on'), 'on', 'It should evaluate treatments with updated data after syncing with the cloud.'); }); client.ready().then(() => { - t.true(Date.now() - startTime >= 400, 'It should resolve ready promise after syncing with the cloud.'); + t.true(nearlyEqual(Date.now() - startTime, CLIENT_READY_MS), 'It should resolve ready promise after syncing with the cloud.'); t.equal(client.getTreatment('always_on'), 'on', 'It should evaluate treatments with updated data after syncing with the cloud.'); }); client2.on(client2.Event.SDK_READY, () => { - t.true(Date.now() - startTime >= 700, 'It should emit SDK_READY after syncing with the cloud.'); + t.true(nearlyEqual(Date.now() - startTime, CLIENT2_READY_MS), 'It should emit SDK_READY after syncing with the cloud.'); t.equal(client2.getTreatment('always_on'), 'on', 'It should evaluate treatments with updated data after syncing with the cloud.'); }); client2.ready().then(() => { - t.true(Date.now() - startTime >= 700, 'It should resolve ready promise after syncing with the cloud.'); + t.true(nearlyEqual(Date.now() - startTime, CLIENT2_READY_MS), 'It should resolve ready promise after syncing with the cloud.'); t.equal(client2.getTreatment('always_on'), 'on', 'It should evaluate treatments with updated data after syncing with the cloud.'); }); client3.on(client3.Event.SDK_READY, () => { client3.ready().then(() => { - t.true(Date.now() - startTime >= 1000, 'It should resolve ready promise after syncing with the cloud.'); + t.true(nearlyEqual(Date.now() - startTime, CLIENT3_READY_MS), 'It should resolve ready promise after syncing with the cloud.'); t.equal(client3.getTreatment('always_on'), 'on', 'It should evaluate treatments with updated data after syncing with the cloud.'); // Last cb: destroy clients and check that localstorage has the expected items @@ -486,7 +484,7 @@ export default function (fetchMock, assert) { events: 'https://events.baseurl/readyFromCache_5' }; localStorage.clear(); - t.plan(7); + t.plan(8); fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1&names=p1__split,p2__split', { status: 200, body: { splits: [splitDeclarations.p1__split, splitDeclarations.p2__split], since: -1, till: 1457552620999 } }, { delay: 10 }); // short delay to let emit SDK_READY_FROM_CACHE fetchMock.getOnce(testUrls.sdk + '/memberships/nicolas%40split.io', { status: 200, body: { ms: {} } }); @@ -507,14 +505,12 @@ export default function (fetchMock, assert) { sync: { splitFilters: [{ type: 'byName', values: ['p2__split', 'p1__split'] }, { type: 'byName', values: ['p2__split', null] }] }, - debug: true }); const client = splitio.client(); const manager = splitio.manager(); client.once(client.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE because localStorage is cleaned and there isn\'t cached feature flags'); - t.end(); + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client.once(client.Event.SDK_READY, () => { @@ -538,7 +534,7 @@ export default function (fetchMock, assert) { events: 'https://events.baseurl/readyFromCache_5B' }; localStorage.clear(); - t.plan(5); + t.plan(6); fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1&names=p1__split,p2__split', { status: 200, body: { splits: [splitDeclarations.p1__split, splitDeclarations.p2__split], since: -1, till: 1457552620999 } }, { delay: 10 }); // short delay to let emit SDK_READY_FROM_CACHE fetchMock.getOnce(testUrls.sdk + '/memberships/nicolas%40split.io', { status: 200, body: { ms: {} } }); @@ -553,14 +549,12 @@ export default function (fetchMock, assert) { sync: { splitFilters: [{ type: 'byName', values: ['p2__split', 'p1__split'] }, { type: 'byName', values: ['p2__split', null] }] }, - debug: true }); const client = splitio.client(); const manager = splitio.manager(); client.once(client.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE if cache is empty.'); - t.end(); + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client.once(client.Event.SDK_READY, () => { @@ -604,7 +598,6 @@ export default function (fetchMock, assert) { sync: { splitFilters: [{ type: 'byName', values: [undefined, true, 'p2__split'] }, { type: 'byPrefix', values: ['p1'] }, { type: 'byName', values: ['p2__split'] }] }, - debug: true }); const client = splitio.client(); const manager = splitio.manager(); @@ -633,7 +626,7 @@ export default function (fetchMock, assert) { events: 'https://events.baseurl/readyFromCache_7' }; localStorage.clear(); - t.plan(6); + t.plan(7); fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1&prefixes=p1,p2', { status: 200, body: { splits: [splitDeclarations.p1__split, splitDeclarations.p2__split], since: -1, till: 1457552620999 } }, { delay: 10 }); // short delay to let emit SDK_READY_FROM_CACHE fetchMock.getOnce(testUrls.sdk + '/memberships/nicolas%40split.io', { status: 200, body: { ms: {} } }); @@ -650,20 +643,19 @@ export default function (fetchMock, assert) { ...baseConfig, storage: { type: 'LOCALSTORAGE', - prefix: 'readyFromCache_7' + prefix: 'readyFromCache_7', + expirationDays: 0, // invalid value, will use default (10) }, urls: testUrls, sync: { splitFilters: [{ type: 'byPrefix', values: ['p2'] }, { type: 'byPrefix', values: ['p1', ''] }, { type: '', values: [] }, {}, { type: 'byPrefix' }] }, - debug: true }); const client = splitio.client(); const manager = splitio.manager(); client.once(client.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE if cache has expired.'); - t.end(); + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client.once(client.Event.SDK_READY, () => { @@ -699,7 +691,7 @@ export default function (fetchMock, assert) { events: 'https://events.baseurl/readyFromCache_8' }; localStorage.clear(); - t.plan(7); + t.plan(8); fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1', { status: 200, body: { splits: [splitDeclarations.p1__split, splitDeclarations.p2__split, splitDeclarations.p3__split], since: -1, till: 1457552620999 } }, { delay: 10 }); // short delay to let emit SDK_READY_FROM_CACHE fetchMock.getOnce(testUrls.sdk + '/memberships/nicolas%40split.io', { status: 200, body: { ms: {} } }); @@ -719,14 +711,12 @@ export default function (fetchMock, assert) { }, urls: testUrls, sync: syncParam, - debug: true }); const client = splitio.client(); const manager = splitio.manager(); client.once(client.Event.SDK_READY_FROM_CACHE, () => { - t.fail('It should not emit SDK_READY_FROM_CACHE because all feature flags were removed from cache since the filter query changed.'); - t.end(); + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY'); }); client.once(client.Event.SDK_READY, () => { @@ -774,7 +764,6 @@ export default function (fetchMock, assert) { sync: { splitFilters: [{ type: 'byName', values: ['p3__split'] }, { type: 'byPrefix', values: [' p2', ' p2', ' p2', ' p2', 'no exist trim '] }, { type: 'byName', values: ['no_exist', ' no exist trim '] }] }, - debug: true }); const client = splitio.client(); const manager = splitio.manager(); @@ -793,4 +782,91 @@ export default function (fetchMock, assert) { }); }); + assert.test(async t => { // Testing clearOnInit config true + sinon.spy(console, 'log'); + + const testUrls = { + sdk: 'https://sdk.baseurl/readyFromCache_10', + events: 'https://events.baseurl/readyFromCache_10' + }; + const clearOnInitConfig = { + ...baseConfig, + storage: { + type: 'LOCALSTORAGE', + prefix: 'readyFromCache_10', + clearOnInit: true + }, + urls: testUrls, + debug: true + }; + + // Start with cached data but without lastClear item (JS SDK below 11.1.0) -> cache cleanup + localStorage.clear(); + localStorage.setItem('readyFromCache_10.SPLITIO.splits.till', 25); + localStorage.setItem('readyFromCache_10.SPLITIO.split.p1__split', JSON.stringify(splitDeclarations.p1__split)); + localStorage.setItem('readyFromCache_10.SPLITIO.split.p2__split', JSON.stringify(splitDeclarations.p2__split)); + localStorage.setItem('readyFromCache_10.SPLITIO.hash', expectedHashNullFilter); + + fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1', { status: 200, body: splitChangesMock1 }); + fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); + fetchMock.get(testUrls.sdk + '/memberships/nicolas%40split.io', { status: 200, body: { ms: {} } }); + + let splitio = SplitFactory(clearOnInitConfig); + let client = splitio.client(); + let manager = splitio.manager(); + + 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'); + + client.once(client.Event.SDK_READY_FROM_CACHE, () => { + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY, because clearOnInit is true'); + }); + + await client.ready(); + t.equal(manager.names().sort().length, 33, 'active splits should be present for evaluation'); + + await splitio.destroy(); + t.equal(localStorage.getItem('readyFromCache_10.SPLITIO.splits.till'), '1457552620999', 'splits.till must correspond to the till of the last successfully fetched Splits'); + t.equal(localStorage.getItem('readyFromCache_10.SPLITIO.hash'), expectedHashNullFilter, 'Storage hash must not be changed'); + t.true(nearlyEqual(parseInt(localStorage.getItem('readyFromCache_10.SPLITIO.lastClear')), Date.now()), 'lastClear timestamp must be set'); + + // Start again with cached data and lastClear item within the last 24 hours -> no cache cleanup + console.log.resetHistory(); + fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); + + splitio = SplitFactory(clearOnInitConfig); + client = splitio.client(); + manager = splitio.manager(); + + await new Promise(res => client.once(client.Event.SDK_READY_FROM_CACHE, res)); + + t.equal(manager.names().sort().length, 33, 'active splits should be present for evaluation'); + 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'); + + await splitio.destroy(); + + // Start again with cached data and lastClear item older than 24 hours -> cache cleanup + console.log.resetHistory(); + localStorage.setItem('readyFromCache_10.SPLITIO.lastClear', Date.now() - 25 * 60 * 60 * 1000); // 25 hours ago + fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=-1', { status: 200, body: splitChangesMock1 }); + fetchMock.getOnce(testUrls.sdk + '/splitChanges?s=1.2&since=1457552620999', { status: 200, body: splitChangesMock2 }); + + splitio = SplitFactory(clearOnInitConfig); + client = splitio.client(); + manager = splitio.manager(); + + client.once(client.Event.SDK_READY_FROM_CACHE, () => { + t.true(client.__getStatus().isReady, 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY, because clearOnInit is true'); + }); + + await new Promise(res => client.once(client.Event.SDK_READY, res)); + + t.equal(manager.names().sort().length, 33, 'active splits should be present for evaluation'); + 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'); + + await splitio.destroy(); + + console.log.restore(); + t.end(); + }); + } diff --git a/src/__tests__/consumer/node_redis.spec.js b/src/__tests__/consumer/node_redis.spec.js index a2317ce48..7bc0c93b1 100644 --- a/src/__tests__/consumer/node_redis.spec.js +++ b/src/__tests__/consumer/node_redis.spec.js @@ -23,7 +23,7 @@ const NA = 'NA'; const redisPort = '6385'; -const TOTAL_RAW_IMPRESSIONS = 16; +const TOTAL_RAW_IMPRESSIONS = 17; const TOTAL_EVENTS = 2; const DEDUPED_IMPRESSIONS = 3; @@ -118,7 +118,8 @@ tape('Node.js Redis', function (t) { /** Evaluation, track and manager methods on SDK_READY */ - assert.equal(await client.getTreatment('UT_Segment_member', 'UT_IN_SEGMENT'), 'on', 'Evaluations using Redis storage should be correct.'); + assert.equal(await client.getTreatment('UT_Segment_member', 'UT_IN_SEGMENT', undefined, { properties: { /* empty properties are ignored */ } }), 'on', 'Evaluations using Redis storage should be correct.'); + assert.equal(await client.getTreatment('UT_Segment_member', 'UT_IN_SEGMENT', undefined, { properties: { some: 'value1' } }), 'on', 'Evaluations using Redis storage should be correct.'); assert.equal(await client.getTreatment('other', 'UT_IN_SEGMENT'), 'off', 'Evaluations using Redis storage should be correct.'); assert.equal(await client.getTreatment('UT_Segment_member', 'UT_NOT_IN_SEGMENT'), 'off', 'Evaluations using Redis storage should be correct.'); @@ -223,7 +224,9 @@ tape('Node.js Redis', function (t) { // this should be deduped assert.equal(await client.getTreatment('other', 'UT_IN_SEGMENT'), 'off', 'Evaluations using Redis storage should be correct.'); // this should be deduped - assert.equal(await client.getTreatment('other', 'UT_IN_SEGMENT'), 'off', 'Evaluations using Redis storage should be correct.'); + assert.equal(await client.getTreatment('other', 'UT_IN_SEGMENT', undefined, { properties: { /* empty properties are ignored */ } }), 'off', 'Evaluations using Redis storage should be correct.'); + // this should not be deduped because of properties + assert.equal(await client.getTreatment('other', 'UT_IN_SEGMENT', undefined, { properties: { some: 'value1' } }), 'off', 'Evaluations using Redis storage should be correct.'); assert.equal(await client.getTreatment('UT_Segment_member', 'UT_NOT_IN_SEGMENT'), 'off', 'Evaluations using Redis storage should be correct.'); assert.equal(await client.getTreatment('other', 'UT_NOT_IN_SEGMENT'), 'on', 'Evaluations using Redis storage should be correct.'); diff --git a/src/__tests__/mocks/splitchanges.since.-1.json b/src/__tests__/mocks/splitchanges.since.-1.json index 372b030b8..c479eae1d 100644 --- a/src/__tests__/mocks/splitchanges.since.-1.json +++ b/src/__tests__/mocks/splitchanges.since.-1.json @@ -1387,7 +1387,7 @@ "environment": null, "trafficTypeId": null, "trafficTypeName": null, - "name": "always_on_track_impressions_false", + "name": "always_on_impressions_disabled_true", "impressionsDisabled": true, "seed": -790401604, "status": "ACTIVE", diff --git a/src/__tests__/nodeSuites/impressions-listener.spec.js b/src/__tests__/nodeSuites/impressions-listener.spec.js index 984c36d1e..5a09d532c 100644 --- a/src/__tests__/nodeSuites/impressions-listener.spec.js +++ b/src/__tests__/nodeSuites/impressions-listener.spec.js @@ -39,13 +39,13 @@ export default function (assert) { const testAttrs = { is_test: true }; // Generate one impression, depends on hierarchical_dep_hierarchical which depends on hierarchical_dep_always_on - client.getTreatment('nicolas@split.io', 'hierarchical_splits_test'); + client.getTreatment('nicolas@split.io', 'hierarchical_splits_test', undefined, { properties: { prop1: 'prop-value' } }); client.getTreatment({ matchingKey: 'marcio@split.io', bucketingKey: 'impr_bucketing_2' }, 'qc_team'); client.getTreatment('facundo@split.io', 'qc_team', testAttrs); client.getTreatment('facundo@split.io', 'qc_team', testAttrs); setTimeout(() => { - assert.true(listener.logImpression.callCount, 4, 'Impression listener logImpression method should be called after we call client.getTreatment, once per each impression generated.'); + assert.equal(listener.logImpression.callCount, 4, 'Impression listener logImpression method should be called after we call client.getTreatment, once per each impression generated.'); assert.true(listener.logImpression.getCall(0).calledWithExactly({ impression: { feature: 'hierarchical_splits_test', @@ -55,7 +55,7 @@ export default function (assert) { bucketingKey: undefined, label: 'expected label', changeNumber: 2828282828, - pt: undefined + properties: '{"prop1":"prop-value"}' }, attributes: undefined, ...metaData diff --git a/src/__tests__/nodeSuites/impressions.debug.spec.js b/src/__tests__/nodeSuites/impressions.debug.spec.js index 08faa13e3..f0f194f9b 100644 --- a/src/__tests__/nodeSuites/impressions.debug.spec.js +++ b/src/__tests__/nodeSuites/impressions.debug.spec.js @@ -63,6 +63,14 @@ export default async function (key, fetchMock, assert) { k: 'facundo@split.io', t: 'o.n', m: data[0].i[1].m, c: 828282828282, r: 'another expected label', b: 'test_buck_key', pt: data[0].i[0].m }, { k: 'facundo@split.io', t: 'o.n', m: data[0].i[2].m, c: 828282828282, r: 'another expected label', b: 'test_buck_key', pt: data[0].i[1].m + }, { + k: 'emi@split.io', t: 'o.n', m: data[0].i[3].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value1"}' + }, { + k: 'emi@split.io', t: 'o.n', m: data[0].i[4].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value2"}' + }, { + k: 'emi@split.io', t: 'o.n', m: data[0].i[5].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value3"}' + }, { + k: 'emi@split.io', t: 'o.n', m: data[0].i[6].m, c: 828282828282, r: 'another expected label', properties: '{"prop1":"value4"}' }] }], 'We performed evaluations for one split, so we should have 1 item total.'); @@ -75,7 +83,7 @@ export default async function (key, fetchMock, assert) { fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => { assert.deepEqual(JSON.parse(opts.body), { - pf: [{ f: 'always_on_track_impressions_false', m: truncatedTimeFrame, rc: 1 }] + pf: [{ f: 'always_on_impressions_disabled_true', m: truncatedTimeFrame, rc: 1 }] }, 'We should generate impression count for the feature with track impressions disabled.'); return 200; @@ -83,7 +91,7 @@ export default async function (key, fetchMock, assert) { fetchMock.postOnce(url(settings, '/v1/keys/ss'), (url, opts) => { assert.deepEqual(JSON.parse(opts.body), { - keys: [{ f: 'always_on_track_impressions_false', ks: ['other_key'] }] + keys: [{ f: 'always_on_impressions_disabled_true', ks: ['other_key'] }] }, 'We should track unique keys for the feature with track impressions disabled.'); return 200; @@ -97,5 +105,10 @@ export default async function (key, fetchMock, assert) { assert.equal(client.getTreatment({ matchingKey: key, bucketingKey: 'test_buck_key' }, 'split_with_config'), 'o.n'); assert.equal(client.getTreatment({ matchingKey: key, bucketingKey: 'test_buck_key' }, 'split_with_config'), 'o.n'); assert.equal(client.getTreatment({ matchingKey: key, bucketingKey: 'test_buck_key' }, 'split_with_config'), 'o.n'); - assert.equal(client.getTreatment({ matchingKey: 'other_key', bucketingKey: 'test_buck_key' }, 'always_on_track_impressions_false'), 'on'); + assert.equal(client.getTreatment({ matchingKey: 'other_key', bucketingKey: 'test_buck_key' }, 'always_on_impressions_disabled_true'), 'on'); + + assert.equal(client.getTreatment('emi@split.io', 'split_with_config', undefined, { properties: { prop1: 'value1' } }), 'o.n'); + assert.equal(client.getTreatments('emi@split.io', ['split_with_config'], undefined, { properties: { prop1: 'value2' } }).split_with_config, 'o.n'); + assert.equal(client.getTreatmentWithConfig('emi@split.io', 'split_with_config', undefined, { properties: { prop1: 'value3' } }).treatment, 'o.n'); + assert.equal(client.getTreatmentsWithConfig('emi@split.io', ['split_with_config'], undefined, { properties: { prop1: 'value4' } }).split_with_config.treatment, 'o.n'); } diff --git a/src/__tests__/nodeSuites/impressions.none.spec.js b/src/__tests__/nodeSuites/impressions.none.spec.js index 2e47f32e1..004748e3b 100644 --- a/src/__tests__/nodeSuites/impressions.none.spec.js +++ b/src/__tests__/nodeSuites/impressions.none.spec.js @@ -55,7 +55,7 @@ export default async function (key, fetchMock, assert) { { f: 'split_with_config', m: truncatedTimeFrame, rc: 3 }, { f: 'always_off', m: truncatedTimeFrame, rc: 3 }, { f: 'always_on', m: truncatedTimeFrame, rc: 5 }, - { f: 'always_on_track_impressions_false', m: truncatedTimeFrame, rc: 1 } + { f: 'always_on_impressions_disabled_true', m: truncatedTimeFrame, rc: 1 } ] }); return 200; @@ -79,7 +79,7 @@ export default async function (key, fetchMock, assert) { ks: ['emma@split.io', 'emi@split.io', 'nico@split.io'] }, { - f: 'always_on_track_impressions_false', + f: 'always_on_impressions_disabled_true', ks: ['emi@split.io'] } ] @@ -100,7 +100,7 @@ export default async function (key, fetchMock, assert) { client.getTreatment('nico@split.io', 'always_on'); client.getTreatment('emi@split.io', 'split_with_config'); client.getTreatment('emma@split.io', 'split_with_config'); - client.getTreatment('emi@split.io', 'always_on_track_impressions_false'); + client.getTreatment('emi@split.io', 'always_on_impressions_disabled_true'); client.destroy().then(() => { assert.end(); diff --git a/src/__tests__/nodeSuites/impressions.spec.js b/src/__tests__/nodeSuites/impressions.spec.js index e7615140c..6489f3c99 100644 --- a/src/__tests__/nodeSuites/impressions.spec.js +++ b/src/__tests__/nodeSuites/impressions.spec.js @@ -55,12 +55,12 @@ export default async function (key, fetchMock, assert) { const dependencyChildImpr = data.filter(e => e.f === 'hierarchical_splits_test')[0]; const splitWithConfigImpr = data.filter(e => e.f === 'split_with_config')[0]; const notExistentSplitImpr = data.filter(e => e.f === 'not_existent_split')[0]; - const alwaysOnWithTrackImpressionsFalse = data.filter(e => e.f === 'always_on_track_impressions_false'); + const alwaysOnWithImpressionsDisabledTrue = data.filter(e => e.f === 'always_on_impressions_disabled_true'); assert.equal(notExistentSplitImpr.i.length, 1); // Only one, the split not found is filtered by the non existent Split check. - assert.equal(splitWithConfigImpr.i.length, 2); + assert.equal(splitWithConfigImpr.i.length, 3); assert.equal(dependencyChildImpr.i.length, 1); - assert.equal(alwaysOnWithTrackImpressionsFalse.length, 0); + assert.equal(alwaysOnWithImpressionsDisabledTrue.length, 0); assert.true(dependencyChildImpr, 'Split we wanted to evaluate should be present on the impressions.'); assert.false(data.some(e => e.f === 'hierarchical_dep_always_on'), 'Parent split evaluations should not result in impressions.'); @@ -75,6 +75,7 @@ export default async function (key, fetchMock, assert) { assert.equal(output.t, expected.treatment, 'Present impressions should have the correct treatment.'); assert.equal(output.r, expected.label, 'Present impressions should have the correct label.'); assert.equal(output.c, expected.changeNumber, 'Present impressions should have the correct changeNumber.'); + assert.equal(output.properties, expected.properties, 'Present impressions should have the correct properties.'); assert.true(output.m >= (performedWhenReady ? readyEvaluationsStart : evaluationsStart) && output.m <= evaluationsEnd, 'Present impressions should have the correct timestamp (test with error margin).'); } @@ -90,6 +91,10 @@ export default async function (key, fetchMock, assert) { keyName: 'facundo@split.io', label: 'another expected label', treatment: 'o.n', bucketingKey: 'test_buck_key', changeNumber: 828282828282 }); + validateImpressionData(splitWithConfigImpr.i[2], { + keyName: 'other_key', label: 'another expected label', treatment: 'o.n', + changeNumber: 828282828282, properties: '{"some":"value2"}' + }); // Not push impressions with a invalid key (aka matching key) assert.true( @@ -111,21 +116,21 @@ export default async function (key, fetchMock, assert) { // finding these validate the feature names collection too const splitWithConfigImpr = data.pf.filter(e => e.f === 'split_with_config')[0]; - const alwaysOnWithTrackImpressionsFalse = data.pf.filter(e => e.f === 'always_on_track_impressions_false')[0]; + const alwaysOnWithImpressionsDisabledTrue = data.pf.filter(e => e.f === 'always_on_impressions_disabled_true')[0]; assert.equal(splitWithConfigImpr.rc, 1); assert.equal(typeof splitWithConfigImpr.m, 'number'); assert.equal(splitWithConfigImpr.m, truncatedTimeFrame); - assert.equal(alwaysOnWithTrackImpressionsFalse.rc, 1); - assert.equal(typeof alwaysOnWithTrackImpressionsFalse.m, 'number'); - assert.equal(alwaysOnWithTrackImpressionsFalse.m, truncatedTimeFrame); + assert.equal(alwaysOnWithImpressionsDisabledTrue.rc, 1); + assert.equal(typeof alwaysOnWithImpressionsDisabledTrue.m, 'number'); + assert.equal(alwaysOnWithImpressionsDisabledTrue.m, truncatedTimeFrame); return 200; }); fetchMock.postOnce(url(settings, '/v1/keys/ss'), (url, opts) => { assert.deepEqual(JSON.parse(opts.body), { - keys: [{ f: 'always_on_track_impressions_false', ks: ['other_key'] }] + keys: [{ f: 'always_on_impressions_disabled_true', ks: ['other_key'] }] }, 'We should only track unique keys for features flags with track impressions disabled.'); return 200; @@ -153,8 +158,11 @@ export default async function (key, fetchMock, assert) { client.getTreatmentWithConfig({ matchingKey: key, bucketingKey: 'test_buck_key' }, 'split_with_config'); client.getTreatmentWithConfig({ matchingKey: 'different', bucketingKey: 'test_buck_key' }, 'split_with_config'); - // Impression should not be tracked - assert.equal(client.getTreatment('other_key', 'always_on_track_impressions_false'), 'on'); + // Impression should not be tracked (passed properties will not be submitted) + assert.equal(client.getTreatment('other_key', 'always_on_impressions_disabled_true'), 'on', undefined, { properties: { some: 'value1' } }); + + // Tracked impression with properties should be handled in DEBUG mode + assert.equal(client.getTreatment('other_key', 'split_with_config', undefined, { properties: { some: 'value2' } }), 'o.n'); evaluationsEnd = Date.now(); } diff --git a/src/factory/browser.js b/src/factory/browser.js index 8b71ed235..da9ca826c 100644 --- a/src/factory/browser.js +++ b/src/factory/browser.js @@ -20,10 +20,10 @@ const syncManagerOnlineCSFactory = syncManagerOnlineFactory(pollingManagerCSFact function getStorage(settings) { return settings.storage.type === STORAGE_LOCALSTORAGE ? - InLocalStorage(settings.storage) - : settings.storage.__originalType === STORAGE_LOCALSTORAGE ? - __InLocalStorageMockFactory - : InMemoryStorageCSFactory; + InLocalStorage(settings.storage) : + settings.storage.__originalType === STORAGE_LOCALSTORAGE ? + __InLocalStorageMockFactory : + InMemoryStorageCSFactory; } /** diff --git a/src/settings/defaults/version.js b/src/settings/defaults/version.js index eb209ae90..e116c4aa8 100644 --- a/src/settings/defaults/version.js +++ b/src/settings/defaults/version.js @@ -1 +1 @@ -export const packageVersion = '11.1.0'; +export const packageVersion = '11.2.0'; diff --git a/src/settings/storage/browser.js b/src/settings/storage/browser.js index 10a9e3eea..0165d45e3 100644 --- a/src/settings/storage/browser.js +++ b/src/settings/storage/browser.js @@ -10,7 +10,9 @@ export function validateStorage(settings) { storage: { type, options = {}, - prefix + prefix, + expirationDays, + clearOnInit } = { type: STORAGE_MEMORY }, } = settings; let __originalType; @@ -38,6 +40,8 @@ export function validateStorage(settings) { type, options, prefix, + expirationDays, + clearOnInit, __originalType }; } diff --git a/ts-tests/index.ts b/ts-tests/index.ts index 823e00771..cc6421447 100644 --- a/ts-tests/index.ts +++ b/ts-tests/index.ts @@ -531,7 +531,9 @@ let fullBrowserSettings: SplitIO.IBrowserSettings = { features: mockedFeaturesMap, storage: { type: 'LOCALSTORAGE', - prefix: 'PREFIX' + prefix: 'PREFIX', + expirationDays: 1, + clearOnInit: true }, impressionListener: impressionListener, debug: true,