Skip to content

Commit

Permalink
get rid of drained delay because not needed for now
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Sep 29, 2022
1 parent dec2a8f commit efb08ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 48 deletions.
49 changes: 13 additions & 36 deletions packages/node/src/__tests__/graceful-shutdown-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('Ability for users to exit without losing events', () => {
beforeEach(async () => {
jest.resetAllMocks()
ajs = new AnalyticsNode({
drainedDelay: 100,
writeKey: 'abc123',
})
})
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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++
Expand Down
11 changes: 1 addition & 10 deletions packages/node/src/app/analytics-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ export class AnalyticsNode
implements CoreAnalytics
{
private _eventFactory: EventFactory
private _drainedEventEmitTimeout?: ReturnType<typeof setTimeout>
private _drainedDelay: number
private _isClosed = false
private _pendingEvents = 0

Expand All @@ -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))

Expand Down Expand Up @@ -134,10 +131,6 @@ export class AnalyticsNode

this._pendingEvents++

if (this._drainedEventEmitTimeout) {
clearTimeout(this._drainedEventEmitTimeout)
}

dispatchAndEmit(segmentEvent, this.queue, this, {
callback: callback,
})
Expand All @@ -146,9 +139,7 @@ export class AnalyticsNode
this._pendingEvents--

if (!this._pendingEvents) {
this._drainedEventEmitTimeout = setTimeout(() => {
this.emit('drained')
}, this._drainedDelay)
this.emit('drained')
}
})
}
Expand Down
2 changes: 0 additions & 2 deletions packages/node/src/app/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit efb08ac

Please sign in to comment.