| 1 |
/* |
| 2 |
* portfinder-test.js: Tests for the `portfinder` module. |
| 3 |
* |
| 4 |
* (C) 2011, Charlie Robbins |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
"use strict"; |
| 9 |
|
| 10 |
const net = require('net'), |
| 11 |
path = require('path'), |
| 12 |
_async = require('async'), |
| 13 |
portfinder = require('../lib/portfinder'), |
| 14 |
fs = require('fs'); |
| 15 |
|
| 16 |
const servers = [], |
| 17 |
socketDir = path.join(__dirname, 'fixtures'), |
| 18 |
badDir = path.join(__dirname, 'bad-dir'); |
| 19 |
|
| 20 |
function createServers (callback) { |
| 21 |
let base = 0; |
| 22 |
|
| 23 |
_async.whilst( |
| 24 |
function (cb) { cb(null, base < 5); }, |
| 25 |
function (next) { |
| 26 |
const server = net.createServer(function () { }), |
| 27 |
name = base === 0 ? 'test.sock' : 'test' + base + '.sock'; |
| 28 |
const socket = path.join(socketDir, name); |
| 29 |
let sock = socket; |
| 30 |
|
| 31 |
// shamelessly stolen from foreverjs, |
| 32 |
// https://github.com/foreverjs/forever/blob/6d143609dd3712a1cf1bc515d24ac6b9d32b2588/lib/forever/worker.js#L141-L154 |
| 33 |
if (process.platform === 'win32') { |
| 34 |
// |
| 35 |
// Create 'symbolic' file on the system, so it can be later |
| 36 |
// found via "forever list" since the `\\.pipe\\*` "files" can't |
| 37 |
// be enumerated because ... Windows. |
| 38 |
// |
| 39 |
fs.openSync(sock, 'w'); |
| 40 |
|
| 41 |
// |
| 42 |
// It needs the prefix, otherwise EACCESS error happens on Windows |
| 43 |
// (no .sock extension, only named pipes with .pipe prefixes) |
| 44 |
// |
| 45 |
sock = '\\\\.\\pipe\\' + sock; |
| 46 |
} |
| 47 |
|
| 48 |
server.listen(sock, next); |
| 49 |
base++; |
| 50 |
servers.push([server, socket]); |
| 51 |
}, callback); |
| 52 |
} |
| 53 |
|
| 54 |
function stopServers(callback) { |
| 55 |
_async.each(servers, function ([server, socket], next) { |
| 56 |
server.close(function () { |
| 57 |
if (process.platform === 'win32' && fs.existsSync(socket)) { |
| 58 |
fs.unlinkSync(socket); |
| 59 |
} |
| 60 |
next(); |
| 61 |
}); |
| 62 |
}, callback); |
| 63 |
} |
| 64 |
|
| 65 |
function cleanup(callback) { |
| 66 |
if (fs.existsSync(path.join(badDir, 'deeply', 'nested'))) { |
| 67 |
fs.rmdirSync(path.join(badDir, 'deeply', 'nested')); |
| 68 |
} |
| 69 |
if (fs.existsSync(path.join(badDir, 'deeply'))) { |
| 70 |
fs.rmdirSync(path.join(badDir, 'deeply')); |
| 71 |
} |
| 72 |
if (fs.existsSync(badDir)) { |
| 73 |
fs.rmdirSync(badDir); |
| 74 |
} |
| 75 |
stopServers(callback); |
| 76 |
} |
| 77 |
|
| 78 |
describe('portfinder', function () { |
| 79 |
beforeAll(function (done) { |
| 80 |
cleanup(done); |
| 81 |
}); |
| 82 |
|
| 83 |
afterAll(function (done) { |
| 84 |
cleanup(done); |
| 85 |
}); |
| 86 |
|
| 87 |
describe('with 5 existing servers', function () { |
| 88 |
beforeAll(function (done) { |
| 89 |
createServers(done); |
| 90 |
}); |
| 91 |
|
| 92 |
afterAll(function (done) { |
| 93 |
stopServers(done); |
| 94 |
}); |
| 95 |
|
| 96 |
describe.each([ |
| 97 |
['getSocket()', false, portfinder.getSocket], |
| 98 |
['getSocket()', true, portfinder.getSocket], |
| 99 |
['getSocketPromise()', true, portfinder.getSocketPromise], |
| 100 |
])(`the %s method (promise: %p)`, function (name, isPromise, method) { |
| 101 |
test('should respond with the first free socket (test5.sock)', function (done) { |
| 102 |
if (isPromise) { |
| 103 |
method({ |
| 104 |
path: path.join(socketDir, 'test.sock') |
| 105 |
}) |
| 106 |
.then(function (socket) { |
| 107 |
expect(socket).toEqual(path.join(socketDir, 'test5.sock')); |
| 108 |
done(); |
| 109 |
}) |
| 110 |
.catch(function (err) { |
| 111 |
done(err); |
| 112 |
}); |
| 113 |
} else { |
| 114 |
method({ |
| 115 |
path: path.join(socketDir, 'test.sock'), |
| 116 |
}, function (err, socket) { |
| 117 |
if (err) { |
| 118 |
done(err); |
| 119 |
return; |
| 120 |
} |
| 121 |
expect(err).toBeNull(); |
| 122 |
expect(socket).toEqual(path.join(socketDir, 'test5.sock')); |
| 123 |
done(); |
| 124 |
}); |
| 125 |
} |
| 126 |
}); |
| 127 |
}); |
| 128 |
}); |
| 129 |
|
| 130 |
describe('with no existing servers', function () { |
| 131 |
describe.each([ |
| 132 |
['getSocket()', false, portfinder.getSocket], |
| 133 |
['getSocket()', true, portfinder.getSocket], |
| 134 |
['getSocketPromise()', true, portfinder.getSocketPromise], |
| 135 |
])(`the %s method (promise: %p)`, function (name, isPromise, method) { |
| 136 |
test("with a directory that doesn't exist should respond with the first free socket (test.sock)", function (done) { |
| 137 |
if (isPromise) { |
| 138 |
method({ |
| 139 |
path: path.join(badDir, 'test.sock'), |
| 140 |
}) |
| 141 |
.then(function (socket) { |
| 142 |
expect(socket).toEqual(path.join(badDir, 'test.sock')); |
| 143 |
done(); |
| 144 |
}) |
| 145 |
.catch(function (err) { |
| 146 |
done(err); |
| 147 |
}); |
| 148 |
} else { |
| 149 |
method({ |
| 150 |
path: path.join(badDir, 'test.sock'), |
| 151 |
}, function (err, socket) { |
| 152 |
if (err) { |
| 153 |
done(err); |
| 154 |
return; |
| 155 |
} |
| 156 |
expect(err).toBeNull(); |
| 157 |
expect(socket).toEqual(path.join(badDir, 'test.sock')); |
| 158 |
done(); |
| 159 |
}); |
| 160 |
} |
| 161 |
}); |
| 162 |
|
| 163 |
test("with a nested directory that doesn't exist should respond with the first free socket (test.sock)", function (done) { |
| 164 |
if (isPromise) { |
| 165 |
method({ |
| 166 |
path: path.join(badDir, 'deeply', 'nested', 'test.sock'), |
| 167 |
}) |
| 168 |
.then(function (socket) { |
| 169 |
expect(socket).toEqual(path.join(badDir, 'deeply', 'nested', 'test.sock')); |
| 170 |
done(); |
| 171 |
}) |
| 172 |
.catch(function (err) { |
| 173 |
done(err); |
| 174 |
}); |
| 175 |
} else { |
| 176 |
method({ |
| 177 |
path: path.join(badDir, 'deeply', 'nested', 'test.sock'), |
| 178 |
}, function (err, socket) { |
| 179 |
expect(err).toBeNull(); |
| 180 |
expect(socket).toEqual(path.join(badDir, 'deeply', 'nested', 'test.sock')); |
| 181 |
done(); |
| 182 |
}); |
| 183 |
} |
| 184 |
}); |
| 185 |
|
| 186 |
// We don't use `test.sock` here due to some race condition on Windows in freeing the `test.sock` file |
| 187 |
// when we close the servers. |
| 188 |
test('with a directory that exists should respond with the first free socket (foo.sock)', function (done) { |
| 189 |
if (isPromise) { |
| 190 |
method({ |
| 191 |
path: path.join(socketDir, 'foo.sock'), |
| 192 |
}) |
| 193 |
.then(function (socket) { |
| 194 |
expect(socket).toEqual(path.join(socketDir, 'foo.sock')); |
| 195 |
done(); |
| 196 |
}) |
| 197 |
.catch(function (err) { |
| 198 |
done(err); |
| 199 |
}); |
| 200 |
} else { |
| 201 |
method({ |
| 202 |
path: path.join(socketDir, 'foo.sock'), |
| 203 |
}, function (err, socket) { |
| 204 |
expect(err).toBeNull(); |
| 205 |
expect(socket).toEqual(path.join(socketDir, 'foo.sock')); |
| 206 |
done(); |
| 207 |
}); |
| 208 |
} |
| 209 |
}); |
| 210 |
}); |
| 211 |
}); |
| 212 |
}); |