Skip to content

Instantly share code, notes, and snippets.

@fengmk2
Created August 28, 2014 11:07
Show Gist options
  • Select an option

  • Save fengmk2/6b84856de5d4dd8face7 to your computer and use it in GitHub Desktop.

Select an option

Save fengmk2/6b84856de5d4dd8face7 to your computer and use it in GitHub Desktop.
agent https keepalive
/*!
* agentkeepalive - test/https_agent.test.js
*
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var HttpsAgent = require('../').HttpsAgent;
var https = require('https');
var urlparse = require('url').parse;
var should = require('should');
var pedding = require('pedding');
var fs = require('fs');
describe.only('https_agent.test.js', function () {
var agentkeepalive = new HttpsAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 5,
maxFreeSockets: 5,
});
it('should GET / success with 200 status', function (done) {
agentkeepalive.get({
hostname: 'npm.taobao.org',
port: 443,
path: '/',
}, function (res) {
res.statusCode.should.equal(200);
done();
});
});
it('should GET / and /package/agentkeepalive use the same socket', function (done) {
var options = {
hostname: 'npm.taobao.org',
port: 443,
path: '/',
agent: agentkeepalive,
};
var remotePort = null;
agentkeepalive.get(options, function (res) {
Object.keys(agentkeepalive.sockets).should.length(1);
Object.keys(agentkeepalive.freeSockets).should.length(0);
res.statusCode.should.equal(200);
res.on('data', function (chunk) {});
res.on('end', function () {
Object.keys(agentkeepalive.sockets).should.length(1);
Object.keys(agentkeepalive.freeSockets).should.length(0);
// request again
options.path = '/package/agentkeepalive';
process.nextTick(function () {
Object.keys(agentkeepalive.sockets).should.length(1);
Object.keys(agentkeepalive.freeSockets).should.length(1);
https.get(options, function (res) {
Object.keys(agentkeepalive.sockets).should.length(1);
Object.keys(agentkeepalive.freeSockets).should.length(0);
res.statusCode.should.equal(200);
var data = null;
res.on('data', function (chunk) {});
res.on('end', function () {
Object.keys(agentkeepalive.sockets).should.length(1);
Object.keys(agentkeepalive.freeSockets).should.length(0);
process.nextTick(function () {
Object.keys(agentkeepalive.sockets).should.length(1);
Object.keys(agentkeepalive.freeSockets).should.length(1);
console.log(agentkeepalive);
done();
});
});
});
});
});
});
});
});
@fengmk2

fengmk2 commented Aug 28, 2014

Copy link
Copy Markdown
Author
https_agent.test.js
    ✓ should GET / success with 200 status (68ms)
{ domain: null,
  _events: { free: [Function] },
  _maxListeners: 10,
  defaultPort: 443,
  protocol: 'https:',
  options: 
   { maxFreeSockets: 5,
     maxSockets: 5,
     keepAliveMsecs: 1000,
     keepAlive: true,
     path: null },
  requests: {},
  sockets: { 'npm.taobao.org:443::::::::': [ [Object] ] },
  freeSockets: { 'npm.taobao.org:443::::::::': [ [Object] ] },
  keepAliveMsecs: 1000,
  keepAlive: true,
  maxSockets: 5,
  maxFreeSockets: 5,
  maxKeepAliveRequests: 0,
  createSocketCount: 2,
  timeoutSocketCount: 0,
  requestFinishedCount: 2 }
    ✓ should GET / and /package/agentkeepalive use the same socket (233ms)


  2 passing (312ms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment