From 55a344dceb826c5655067e6006e006846778b5e9 Mon Sep 17 00:00:00 2001 From: Miguel Casqueira Date: Fri, 28 May 2021 15:52:04 -0400 Subject: [PATCH] Prevent a recursive loop when reporting current state Closes: #1673 Change-type: patch Signed-off-by: Miguel Casqueira --- src/device-state/current-state.ts | 13 +++++-------- test/src/device-state/current-state.spec.ts | 19 ------------------- 2 files changed, 5 insertions(+), 27 deletions(-) diff --git a/src/device-state/current-state.ts b/src/device-state/current-state.ts index e3ac7b4f..7dd08dd1 100644 --- a/src/device-state/current-state.ts +++ b/src/device-state/current-state.ts @@ -203,14 +203,10 @@ const reportCurrentState = (): null => { ...currentDeviceState.dependent, }; - const stateDiff = getStateDiff(); - if (_.size(stateDiff) === 0) { - reportPending = false; - return null; - } - + // Report current state await report(); - reportCurrentState(); + // Finishing pending report + reportPending = false; } catch (e) { eventTracker.track('Device state report failure', { error: e }); // We use the poll interval as the upper limit of @@ -241,5 +237,6 @@ export const startReporting = () => { // But check once every max report frequency to ensure that changes in system // info are picked up (CPU temp etc) setInterval(doReport, constants.maxReportFrequency); - return reportCurrentState(); + // Try to perform a report right away + return doReport(); }; diff --git a/test/src/device-state/current-state.spec.ts b/test/src/device-state/current-state.spec.ts index b8ada63d..f1309181 100644 --- a/test/src/device-state/current-state.spec.ts +++ b/test/src/device-state/current-state.spec.ts @@ -505,25 +505,6 @@ describe('device-state/current-state', () => { report.cancel(); }); - it('does not report if current state has not changed', async () => { - // Use a temporary unhandledRejectionHandler to catch the promise - // rejection from the Bluebird.delay stub - process.on('unhandledRejection', unhandledRejectionHandler); - spy(_, 'size'); - - reportCurrentState(); - - // Wait 200ms for anonymous async IIFE inside reportCurrentState to finish executing - // TODO: is there a better way to test this? Possible race condition - await sleep(200); - - expect(stateForReport).to.deep.equal({ local: {}, dependent: {} }); - expect(_.size as SinonSpy).to.have.returned(0); - - (_.size as SinonSpy).restore(); - process.removeListener('unhandledRejection', unhandledRejectionHandler); - }); - it('sends a null patch for system metrics when HARDWARE_METRICS is false', async () => { // Use a temporary unhandledRejectionHandler to catch the promise // rejection from the Bluebird.delay stub