Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ class Device extends EventEmitter {
async readMailbox() {
const rep = await this._request({ c: 'M' });
if (rep && rep.data) {
this.emit('mailbox', rep.data);
// `id` is an internal correlation token for the ACK mechanism,
// don't surface it to listeners of the 'mailbox' event.
const { id: _mboxId, ...emitted } = rep.data;
this.emit('mailbox', emitted);
}
return rep.data;
}
Expand Down Expand Up @@ -144,25 +147,29 @@ class Device extends EventEmitter {
const mbox = await this.readMailbox();
if (mbox) {
this._log.debug('Mailbox message', mbox);
// `id` is an internal correlation token for the ACK mechanism. strip it from what's
// exposed to tests via mboxMessages so deep-equal assertions on shape don't break.
// mbox.id is still used below for ackMailbox().
const { id: _mboxId, ...mboxClean } = mbox;
// Capture local state (mboxMessages, expectingReset, expectingSafeMode)
// BEFORE awaiting ACK. If the ACK call throws, the device-side ACK handler has already run
// and the device is proceeding and we need our local state to match. setWillDetach()/close()
// are deferred until AFTER ACK so they don't break ACK's _open() path.
let isResetMbox = false;
let isSafeModeMbox = false;
if (mbox.t && mbox.t === MailboxTypes.RESET_PENDING) {
if (mboxClean.t && mboxClean.t === MailboxTypes.RESET_PENDING) {
this._log.info('Device test notified about expected reset');
isResetMbox = true;
expectingReset = true;
mboxMessages.push(mbox);
} else if (mbox.t && mbox.t === MailboxTypes.SAFE_MODE_PENDING) {
mboxMessages.push(mboxClean);
} else if (mboxClean.t && mboxClean.t === MailboxTypes.SAFE_MODE_PENDING) {
this._log.info('Device test notified about expected safe mode');
isSafeModeMbox = true;
expectingSafeMode = true;
expectingReset = true;
mboxMessages.push(mbox);
} else if (mbox.t) {
mboxMessages.push(mbox);
mboxMessages.push(mboxClean);
} else if (mboxClean.t) {
mboxMessages.push(mboxClean);
}
if (typeof mbox.id === 'number') {
try {
Expand Down Expand Up @@ -254,7 +261,18 @@ class Device extends EventEmitter {
try {
const mbox = await this.readMailbox();
if (mbox && mbox.t) {
mboxMessages.push(mbox);
// Strip the internal correlation `id` from the test-visible message (see comment in main loop above)
const { id: _mboxId, ...mboxClean } = mbox;
mboxMessages.push(mboxClean);
// ACK so the entry is popped from the device-side queue and the kernel-bug workaround (OUT
// between control INs) stays engaged on the next read. Same swallow semantics as the main-loop ACK.
if (typeof mbox.id === 'number') {
try {
await this.ackMailbox(mbox.id);
} catch (_ackErr) {
// ACK response can race with device detach/reset at end of test, consume.
}
}
} else {
break;
}
Expand Down
26 changes: 5 additions & 21 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"particle-usb": "^4.0.2",
"shellsubstitute": "^1.2.0",
"simple-git": "^3.16.0",
"tmp": "^0.1.0"
"tmp": "^0.2.6"
},
"devDependencies": {
"chai-as-promised": "^7.1.1",
Expand Down
Loading