port-finder-test.js
1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* portfinder-test.js: Tests for the `portfinder` module.
*
* (C) 2011, Charlie Robbins
*
*/
var vows = require('vows'),
assert = require('assert'),
async = require('async'),
http = require('http'),
portfinder = require('../lib/portfinder');
var servers = [];
function createServers (callback) {
var base = 8000;
async.whilst(
function () { return base < 8005 },
function (next) {
var server = http.createServer(function () { });
server.listen(base, next);
base++;
servers.push(server);
}, callback);
}
vows.describe('portfinder').addBatch({
"When using portfinder module": {
"with 5 existing servers": {
topic: function () {
createServers(this.callback);
},
"the getPort() method": {
topic: function () {
portfinder.getPort(this.callback);
},
"should respond with the first free port (8005)": function (err, port) {
assert.isTrue(!err);
assert.equal(port, 8005);
}
}
}
}
}).addBatch({
"When using portfinder module": {
"with no existing servers": {
topic: function () {
servers.forEach(function (server) {
server.close();
});
return null;
},
"the getPort() method": {
topic: function () {
portfinder.getPort(this.callback);
},
"should respond with the first free port (8000)": function (err, port) {
assert.isTrue(!err);
assert.equal(port, 8000);
}
}
}
}
}).export(module);