/
port-finder-socket-test.js
140 lines (129 loc) · 3.89 KB
/
port-finder-socket-test.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* portfinder-test.js: Tests for the `portfinder` module.
*
* (C) 2011, Charlie Robbins
*
*/
"use strict";
var assert = require('assert'),
exec = require('child_process').exec,
net = require('net'),
path = require('path'),
async = require('async'),
vows = require('vows'),
portfinder = require('../lib/portfinder'),
fs = require('fs'),
glob = require('glob');
var servers = [],
socketDir = path.join(__dirname, 'fixtures'),
badDir = path.join(__dirname, 'bad-dir');
function createServers (callback) {
var base = 0;
async.whilst(
function () { return base < 5 },
function (next) {
var server = net.createServer(function () { }),
name = base === 0 ? 'test.sock' : 'test' + base + '.sock',
sock = path.join(socketDir, name);
// shamelessly stolen from foreverjs,
// https://github.com/foreverjs/forever/blob/6d143609dd3712a1cf1bc515d24ac6b9d32b2588/lib/forever/worker.js#L141-L154
if (process.platform === 'win32') {
//
// Create 'symbolic' file on the system, so it can be later
// found via "forever list" since the `\\.pipe\\*` "files" can't
// be enumerated because ... Windows.
//
fs.openSync(sock, 'w');
//
// It needs the prefix, otherwise EACCESS error happens on Windows
// (no .sock extension, only named pipes with .pipe prefixes)
//
sock = '\\\\.\\pipe\\' + sock;
}
server.listen(sock, next);
base++;
servers.push(server);
}, callback);
}
function stopServers(callback, index) {
if (index < servers.length) {
servers[index].close(function (err) {
if (err) {
callback(err, false);
} else {
stopServers(callback, index + 1);
}
});
} else {
callback(null, true);
}
}
function cleanup(callback) {
fs.rmdirSync(badDir);
stopServers(callback, 0);
}
vows.describe('portfinder').addBatch({
"When using portfinder module": {
"with 5 existing servers": {
topic: function () {
createServers(function() {
portfinder.getSocket({
path: path.join(badDir, 'test.sock')
}, this.callback);
}.bind(this));
},
"the getPort() method": {
topic: function () {
portfinder.getSocket({
path: path.join(socketDir, 'test.sock')
}, this.callback);
},
"should respond with the first free socket (test5.sock)": function (err, socket) {
assert.isTrue(!err);
assert.equal(socket, path.join(socketDir, 'test5.sock'));
}
}
}
}
}).addBatch({
"When using portfinder module": {
"with no existing servers": {
"the getSocket() method": {
"with a directory that doesnt exist": {
topic: function () {
fs.rmdir(badDir, function () {
portfinder.getSocket({
path: path.join(badDir, 'test.sock')
}, this.callback);
}.bind(this));
},
"should respond with the first free socket (test.sock)": function (err, socket) {
assert.isTrue(!err);
assert.equal(socket, path.join(badDir, 'test.sock'));
}
},
"with a directory that exists": {
topic: function () {
portfinder.getSocket({
path: path.join(socketDir, 'exists.sock')
}, this.callback);
},
"should respond with the first free socket (exists.sock)": function (err, socket) {
assert.isTrue(!err);
assert.equal(socket, path.join(socketDir, 'exists.sock'));
}
}
}
}
}
}).addBatch({
"When the tests are over": {
topic: function() {
cleanup(this.callback);
},
"necessary cleanup should have taken place": function (err, wasRun) {
assert.isTrue(!err);
assert.isTrue(wasRun);
}
}
}).export(module);