| 1 |
/* |
| 2 |
* port-finder-0-vs-127-test.js: Test for the `portfinder` module. |
| 3 |
* that demonstrates issue #24 |
| 4 |
* https://github.com/http-party/node-portfinder/issues/24 |
| 5 |
*/ |
| 6 |
|
| 7 |
|
| 8 |
const portfinder = require('../lib/portfinder'), |
| 9 |
child_process = require('child_process'), |
| 10 |
path = require('path'), |
| 11 |
http = require('http'); |
| 12 |
|
| 13 |
const host = "localhost"; |
| 14 |
let server; |
| 15 |
|
| 16 |
describe('portfinder', function () { |
| 17 |
afterAll(function (done) { |
| 18 |
if (server) { |
| 19 |
server.close(done); |
| 20 |
} |
| 21 |
}); |
| 22 |
|
| 23 |
test('ensure that infinite loop is not triggered', function (done) { |
| 24 |
portfinder.getPort(function (err, port) { |
| 25 |
expect(err).toBeNull(); |
| 26 |
server = http.createServer(function () {}).listen(port, host, function () { |
| 27 |
let timeout = false; |
| 28 |
const fileToExec = path.join(__dirname, 'getPort.js'); |
| 29 |
const timer = setTimeout(function () { |
| 30 |
timeout = true; |
| 31 |
process.kill(child.pid); |
| 32 |
done("timeout"); |
| 33 |
}, 10000); // 10 seconds |
| 34 |
const child = child_process.spawn('node', [fileToExec, port]); |
| 35 |
child.on('close', function () { |
| 36 |
if (timeout === false) { |
| 37 |
clearTimeout(timer); |
| 38 |
done(); |
| 39 |
} |
| 40 |
}); |
| 41 |
}); |
| 42 |
}); |
| 43 |
}, 12000); |
| 44 |
}); |