From 95110dc31fac9d494c69b6ff82cd56a024ef71fa Mon Sep 17 00:00:00 2001 From: airdeploy-eng Date: Fri, 26 Feb 2021 15:53:04 +0000 Subject: [PATCH] Fixing a bug in init ingestion --- package.json | 2 +- src/index.test.ts | 6 ++---- src/ingester/strategy/groupStrategy.ts | 8 ++++++++ src/ingestion.test.ts | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index ea7d2e9..0527400 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flagger", - "version": "3.0.15", + "version": "3.0.16", "description": "An open source JavaScript SDK for feature flagging (feature gating, feature toggles) for Node.js and Javascript applications", "main": "dist/index.cjs.js", "module": "dist/index.es.js", diff --git a/src/index.test.ts b/src/index.test.ts index 9aa4e36..f08b76a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -47,13 +47,11 @@ describe('init function tests', () => { await Flagger.init({ apiKey, - sseURL, - logLevel: LogLevel.debug + sseURL }) await Flagger.init({ apiKey, - sseURL, - logLevel: LogLevel.error + sseURL }) await Flagger.shutdown() diff --git a/src/ingester/strategy/groupStrategy.ts b/src/ingester/strategy/groupStrategy.ts index 9682811..cc4706e 100644 --- a/src/ingester/strategy/groupStrategy.ts +++ b/src/ingester/strategy/groupStrategy.ts @@ -59,6 +59,7 @@ export default class GroupStrategy implements IIngestionStrategy { private detectedFlags: Set = new Set() private readonly sendDataFunction: (url: string, data: any) => Promise private ingestionPromise?: Promise + private isInitIngestionSent = false constructor({ ingestionURL, @@ -152,6 +153,13 @@ export default class GroupStrategy implements IIngestionStrategy { } private async sendData(reason: string) { + if (this.isEmpty()) { + if (this.isInitIngestionSent) { + return + } else { + this.isInitIngestionSent = true + } + } logger.debug(`${reason} triggers ingestion request`) const requestBody = { diff --git a/src/ingestion.test.ts b/src/ingestion.test.ts index 552c315..037047e 100644 --- a/src/ingestion.test.ts +++ b/src/ingestion.test.ts @@ -348,6 +348,26 @@ describe('ingestion plugin data test', () => { const errors = ajv.errorsText(ingestionValidator.errors) expect(errors).toEqual('No errors') }) + it('browser must not send more than 1 empty ingestion upon init', async () => { + const trackCallback = jest.fn() + api + .post(uri) + .optionally() + .reply(200, (_, body: any) => { + trackCallback(body) + }) + + await Flagger.init({ + apiKey, + sdkInfo: {name: JS_SDK_NAME, version}, + sseURL + }) + + await waitTime(1000) + + expect(trackCallback).toHaveBeenCalledTimes(1) + await Flagger.shutdown() + }) }) describe('Nodejs tests', () => {