diff --git a/packages/cli/src/modules/insights/__tests__/insights.service.test.ts b/packages/cli/src/modules/insights/__tests__/insights.service.test.ts index d7fe19bd68..b01be93751 100644 --- a/packages/cli/src/modules/insights/__tests__/insights.service.test.ts +++ b/packages/cli/src/modules/insights/__tests__/insights.service.test.ts @@ -544,6 +544,29 @@ describe('workflowExecuteAfterHandler - flushEvents', () => { } }); + test('reschedule flush on no buffered insights', async () => { + // ARRANGE + jest.useFakeTimers(); + trxMock.insert.mockClear(); + insightsService.startBackgroundProcess(); + const flushEventsSpy = jest.spyOn(insightsService, 'flushEvents'); + + try { + // ACT + await jest.advanceTimersByTimeAsync(31 * 1000); + + // ASSERT + expect(flushEventsSpy).toHaveBeenCalledTimes(1); + expect(trxMock.insert).not.toHaveBeenCalled(); + + // ACT + await jest.advanceTimersByTimeAsync(31 * 1000); + expect(flushEventsSpy).toHaveBeenCalledTimes(2); + } finally { + jest.useRealTimers(); + } + }); + test('flushes events to the database on shutdown', async () => { // ARRANGE trxMock.insert.mockClear(); diff --git a/packages/cli/src/modules/insights/insights.service.ts b/packages/cli/src/modules/insights/insights.service.ts index 3b8e1b64ae..da97477dbd 100644 --- a/packages/cli/src/modules/insights/insights.service.ts +++ b/packages/cli/src/modules/insights/insights.service.ts @@ -259,6 +259,8 @@ export class InsightsService { async flushEvents() { // Prevent flushing if there are no events to flush if (this.bufferedInsights.size === 0) { + // reschedule the timer to flush again + this.startFlushingScheduler(); return; }