| 
 | 1 | +'use strict';  | 
 | 2 | +const common = require('../common');  | 
 | 3 | +const assert = require('assert');  | 
 | 4 | +const http = require('http');  | 
 | 5 | + | 
 | 6 | +// To test the LIFO behavior of the agent:  | 
 | 7 | +// Send a request and store the local TCP port  | 
 | 8 | +// number used to send a request to the server.  | 
 | 9 | + | 
 | 10 | +// Store a list of port numbers used to process requests...  | 
 | 11 | +let port_list = [];  | 
 | 12 | + | 
 | 13 | +const server = http.createServer(common.mustCallAtLeast((req, res) => {  | 
 | 14 | +  // Return the remote port number used for this connection.  | 
 | 15 | +  res.end(req.socket.remotePort.toString(10));  | 
 | 16 | +}), 6);  | 
 | 17 | + | 
 | 18 | +server.listen(0, function() {  | 
 | 19 | +  const agent = http.Agent({  | 
 | 20 | +    keepAlive: true,  | 
 | 21 | +    maxSockets: 4  | 
 | 22 | +  });  | 
 | 23 | + | 
 | 24 | +  // Fill the agent with connections  | 
 | 25 | +  const fill_request_count = agent.maxSockets;  | 
 | 26 | + | 
 | 27 | +  makeReqs(fill_request_count, function(er) {  | 
 | 28 | +    assert.ifError(er);  | 
 | 29 | +    // Assert that different ports were used.  | 
 | 30 | +    assert.strictEqual(port_list.length, fill_request_count);  | 
 | 31 | + | 
 | 32 | +    // Now the agent is fully populated with connections to the server  | 
 | 33 | +    // just waiting to be reused.  | 
 | 34 | + | 
 | 35 | +    // For LIFO behavior the last used port should always be used  | 
 | 36 | +    // to send a serial string of requests.  | 
 | 37 | + | 
 | 38 | +    // If there was FIFO behavior for each serial request a  | 
 | 39 | +    // the oldest used port should be used to send the request.  | 
 | 40 | + | 
 | 41 | +    const fill_ports = port_list;  | 
 | 42 | +    port_list = [];  | 
 | 43 | +    makeReq(common.mustCall(() => {  | 
 | 44 | +      // Since the agent's free socket list isn't updated  | 
 | 45 | +      // until after the response is processed. Send the next  | 
 | 46 | +      // request completely after the first one is finished processing.  | 
 | 47 | +      process.nextTick(common.mustCall(() => {  | 
 | 48 | +        makeReq(common.mustCall(() => {  | 
 | 49 | +          // LIFO should always use the last used port to send the next  | 
 | 50 | +          // request, so verify that behavior.  | 
 | 51 | +          const lastPort = fill_ports[fill_ports.length - 1];  | 
 | 52 | +          assert.deepStrictEqual(port_list, [lastPort, lastPort]);  | 
 | 53 | +          agent.destroy();  | 
 | 54 | +          server.close();  | 
 | 55 | +        }));  | 
 | 56 | +      }));  | 
 | 57 | +    }));  | 
 | 58 | +  });  | 
 | 59 | + | 
 | 60 | +  process.on('exit', function() {  | 
 | 61 | +    console.log('ok');  | 
 | 62 | +  });  | 
 | 63 | + | 
 | 64 | +  function makeReqs(n, cb) {  | 
 | 65 | +    for (let i = 0; i < n; i++) makeReq(then);  | 
 | 66 | + | 
 | 67 | +    function then(er) {  | 
 | 68 | +      if (er) return cb(er);  | 
 | 69 | +      else if (--n === 0) process.nextTick(cb);  | 
 | 70 | +    }  | 
 | 71 | +  }  | 
 | 72 | + | 
 | 73 | +  function makeReq(cb) {  | 
 | 74 | +    http  | 
 | 75 | +      .request(  | 
 | 76 | +        {  | 
 | 77 | +          port: server.address().port,  | 
 | 78 | +          path: '/',  | 
 | 79 | +          agent: agent  | 
 | 80 | +        },  | 
 | 81 | +        function(res) {  | 
 | 82 | +          let data = '';  | 
 | 83 | +          res.setEncoding('ascii');  | 
 | 84 | +          res.on('data', function(c) {  | 
 | 85 | +            data += c;  | 
 | 86 | +          });  | 
 | 87 | +          res.on('end', function() {  | 
 | 88 | +            port_list.push(data);  | 
 | 89 | +            cb();  | 
 | 90 | +          });  | 
 | 91 | +        }  | 
 | 92 | +      )  | 
 | 93 | +      .end();  | 
 | 94 | +  }  | 
 | 95 | +});  | 
0 commit comments