From 91aa5e9a6023f6a739441fa60421eedd4e6ed9da Mon Sep 17 00:00:00 2001 From: Emmanuel Zamora Date: Mon, 13 Jun 2022 20:40:31 -0300 Subject: [PATCH 1/2] Add single sync functionality for main client --- src/sdkClient/clientAttributesDecoration.ts | 18 ++++----- src/sync/__tests__/syncManagerOnline.spec.ts | 18 +++++++++ src/sync/syncManagerOnline.ts | 41 +++++++++++++------- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/sdkClient/clientAttributesDecoration.ts b/src/sdkClient/clientAttributesDecoration.ts index 5d55c7df..8d160108 100644 --- a/src/sdkClient/clientAttributesDecoration.ts +++ b/src/sdkClient/clientAttributesDecoration.ts @@ -5,7 +5,7 @@ import { ILogger } from '../logger/types'; import { objectAssign } from '../utils/lang/objectAssign'; /** - * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call + * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call */ export function clientAttributesDecoration(log: ILogger, client: TClient) { @@ -52,10 +52,10 @@ export function clientAttributesDecoration { @@ -101,8 +101,8 @@ export function clientAttributesDecoration { + const settings = { ...fullSettings }; + + settings.sync.singleSync = true; + + const pollingManager = { + syncAll: jest.fn(), + start: jest.fn() + }; + // @ts-ignore + const syncManager = syncManagerOnlineFactory(() => pollingManager)({ settings }); + + syncManager.start(); + + expect(pollingManager.start).not.toBeCalled(); + expect(pollingManager.syncAll).toBeCalledTimes(1); +}); diff --git a/src/sync/syncManagerOnline.ts b/src/sync/syncManagerOnline.ts index 61f0603d..73f5658a 100644 --- a/src/sync/syncManagerOnline.ts +++ b/src/sync/syncManagerOnline.ts @@ -89,15 +89,24 @@ export function syncManagerOnlineFactory( // start syncing splits and segments if (pollingManager) { - if (pushManager) { - // Doesn't call `syncAll` when the syncManager is resuming + + // If singleSync is enabled pushManager and pollingManager should not start + if (settings.sync.singleSync === true) { if (startFirstTime) { pollingManager.syncAll(); startFirstTime = false; } - pushManager.start(); } else { - pollingManager.start(); + if (pushManager) { + // Doesn't call `syncAll` when the syncManager is resuming + if (startFirstTime) { + pollingManager.syncAll(); + startFirstTime = false; + } + pushManager.start(); + } else { + pollingManager.start(); + } } } @@ -138,18 +147,22 @@ export function syncManagerOnlineFactory( return { isRunning: mySegmentsSyncTask.isRunning, start() { - if (pushManager) { - if (pollingManager!.isRunning()) { - // if doing polling, we must start the periodic fetch of data - if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); + if (settings.sync.singleSync === true) { + if (!readinessManager.isReady()) mySegmentsSyncTask.execute(); + } else { + if (pushManager) { + if (pollingManager!.isRunning()) { + // if doing polling, we must start the periodic fetch of data + if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); + } else { + // if not polling, we must execute the sync task for the initial fetch + // of segments since `syncAll` was already executed when starting the main client + mySegmentsSyncTask.execute(); + } + pushManager.add(matchingKey, mySegmentsSyncTask); } else { - // if not polling, we must execute the sync task for the initial fetch - // of segments since `syncAll` was already executed when starting the main client - mySegmentsSyncTask.execute(); + if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); } - pushManager.add(matchingKey, mySegmentsSyncTask); - } else { - if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); } }, stop() { From f03ede2ec5ae22a22fe5a16931d2956e70a6d912 Mon Sep 17 00:00:00 2001 From: Emmanuel Zamora Date: Tue, 14 Jun 2022 15:45:52 -0300 Subject: [PATCH 2/2] Add test for pushManager --- src/sync/__tests__/syncManagerOnline.spec.ts | 45 ++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/sync/__tests__/syncManagerOnline.spec.ts b/src/sync/__tests__/syncManagerOnline.spec.ts index 389f2d27..93678113 100644 --- a/src/sync/__tests__/syncManagerOnline.spec.ts +++ b/src/sync/__tests__/syncManagerOnline.spec.ts @@ -1,5 +1,9 @@ import { fullSettings } from '../../utils/settingsValidation/__tests__/settings.mocks'; import { syncTaskFactory } from './syncTask.mock'; +import { syncManagerOnlineFactory } from '../syncManagerOnline'; +import { pushManagerFactory } from '../streaming/pushManager'; +import { IPushManager } from '../streaming/types'; +import { EventEmitter } from '../../utils/MinEvents'; jest.mock('../submitters/submitterManager', () => { return { @@ -7,7 +11,16 @@ jest.mock('../submitters/submitterManager', () => { }; }); -import { syncManagerOnlineFactory } from '../syncManagerOnline'; +const paramsMock = { + platform: { + getEventSource: jest.fn(() => { return () => { }; }), + EventEmitter + }, + settings: fullSettings, + storage: {}, + readiness: {}, + start: jest.fn() +}; test('syncManagerOnline should start or not the submitter depending on user consent status', () => { const settings = { ...fullSettings }; @@ -48,13 +61,37 @@ test('syncManagerOnline should syncAll a single time in singleSync mode', () => const pollingManager = { syncAll: jest.fn(), - start: jest.fn() + start: jest.fn(), + stop: jest.fn(), + isRunning: jest.fn() }; + + const fetchAuthMock = jest.fn(); + // @ts-ignore - const syncManager = syncManagerOnlineFactory(() => pollingManager)({ settings }); + const pollingSyncManager = syncManagerOnlineFactory(() => pollingManager)({ settings }); - syncManager.start(); + pollingSyncManager.start(); expect(pollingManager.start).not.toBeCalled(); expect(pollingManager.syncAll).toBeCalledTimes(1); + + pollingSyncManager.stop(); + + const pushManager = pushManagerFactory({ // @ts-ignore + ...paramsMock, splitApi: { fetchAuth: fetchAuthMock } + }, {}) as IPushManager; + + pushManager.start = jest.fn(); + + // @ts-ignore + const pushingSyncManager = syncManagerOnlineFactory(() => pollingManager, () => pushManager)({ settings }); + + pushingSyncManager.start(); + + expect(pushManager.start).not.toBeCalled(); + expect(pollingManager.start).not.toBeCalled(); + + pushingSyncManager.stop(); + });