Merge pull request #1715 from balena-os/1673-prevent-recursive-state-report

Prevent a recursive loop when reporting current state
This commit is contained in:
bulldozer-balena[bot] 2021-05-29 00:09:14 +00:00 committed by GitHub
commit 35d9a32a6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 27 deletions

View File

@ -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();
};

View File

@ -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