From efb08ac3f95864c4b7ad5e5ac1658f955dcbb627 Mon Sep 17 00:00:00 2001 From: Seth Silesky Date: Thu, 29 Sep 2022 12:28:44 -0500 Subject: [PATCH] get rid of drained delay because not needed for now --- .../graceful-shutdown-integration.test.ts | 49 +++++-------------- packages/node/src/app/analytics-node.ts | 11 +---- packages/node/src/app/settings.ts | 2 - 3 files changed, 14 insertions(+), 48 deletions(-) diff --git a/packages/node/src/__tests__/graceful-shutdown-integration.test.ts b/packages/node/src/__tests__/graceful-shutdown-integration.test.ts index ac1faa4a2..a4d9d666d 100644 --- a/packages/node/src/__tests__/graceful-shutdown-integration.test.ts +++ b/packages/node/src/__tests__/graceful-shutdown-integration.test.ts @@ -20,7 +20,6 @@ describe('Ability for users to exit without losing events', () => { beforeEach(async () => { jest.resetAllMocks() ajs = new AnalyticsNode({ - drainedDelay: 100, writeKey: 'abc123', }) }) @@ -36,45 +35,23 @@ describe('Ability for users to exit without losing events', () => { } describe('drained emitted event', () => { - test('Analytics should emit a drained event and respect the drained delay', async () => { - const DRAINED_DELAY = 500 - ajs = new AnalyticsNode({ - writeKey: 'abc123', - drainedDelay: DRAINED_DELAY, - }) + test('emits a drained event if only one event is dispatched', async () => { _helpers.makeTrackCall() - const startTime = Date.now() - const drainedCbArgs = await _helpers.listenOnDrain() - const drainedTime = Date.now() - startTime - expect(drainedTime).toBeGreaterThan(DRAINED_DELAY) - expect(drainedTime).toBeLessThan(DRAINED_DELAY + 200) - - expect(drainedCbArgs).toBeUndefined() + return expect( + new Promise((resolve) => ajs.once('drained', () => resolve(undefined))) + ).resolves.toBe(undefined) }) - test('every time a new event enters the queue, the timeout should be reset (like debounce)', async () => { - const DRAINED_DELAY = 250 - ajs = new AnalyticsNode({ - writeKey: 'abc123', - drainedDelay: DRAINED_DELAY, - }) - await ajs.register({ - ...testPlugin, - track: async (ctx) => { - await sleep(50) // should be - return ctx - }, + test('emits a drained event if multiple events are dispatched', async () => { + let drainedCalls = 0 + ajs.on('drained', () => { + drainedCalls++ }) - await new Promise((resolve) => - _helpers.makeTrackCall(undefined, () => resolve(undefined)) - ) _helpers.makeTrackCall() - - const startTime = Date.now() - await _helpers.listenOnDrain() - const drainedTime = Date.now() - startTime - expect(drainedTime).toBeGreaterThan(DRAINED_DELAY) - expect(drainedTime).toBeLessThan(DRAINED_DELAY + 200) + _helpers.makeTrackCall() + _helpers.makeTrackCall() + await sleep(200) + expect(drainedCalls).toBe(1) }) test('all callbacks should be called ', async () => { @@ -133,7 +110,7 @@ describe('Ability for users to exit without losing events', () => { expect(trackCallCount).toBe(1) }) - test('if queue has multiple track events, all of those items should be dispatched, and drain and track events should be emitted', async () => { + test('if queue has multiple track events, all of those items should be dispatched, and drain and track events should be emitted', async () => { let drainedCalls = 0 ajs.on('drained', () => { drainedCalls++ diff --git a/packages/node/src/app/analytics-node.ts b/packages/node/src/app/analytics-node.ts index fddd72770..b7937be8f 100644 --- a/packages/node/src/app/analytics-node.ts +++ b/packages/node/src/app/analytics-node.ts @@ -72,8 +72,6 @@ export class AnalyticsNode implements CoreAnalytics { private _eventFactory: EventFactory - private _drainedEventEmitTimeout?: ReturnType - private _drainedDelay: number private _isClosed = false private _pendingEvents = 0 @@ -84,7 +82,6 @@ export class AnalyticsNode constructor(settings: AnalyticsNodeSettings) { super() validateSettings(settings) - this._drainedDelay = settings.drainedDelay ?? 500 this._eventFactory = new EventFactory() this.queue = new EventQueue(new NodePriorityQueue(3)) @@ -134,10 +131,6 @@ export class AnalyticsNode this._pendingEvents++ - if (this._drainedEventEmitTimeout) { - clearTimeout(this._drainedEventEmitTimeout) - } - dispatchAndEmit(segmentEvent, this.queue, this, { callback: callback, }) @@ -146,9 +139,7 @@ export class AnalyticsNode this._pendingEvents-- if (!this._pendingEvents) { - this._drainedEventEmitTimeout = setTimeout(() => { - this.emit('drained') - }, this._drainedDelay) + this.emit('drained') } }) } diff --git a/packages/node/src/app/settings.ts b/packages/node/src/app/settings.ts index ef602e9e3..fc5a229b2 100644 --- a/packages/node/src/app/settings.ts +++ b/packages/node/src/app/settings.ts @@ -4,8 +4,6 @@ export interface AnalyticsNodeSettings { writeKey: string timeout?: number plugins?: CorePlugin[] - /** Number of ms to wait for the queue to be empty before emitting a 'drained' event */ - drainedDelay?: number } export const validateSettings = (settings: AnalyticsNodeSettings) => {