|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const net = require('net'); |
| 5 | + |
| 6 | + |
| 7 | +// With only a callback, server should get a port assigned by the OS |
| 8 | +{ |
| 9 | + const server = net.createServer(common.mustNotCall()); |
| 10 | + |
| 11 | + server.listen(common.mustCall(function() { |
| 12 | + assert.ok(server.address().port > 100); |
| 13 | + server.close(); |
| 14 | + })); |
| 15 | +} |
| 16 | + |
| 17 | +// No callback to listen(), assume we can bind in 100 ms |
| 18 | +{ |
| 19 | + const server = net.createServer(common.mustNotCall()); |
| 20 | + |
| 21 | + server.listen(common.PORT); |
| 22 | + |
| 23 | + setTimeout(function() { |
| 24 | + const address = server.address(); |
| 25 | + assert.strictEqual(address.port, common.PORT); |
| 26 | + |
| 27 | + if (address.family === 'IPv6') |
| 28 | + assert.strictEqual(server._connectionKey, `6::::${address.port}`); |
| 29 | + else |
| 30 | + assert.strictEqual(server._connectionKey, `4:0.0.0.0:${address.port}`); |
| 31 | + |
| 32 | + server.close(); |
| 33 | + }, 100); |
| 34 | +} |
| 35 | + |
| 36 | +// Callback to listen() |
| 37 | +{ |
| 38 | + const server = net.createServer(common.mustNotCall()); |
| 39 | + |
| 40 | + server.listen(common.PORT + 1, common.mustCall(function() { |
| 41 | + assert.strictEqual(server.address().port, common.PORT + 1); |
| 42 | + server.close(); |
| 43 | + })); |
| 44 | +} |
| 45 | + |
| 46 | +// Backlog argument |
| 47 | +{ |
| 48 | + const server = net.createServer(common.mustNotCall()); |
| 49 | + |
| 50 | + server.listen(common.PORT + 2, '0.0.0.0', 127, common.mustCall(function() { |
| 51 | + assert.strictEqual(server.address().port, common.PORT + 2); |
| 52 | + server.close(); |
| 53 | + })); |
| 54 | +} |
| 55 | + |
| 56 | +// Backlog argument without host argument |
| 57 | +{ |
| 58 | + const server = net.createServer(common.mustNotCall()); |
| 59 | + |
| 60 | + server.listen(common.PORT + 3, 127, common.mustCall(function() { |
| 61 | + assert.strictEqual(server.address().port, common.PORT + 3); |
| 62 | + server.close(); |
| 63 | + })); |
| 64 | +} |
0 commit comments