| 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 portfinder = require('../lib/portfinder'), |
| 11 |
helper = require('./helper'); |
| 12 |
|
| 13 |
const basePort = 32768; |
| 14 |
portfinder.basePort = basePort; |
| 15 |
|
| 16 |
describe('with 5 existing servers', function () { |
| 17 |
const servers = []; |
| 18 |
beforeAll(function (done) { |
| 19 |
helper.startServers(servers, done); |
| 20 |
}); |
| 21 |
|
| 22 |
afterAll(function (done) { |
| 23 |
helper.stopServers(servers, done); |
| 24 |
}); |
| 25 |
|
| 26 |
describe.each([ |
| 27 |
['getPort()', false, portfinder.getPort], |
| 28 |
['getPort()', true, portfinder.getPort], |
| 29 |
['getPortPromise()', true, portfinder.getPortPromise], |
| 30 |
])(`the %s method (promise: %p)`, function (name, isPromise, method) { |
| 31 |
test('should respond with the first free port (32773)', function (done) { |
| 32 |
if (isPromise) { |
| 33 |
method() |
| 34 |
.then(function (port) { |
| 35 |
expect(port).toEqual(32773); |
| 36 |
done(); |
| 37 |
}) |
| 38 |
.catch(function (err) { |
| 39 |
done(err); |
| 40 |
}); |
| 41 |
} else { |
| 42 |
method(function (err, port) { |
| 43 |
if (err) { |
| 44 |
done(err); |
| 45 |
return; |
| 46 |
} |
| 47 |
expect(err).toBeNull(); |
| 48 |
expect(port).toEqual(32773); |
| 49 |
done(); |
| 50 |
}); |
| 51 |
} |
| 52 |
}); |
| 53 |
|
| 54 |
test('with user passed duplicate host', function (done) { |
| 55 |
if (isPromise) { |
| 56 |
method({ host: 'localhost' }) |
| 57 |
.then(function (port) { |
| 58 |
expect(port).toEqual(32773); |
| 59 |
done(); |
| 60 |
}) |
| 61 |
.catch(function (err) { |
| 62 |
done(err); |
| 63 |
}); |
| 64 |
} else { |
| 65 |
method({ host: 'localhost' }, function (err, port) { |
| 66 |
if (err) { |
| 67 |
done(err); |
| 68 |
return; |
| 69 |
} |
| 70 |
expect(err).toBeNull(); |
| 71 |
expect(port).toEqual(32773); |
| 72 |
done(); |
| 73 |
}); |
| 74 |
} |
| 75 |
}); |
| 76 |
|
| 77 |
test('with stopPort smaller than available port', function (done) { |
| 78 |
// stopPort: 32772 is smaller than available port 32773 (32768 + 5) |
| 79 |
if (isPromise) { |
| 80 |
method({ stopPort: 32772 }) |
| 81 |
.then(function () { |
| 82 |
done('Expected error to be thrown'); |
| 83 |
}) |
| 84 |
.catch(function (err) { |
| 85 |
expect(err).not.toBeNull(); |
| 86 |
expect(err.message).toEqual('No open ports found in between 32768 and 32772'); |
| 87 |
done(); |
| 88 |
}); |
| 89 |
} else { |
| 90 |
method({ stopPort: 32772 }, function (err, port) { |
| 91 |
expect(err).not.toBeNull(); |
| 92 |
expect(err.message).toEqual('No open ports found in between 32768 and 32772'); |
| 93 |
expect(port).toBeUndefined(); |
| 94 |
done(); |
| 95 |
}); |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
test('should respond with the first free port (32773) less than provided stopPort', function (done) { |
| 100 |
// stopPort: 32774 is greater than available port 32773 (32768 + 5) |
| 101 |
if (isPromise) { |
| 102 |
method({ stopPort: 32774 }) |
| 103 |
.then(function (port) { |
| 104 |
expect(port).toEqual(32773); |
| 105 |
done(); |
| 106 |
}) |
| 107 |
.catch(function (err) { |
| 108 |
done(err); |
| 109 |
}); |
| 110 |
} else { |
| 111 |
method({ stopPort: 32774 }, function (err, port) { |
| 112 |
if (err) { |
| 113 |
done(err); |
| 114 |
return; |
| 115 |
} |
| 116 |
expect(err).toBeNull(); |
| 117 |
expect(port).toEqual(32773); |
| 118 |
done(); |
| 119 |
}); |
| 120 |
} |
| 121 |
}); |
| 122 |
}); |
| 123 |
}); |
| 124 |
|
| 125 |
describe('with no existing servers', function () { |
| 126 |
describe.each([ |
| 127 |
['getPort()', false, portfinder.getPort], |
| 128 |
['getPort()', true, portfinder.getPort], |
| 129 |
['getPortPromise()', true, portfinder.getPortPromise], |
| 130 |
])(`the %s method (promise: %p)`, function (name, isPromise, method) { |
| 131 |
test('should respond with the first free port (32768)', function (done) { |
| 132 |
if (isPromise) { |
| 133 |
method() |
| 134 |
.then(function (port) { |
| 135 |
expect(port).toEqual(32768); |
| 136 |
done(); |
| 137 |
}) |
| 138 |
.catch(function (err) { |
| 139 |
done(err); |
| 140 |
}); |
| 141 |
} else { |
| 142 |
method(function (err, port) { |
| 143 |
if (err) { |
| 144 |
done(err); |
| 145 |
return; |
| 146 |
} |
| 147 |
expect(err).toBeNull(); |
| 148 |
expect(port).toEqual(32768); |
| 149 |
done(); |
| 150 |
}); |
| 151 |
} |
| 152 |
}); |
| 153 |
}); |
| 154 |
}); |
| 155 |
|
| 156 |
|
| 157 |
describe('with startPort provided', function () { |
| 158 |
beforeAll(function () { |
| 159 |
portfinder.basePort = 8000; |
| 160 |
}); |
| 161 |
afterAll(function () { |
| 162 |
portfinder.basePort = basePort; |
| 163 |
}); |
| 164 |
describe.each([ |
| 165 |
['getPort()', false, portfinder.getPort], |
| 166 |
['getPort()', true, portfinder.getPort], |
| 167 |
['getPortPromise()', true, portfinder.getPortPromise], |
| 168 |
])(`the %s method (promise: %p)`, function (name, isPromise, method) { |
| 169 |
test('with startPort less than or equal to 9050', function (done) { |
| 170 |
if (isPromise) { |
| 171 |
method({ startPort: 9050 }) |
| 172 |
.then(function (port) { |
| 173 |
expect(port).toBeGreaterThanOrEqual(9050); |
| 174 |
done(); |
| 175 |
}) |
| 176 |
.catch(function (err) { |
| 177 |
done(err); |
| 178 |
}); |
| 179 |
} else { |
| 180 |
method({ startPort: 9050 }, function (err, port) { |
| 181 |
if (err) { |
| 182 |
done(err); |
| 183 |
return; |
| 184 |
} |
| 185 |
expect(err).toBeNull(); |
| 186 |
expect(port).toBeGreaterThanOrEqual(9050); |
| 187 |
done(); |
| 188 |
}); |
| 189 |
} |
| 190 |
}); |
| 191 |
}); |
| 192 |
}); |
| 193 |
|
| 194 |
describe('with no available ports above the start port', function () { |
| 195 |
const servers = []; |
| 196 |
beforeEach(function (done) { |
| 197 |
helper.startServers(servers, 65530, 65536, done); |
| 198 |
}); |
| 199 |
afterEach(function (done) { |
| 200 |
helper.stopServers(servers, done); |
| 201 |
}); |
| 202 |
|
| 203 |
describe.each([ |
| 204 |
['getPort()', false, portfinder.getPort], |
| 205 |
['getPort()', true, portfinder.getPort], |
| 206 |
['getPortPromise()', true, portfinder.getPortPromise], |
| 207 |
])(`the %s method (promise: %p)`, function (name, isPromise, method) { |
| 208 |
test('the getPort() method requesting an unavailable port', function (done) { |
| 209 |
if (isPromise) { |
| 210 |
method({ port: 65530 }) |
| 211 |
.then(function () { |
| 212 |
done('Expected error to be thrown'); |
| 213 |
}) |
| 214 |
.catch(function (err) { |
| 215 |
expect(err).not.toBeNull(); |
| 216 |
expect(err.message).toEqual('No open ports available'); |
| 217 |
done(); |
| 218 |
}); |
| 219 |
} else { |
| 220 |
method({ port: 65530 }, function (err, port) { |
| 221 |
expect(err).not.toBeNull(); |
| 222 |
expect(err.message).toEqual('No open ports available'); |
| 223 |
expect(port).toBeUndefined(); |
| 224 |
done(); |
| 225 |
}); |
| 226 |
} |
| 227 |
}); |
| 228 |
}); |
| 229 |
}); |
| 230 |
|
| 231 |
test('should return error if port is negative', function (done) { |
| 232 |
portfinder.getPort({ port: -1 }, function (err, port) { |
| 233 |
expect(err).not.toBeNull(); |
| 234 |
expect(err.message).toEqual('Provided options.port(-1) is less than 0, which are cannot be bound.'); |
| 235 |
expect(port).toBeUndefined(); |
| 236 |
done(); |
| 237 |
}); |
| 238 |
}); |
| 239 |
|
| 240 |
test('should return error if port is less than stopPort', function (done) { |
| 241 |
portfinder.getPort({ port: 32768, stopPort: 32767 }, function (err, port) { |
| 242 |
expect(err).not.toBeNull(); |
| 243 |
expect(err.message).toEqual('Provided options.stopPort(32767) is less than options.port(32768)'); |
| 244 |
expect(port).toBeUndefined(); |
| 245 |
done(); |
| 246 |
}); |
| 247 |
}); |