Skip to content

Commit

Permalink
Ensure IDs are sorted prior to selecting next
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Mar 8, 2016
1 parent 4119b79 commit ed843e9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/usb/usb_daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function USBDaemon() {
this._nextID = () => {

// Get all currently used ids, what's the first available?
var usedIDs = this.getIDsInUse();
var usedIDs = this.getIDsInUse().sort();
var prev;
var cur;

Expand Down
62 changes: 62 additions & 0 deletions test/unit/daemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module.exports['Daemon._nextID'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
done();
},

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

// Returns the next element in the list when ordered with no gaps
selectSubsequentId: function(test) {
test.expect(1);
this.getIDsInUse = this.sandbox.stub(Daemon, 'getIDsInUse').returns([0, 1, 2]);
var next = Daemon._nextID();
test.equal(next, 3);
test.done();
},

// Detects gaps at the front of the id array. Returns first element in gap
selectMissingId: function(test) {
test.expect(1);
this.getIDsInUse = this.sandbox.stub(Daemon, 'getIDsInUse').returns([1, 2]);
var next = Daemon._nextID();
test.equal(next, 0);
test.done();
},

// Detects gap in middle of array
detectGapId: function(test) {
test.expect(1);
this.getIDsInUse = this.sandbox.stub(Daemon, 'getIDsInUse').returns([0, 1, 3]);
var next = Daemon._nextID();
test.equal(next, 2);
test.done();
},

// Can handle out of order ids, returns next after max or first gap
handleUnorderedIds: function(test) {
test.expect(1);
this.getIDsInUse = this.sandbox.stub(Daemon, 'getIDsInUse').returns([2, 3, 1]);
var next = Daemon._nextID();
test.equal(next, 0);
test.done();
},

// Will not return a new id if we already have 255 processes running
throwOnMaxIds: function(test) {
test.expect(1);

var maxIds = 255;
var idsInUse = [];
for (var i = 0; i < maxIds; i++) {
idsInUse.push(i);
}

this.getIDsInUse = this.sandbox.stub(Daemon, 'getIDsInUse').returns(idsInUse);
test.throws(Daemon._nextID());
test.done();
}
};

0 comments on commit ed843e9

Please sign in to comment.