forked from josdejong/workerpool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes josdejong#58] Allocate Debug ports safely, so multiple pools d…
…on’t accidentally overlap
- Loading branch information
1 parent
da0c602
commit 76e48fa
Showing
5 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var MAX_PORTS = 65535; | ||
module.exports = DebugPortAllocator; | ||
function DebugPortAllocator() { | ||
this.ports = Object.create(null); | ||
this.length = 0; | ||
} | ||
|
||
DebugPortAllocator.prototype.nextAvailableStartingAt = function(starting) { | ||
while (this.ports[starting] === true) { | ||
starting++; | ||
} | ||
|
||
if (starting >= MAX_PORTS) { | ||
throw new Error('WorkerPool debug port limit reached: ' + starting + '>= ' + MAX_PORTS ); | ||
} | ||
|
||
this.ports[starting] = true; | ||
this.length++; | ||
return starting; | ||
}; | ||
|
||
DebugPortAllocator.prototype.releasePort = function(port) { | ||
delete this.ports[port]; | ||
this.length--; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var DebugPortAllocator = require('../lib/debug-port-allocator'); | ||
|
||
describe('DebugPortAllocator', function () { | ||
it('works', function() { | ||
var allocator = new DebugPortAllocator(); | ||
assert.equal(allocator.length, 0); | ||
assert.equal(allocator.nextAvailableStartingAt(5), 5); | ||
assert.equal(allocator.length, 1); | ||
assert.equal(allocator.nextAvailableStartingAt(5), 6); | ||
assert.equal(allocator.length, 2); | ||
assert.equal(allocator.nextAvailableStartingAt(5), 7); | ||
assert.equal(allocator.length, 3); | ||
assert.equal(allocator.nextAvailableStartingAt(4), 4); | ||
assert.equal(allocator.length, 4); | ||
assert.equal(allocator.nextAvailableStartingAt(4), 8); | ||
assert.equal(allocator.length, 5); | ||
|
||
allocator.releasePort(8); | ||
assert.equal(allocator.length, 4); | ||
assert.equal(allocator.nextAvailableStartingAt(8), 8); | ||
allocator.releasePort(8); | ||
allocator.releasePort(7); | ||
allocator.releasePort(6); | ||
allocator.releasePort(5); | ||
allocator.releasePort(4); | ||
assert.equal(allocator.length, 0); | ||
|
||
assert.throws(function() { | ||
allocator.nextAvailableStartingAt(65535); | ||
}, /WorkerPool debug port limit reached/); | ||
assert.throws(function() { | ||
allocator.nextAvailableStartingAt(75535); | ||
}, /WorkerPool debug port limit reached/); | ||
}) | ||
}); | ||
|