|
| 1 | +'use strict'; |
| 2 | +var common = require('../common'); |
| 3 | +var assert = require('assert'); |
| 4 | + |
| 5 | +var net = require('net'); |
| 6 | + |
| 7 | +// Sets the server's maxConnections property to 1. |
| 8 | +// Open 2 connections (connection 0 and connection 1). |
| 9 | +// Connection 0 should be accepted. |
| 10 | +// Connection 1 should be rejected. |
| 11 | +// Closes connection 0. |
| 12 | +// Open 2 more connections (connection 2 and 3). |
| 13 | +// Connection 2 should be accepted. |
| 14 | +// Connection 3 should be rejected. |
| 15 | + |
| 16 | +var connections = []; |
| 17 | +var received = []; |
| 18 | +var sent = []; |
| 19 | + |
| 20 | +var createConnection = function(index) { |
| 21 | + console.error('creating connection ' + index); |
| 22 | + |
| 23 | + return new Promise(function(resolve, reject) { |
| 24 | + var connection = net.createConnection(common.PORT, function() { |
| 25 | + var msg = '' + index; |
| 26 | + console.error('sending message: ' + msg); |
| 27 | + this.write(msg); |
| 28 | + sent.push(msg); |
| 29 | + }); |
| 30 | + |
| 31 | + connection.on('data', function(e) { |
| 32 | + console.error('connection ' + index + ' received response'); |
| 33 | + resolve(); |
| 34 | + }); |
| 35 | + |
| 36 | + connection.on('end', function() { |
| 37 | + console.error('ending ' + index); |
| 38 | + resolve(); |
| 39 | + }); |
| 40 | + |
| 41 | + connections[index] = connection; |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
| 45 | +var closeConnection = function(index) { |
| 46 | + console.error('closing connection ' + index); |
| 47 | + return new Promise(function(resolve, reject) { |
| 48 | + connections[index].on('end', function() { |
| 49 | + resolve(); |
| 50 | + }); |
| 51 | + connections[index].end(); |
| 52 | + }); |
| 53 | +}; |
| 54 | + |
| 55 | +var server = net.createServer(function(socket) { |
| 56 | + socket.on('data', function(data) { |
| 57 | + console.error('received message: ' + data); |
| 58 | + received.push('' + data); |
| 59 | + socket.write('acknowledged'); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +server.maxConnections = 1; |
| 64 | + |
| 65 | +server.listen(common.PORT, function() { |
| 66 | + createConnection(0) |
| 67 | + .then(createConnection.bind(null, 1)) |
| 68 | + .then(closeConnection.bind(null, 0)) |
| 69 | + .then(createConnection.bind(null, 2)) |
| 70 | + .then(createConnection.bind(null, 3)) |
| 71 | + .then(server.close.bind(server)) |
| 72 | + .then(closeConnection.bind(null, 2)); |
| 73 | +}); |
| 74 | + |
| 75 | +process.on('exit', function() { |
| 76 | + // Confirm that all connections tried to send data... |
| 77 | + assert.deepEqual(sent, [0, 1, 2, 3]); |
| 78 | + // ...but that only connections 0 and 2 were successful. |
| 79 | + assert.deepEqual(received, [0, 2]); |
| 80 | +}); |
| 81 | + |
| 82 | +process.on('unhandledRejection', function() { |
| 83 | + console.error('promise rejected'); |
| 84 | + assert.fail(null, null, 'A promise in the chain rejected'); |
| 85 | +}); |
0 commit comments