Skip to content

Commit

Permalink
Handle corner-case of current cfg being undefined
Browse files Browse the repository at this point in the history
This is a follow-up to the shadow-out-of-sync fix, which turned out to be
incomplete.
  • Loading branch information
jmattsson committed Jun 26, 2024
1 parent ec27f51 commit bd0896e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function processServiceCfg(svc, cfgOld, cfgDelta, onCfgDiff) {


function processOldReportedCfg(svc, cfgOld, onCfgDiff) {
const diff = shadowDiff(cfgOld, svc.getCurrentCfg());
const diff = shadowDiff(cfgOld, svc.getCurrentCfg() || {});
if (!svc.ephemeraldata && diff !== undefined) {
console.log(`Reported values for ${svc.key} outdated, updating.`);
onCfgDiff(diff);
Expand Down
21 changes: 21 additions & 0 deletions test/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ const svc7 = new Service({ // fixed read-back cfg
getCurrentCfg: () => slowClone({ value: 1234 }),
});

const svc8 = new Service({ // always missing cfg
key: 'svc8',
outfile: 'svc8.json',
outformat: 'JSON',
writeout: (cfg) => storeCfg(svc8, cfg),
getCurrentCfg: () => undefined,
});


// Cleanup

function clear()
Expand All @@ -124,6 +133,7 @@ function clear()
const shadow1 =
new Shadow(mock_comms, THING, { svc1, svc2, svc3, svc4, svc5, svc6 });
const shadow2 = new Shadow(mock_comms, THING, { svc7 });
const shadow3 = new Shadow(mock_comms, THING, { svc8 });

// Validate initial processing of services/configs
shadow1.onFetchStatus('accepted', { state: {} }); // fetch pending after new
Expand Down Expand Up @@ -272,3 +282,14 @@ assert.deepEqual(mock_comms.last_update_state, {
desired: null,
});
clear();

// Validate that reported shadow data gets cleared if local data missing
shadow3.fetch();
shadow3.onFetchStatus('accepted', { state: { reported: { svc8: { foo: 10 }}}});
assert.deepEqual(mock_comms.last_update_state, {
reported: {
svc8: { foo: null }
},
desired: null,
});
clear();

0 comments on commit bd0896e

Please sign in to comment.