Skip to content

Commit

Permalink
Wait for _close to finish before resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Feb 16, 2016
1 parent a0a0c61 commit 0c57c1f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/usb_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ USB.Connection.prototype.setAltSetting = function() {
};

USB.Connection.prototype.end = function() {
var self = this;
return new Promise((resolve, reject) => {
// Tell the USB daemon to end all processes active
// on account of this connection
Daemon.deregister(this, (err) => {
this._close();
Daemon.deregister(self, function(err) {
if (err) {
reject(err);
} else {
resolve();
self._close(resolve());
}
});
});
Expand Down
96 changes: 96 additions & 0 deletions test/unit/usb_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,102 @@ exports['USB.Connection.prototype.open'] = {

};

exports['USB.Connection.prototype.end'] = {
setUp: function(done) {
var self = this;
this.sandbox = sinon.sandbox.create();
this.err = this.sandbox.stub(logs, 'err');
this.processExit = this.sandbox.stub(process, 'exit');
this.usbConnection = new USB.Connection({});
this.usbConnection.epOut = new Emitter();
this.usbConnection.epOut.transfer = this.sandbox.spy();
this.usbConnection.epIn = new Emitter();
this.usbConnection.epIn.startPoll = this.sandbox.spy();
this.closeFunc = this.sandbox.spy(this.usbConnection, '_close');
this.fakeInterface = {
claim: function() {},
setAltSetting: function(arg1, cb) {
cb();
},
endpoints: [self.usbConnection.epIn, self.usbConnection.epOut],
};
this.sandbox.stub(this.usbConnection, 'device', {
open: function() {},
interface: function() {
return self.fakeInterface;
},
getStringDescriptor: function(arg1, cb) {
cb();
},
deviceDescriptor: {
iSerialNumber: 'blah',
}
});
this.openDevice = this.sandbox.spy(this.usbConnection.device, 'open');
this.interface = this.sandbox.spy(this.usbConnection.device, 'interface');
this.claim = this.sandbox.spy(this.fakeInterface, 'claim');
this.setAltSetting = this.sandbox.spy(this.fakeInterface, 'setAltSetting');
this.getStringDescriptor = this.sandbox.spy(this.usbConnection.device, 'getStringDescriptor');
this.daemonRegister = this.sandbox.spy(Daemon, 'register');
done();
},

tearDown: function(done) {
this.sandbox.restore();
done();
},

deregistFailNoClose: function(test) {
test.expect(2);

this.daemonDeregister = this.sandbox.stub(Daemon, 'deregister', (val, cb) => {
cb('some error');
});
this.usbConnection.open()
.then(() => this.usbConnection.end())
.then(() => {
// It should not succeed
test.fail('End resolved even with failed deregister');
test.done();
})
.catch(() => {
test.equal(this.daemonDeregister.called, true);
test.equal(this.closeFunc.called, false);
test.done();
});
},
resolveWaitOnClose: function(test) {
test.expect(3);

this.daemonDeregister = this.sandbox.stub(Daemon, 'deregister', (val, cb) => {
// deregister was a success
cb();
});

var waited = false;
this.closeFunc.restore();
this.closeFunc = this.sandbox.stub(this.usbConnection, '_close', (cb) => {
waited = true;
cb();
});

this.usbConnection.open()
.then(() => this.usbConnection.end())
.then(() => {
test.equal(this.daemonDeregister.called, true);
// We did call the close function this time
test.equal(this.closeFunc.called, true);
test.equal(waited, true);
test.done();
})
.catch(() => {
// It should not succeed
test.fail('proper connection.end should not fail');
test.done();
});
},
};

exports['USB.Connection.prototype._receiveMessages'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
Expand Down

0 comments on commit 0c57c1f

Please sign in to comment.