agent.test.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. 'use strict';
  2. const assert = require('assert');
  3. const http = require('http');
  4. const urlparse = require('url').parse;
  5. const pedding = require('pedding');
  6. const Agent = require('../');
  7. describe('test/agent.test.js', () => {
  8. const agentkeepalive = new Agent({
  9. keepAliveTimeout: 1000,
  10. maxSockets: 5,
  11. maxFreeSockets: 5,
  12. });
  13. let port = null;
  14. const app = http.createServer((req, res) => {
  15. if (req.url === '/error') {
  16. res.destroy();
  17. return;
  18. } else if (req.url === '/hang') {
  19. // Wait forever.
  20. return;
  21. } else if (req.url === '/remote_close') {
  22. setTimeout(() => {
  23. req.connection.end();
  24. }, 500);
  25. }
  26. const info = urlparse(req.url, true);
  27. if (info.query.timeout) {
  28. setTimeout(() => {
  29. res.end(info.query.timeout);
  30. }, parseInt(info.query.timeout, 10));
  31. return;
  32. }
  33. res.end(JSON.stringify({
  34. info,
  35. url: req.url,
  36. headers: req.headers,
  37. socket: req.socket._getpeername(),
  38. }));
  39. });
  40. before(done => {
  41. app.listen(0, () => {
  42. port = app.address().port;
  43. done();
  44. });
  45. });
  46. after(done => setTimeout(done, 1500));
  47. it('should default options set right', () => {
  48. const agent = agentkeepalive;
  49. assert(agent.keepAlive === true);
  50. assert(agent.keepAliveMsecs === 1000);
  51. assert(agent.maxSockets === 5);
  52. assert(agent.maxFreeSockets === 5);
  53. assert(agent.timeout === 30000);
  54. assert(!agent.socketActiveTTL);
  55. });
  56. let remotePort = null;
  57. it('should request with connection: keep-alive with http.Agent(keepAlive=true)', done => {
  58. const agent = new http.Agent({
  59. keepAlive: true,
  60. });
  61. const req = http.request({
  62. method: 'GET',
  63. port,
  64. path: '/',
  65. agent,
  66. }, res => {
  67. assert(res.statusCode === 200);
  68. const chunks = [];
  69. res.on('data', data => {
  70. chunks.push(data);
  71. });
  72. res.on('end', () => {
  73. const data = JSON.parse(Buffer.concat(chunks));
  74. assert(data.headers.connection === 'keep-alive');
  75. done();
  76. });
  77. });
  78. req.end();
  79. });
  80. it('should request with connection: close with http.Agent()', done => {
  81. const req = http.request({
  82. method: 'GET',
  83. port,
  84. path: '/',
  85. }, res => {
  86. assert(res.statusCode === 200);
  87. const chunks = [];
  88. res.on('data', data => {
  89. chunks.push(data);
  90. });
  91. res.on('end', () => {
  92. const data = JSON.parse(Buffer.concat(chunks));
  93. assert(data.headers.connection === 'close');
  94. done();
  95. });
  96. });
  97. req.end();
  98. });
  99. it('should inactivity socket timeout', done => {
  100. const name = 'localhost:' + port + ':';
  101. const agentkeepalive = new Agent({
  102. freeSocketKeepAliveTimeout: '5s',
  103. timeout: '1s',
  104. });
  105. assert(!agentkeepalive.sockets[name]);
  106. assert(!agentkeepalive.freeSockets[name]);
  107. http.get({
  108. agent: agentkeepalive,
  109. port,
  110. path: '/',
  111. }, res => {
  112. assert(res.statusCode === 200);
  113. const chunks = [];
  114. res.on('data', data => {
  115. chunks.push(data);
  116. });
  117. res.on('end', () => {
  118. const data = JSON.parse(Buffer.concat(chunks));
  119. remotePort = data.socket.port;
  120. assert(data.headers.connection === 'keep-alive');
  121. assert(agentkeepalive.sockets[name]);
  122. assert(!agentkeepalive.freeSockets[name]);
  123. setTimeout(() => {
  124. assert(!agentkeepalive.sockets[name]);
  125. assert(agentkeepalive.freeSockets[name]);
  126. assert(agentkeepalive.freeSockets[name].length === 1);
  127. // request /hang timeout
  128. http.get({
  129. agent: agentkeepalive,
  130. port,
  131. path: '/hang',
  132. }, () => {
  133. assert(false, 'should not run this');
  134. }).on('error', err => {
  135. // TODO: should be a better error message than "socket hang up"
  136. assert(err.message === 'socket hang up');
  137. assert(err.code === 'ECONNRESET');
  138. done();
  139. });
  140. }, 20);
  141. });
  142. });
  143. });
  144. it('should request / 200 status', done => {
  145. const name = 'localhost:' + port + ':';
  146. assert(!agentkeepalive.sockets[name]);
  147. assert(!agentkeepalive.freeSockets[name]);
  148. http.get({
  149. agent: agentkeepalive,
  150. port,
  151. path: '/',
  152. }, res => {
  153. assert(res.statusCode === 200);
  154. const chunks = [];
  155. res.on('data', data => {
  156. chunks.push(data);
  157. });
  158. res.on('end', () => {
  159. const data = JSON.parse(Buffer.concat(chunks));
  160. remotePort = data.socket.port;
  161. assert(data.headers.connection === 'keep-alive');
  162. assert(agentkeepalive.sockets[name]);
  163. assert(!agentkeepalive.freeSockets[name]);
  164. setTimeout(() => {
  165. assert(!agentkeepalive.sockets[name]);
  166. assert(agentkeepalive.freeSockets[name]);
  167. assert(agentkeepalive.freeSockets[name].length === 1);
  168. done();
  169. }, 20);
  170. });
  171. });
  172. const status = agentkeepalive.getCurrentStatus();
  173. assert(status.createSocketCount === 1);
  174. assert(status.timeoutSocketCount === 0);
  175. assert(status.sockets[name] === 1);
  176. assert(!status.freeSockets[name]);
  177. });
  178. it('should request again and use the same socket', done => {
  179. const name = 'localhost:' + port + ':';
  180. assert(!agentkeepalive.sockets[name]);
  181. assert(agentkeepalive.freeSockets[name]);
  182. assert(agentkeepalive.freeSockets[name].length === 1);
  183. http.get({
  184. agent: agentkeepalive,
  185. port,
  186. path: '/foo',
  187. }, res => {
  188. assert(res.statusCode === 200);
  189. const chunks = [];
  190. res.on('data', data => {
  191. chunks.push(data);
  192. });
  193. res.on('end', () => {
  194. const data = JSON.parse(Buffer.concat(chunks));
  195. assert(data.socket.port === remotePort);
  196. assert(agentkeepalive.sockets[name]);
  197. assert(!agentkeepalive.freeSockets[name]);
  198. setTimeout(() => {
  199. const status = agentkeepalive.getCurrentStatus();
  200. assert(status.createSocketCount === 1);
  201. assert(status.closeSocketCount === 0);
  202. assert(status.timeoutSocketCount === 0);
  203. assert(status.requestCount === 2);
  204. assert(!status.sockets[name]);
  205. assert(status.freeSockets[name]);
  206. assert(status.freeSockets[name] === 1);
  207. done();
  208. }, 10);
  209. });
  210. });
  211. assert(agentkeepalive.sockets[name]);
  212. assert(agentkeepalive.sockets[name].length === 1);
  213. assert(!agentkeepalive.freeSockets[name]);
  214. });
  215. it('should remove keepalive socket when server side destroy()', done => {
  216. done = pedding(2, done);
  217. const name = 'localhost:' + port + ':';
  218. assert(!agentkeepalive.sockets[name]);
  219. assert(agentkeepalive.freeSockets[name].length === 1);
  220. // should emit agent close event too
  221. agentkeepalive.once('close', done);
  222. const req = http.get({
  223. agent: agentkeepalive,
  224. port,
  225. path: '/error',
  226. }, () => {
  227. throw new Error('should not call this');
  228. });
  229. req.on('error', err => {
  230. assert(err.message === 'socket hang up');
  231. assert(agentkeepalive.sockets[name].length === 1);
  232. assert(!agentkeepalive.freeSockets[name]);
  233. setTimeout(() => {
  234. assert(!agentkeepalive.sockets[name]);
  235. assert(!agentkeepalive.freeSockets[name]);
  236. done();
  237. }, 10);
  238. });
  239. assert(agentkeepalive.sockets[name].length === 1);
  240. assert(!agentkeepalive.freeSockets[name]);
  241. });
  242. it('should remove socket when socket.destroy()', done => {
  243. const name = 'localhost:' + port + ':';
  244. assert(!agentkeepalive.sockets[name]);
  245. assert(!agentkeepalive.freeSockets[name]);
  246. http.get({
  247. agent: agentkeepalive,
  248. port,
  249. path: '/',
  250. }, res => {
  251. assert(res.statusCode === 200);
  252. res.resume();
  253. res.on('end', () => {
  254. assert(agentkeepalive.sockets[name].length === 1);
  255. assert(!agentkeepalive.freeSockets[name]);
  256. setTimeout(() => {
  257. assert(!agentkeepalive.sockets[name]);
  258. assert(agentkeepalive.freeSockets[name].length === 1);
  259. agentkeepalive.freeSockets[name][0].destroy();
  260. setTimeout(() => {
  261. assert(!agentkeepalive.sockets[name]);
  262. assert(!agentkeepalive.freeSockets[name]);
  263. done();
  264. }, 10);
  265. }, 10);
  266. });
  267. }).on('error', done);
  268. });
  269. it('should use new socket when hit the max keepalive time: 1000ms', done => {
  270. const name = 'localhost:' + port + ':';
  271. assert(!agentkeepalive.sockets[name]);
  272. assert(!agentkeepalive.freeSockets[name]);
  273. http.get({
  274. agent: agentkeepalive,
  275. port,
  276. path: '/',
  277. }, res => {
  278. assert(res.statusCode === 200);
  279. let lastPort = null;
  280. res.on('data', data => {
  281. data = JSON.parse(data);
  282. lastPort = data.socket.port;
  283. assert(lastPort > 0);
  284. });
  285. res.on('end', () => {
  286. assert(agentkeepalive.sockets[name].length === 1);
  287. assert(!agentkeepalive.freeSockets[name]);
  288. // free keepAlive socket timeout and destroy
  289. setTimeout(() => {
  290. assert(!agentkeepalive.sockets[name]);
  291. assert(!agentkeepalive.freeSockets[name]);
  292. http.get({
  293. agent: agentkeepalive,
  294. port,
  295. path: '/',
  296. }, res => {
  297. assert(res.statusCode === 200);
  298. res.on('data', data => {
  299. data = JSON.parse(data);
  300. assert(data.socket.port > 0);
  301. assert(data.socket.port !== lastPort);
  302. });
  303. res.on('end', done);
  304. });
  305. }, 2000);
  306. });
  307. });
  308. });
  309. it('should disable keepalive when keepAlive=false', done => {
  310. const name = 'localhost:' + port + ':';
  311. const agent = new Agent({
  312. keepAlive: false,
  313. });
  314. assert(agent.keepAlive === false);
  315. http.get({
  316. agent,
  317. port,
  318. path: '/',
  319. }, res => {
  320. assert(res.statusCode === 200);
  321. res.on('data', data => {
  322. assert(JSON.parse(data).headers.connection === 'close');
  323. });
  324. res.on('end', () => {
  325. assert(agent.sockets[name].length === 1);
  326. assert(!agent.freeSockets[name]);
  327. setTimeout(() => {
  328. assert(!agent.sockets[name]);
  329. assert(!agent.freeSockets[name]);
  330. done();
  331. }, 10);
  332. });
  333. });
  334. });
  335. it('should not keepalive when client.abort()', done => {
  336. const name = 'localhost:' + port + ':';
  337. assert(!agentkeepalive.sockets[name]);
  338. const req = http.get({
  339. agent: agentkeepalive,
  340. port,
  341. path: '/',
  342. }, () => {
  343. throw new Error('should not call this.');
  344. });
  345. req.on('error', err => {
  346. assert(err.message, 'socket hang up');
  347. assert(!agentkeepalive.sockets[name]);
  348. assert(!agentkeepalive.freeSockets[name]);
  349. done();
  350. });
  351. process.nextTick(() => {
  352. req.abort();
  353. });
  354. assert(agentkeepalive.sockets[name].length === 1);
  355. });
  356. it('should keep 1 socket', done => {
  357. const name = 'localhost:' + port + ':';
  358. const agent = new Agent({
  359. maxSockets: 1,
  360. maxFreeSockets: 1,
  361. });
  362. let lastPort = null;
  363. http.get({
  364. agent,
  365. port,
  366. path: '/',
  367. }, res => {
  368. assert(agent.sockets[name].length === 1);
  369. assert(agent.requests[name].length === 1);
  370. assert(res.statusCode === 200);
  371. res.on('data', data => {
  372. data = JSON.parse(data);
  373. lastPort = data.socket.port;
  374. assert(lastPort > 0);
  375. });
  376. res.on('end', () => {
  377. // should be reuse
  378. process.nextTick(() => {
  379. assert(agent.sockets[name].length === 1);
  380. assert(!agent.freeSockets[name]);
  381. });
  382. });
  383. });
  384. http.get({
  385. agent,
  386. port,
  387. path: '/',
  388. }, res => {
  389. assert(agent.sockets[name].length === 1);
  390. assert(!agent.requests[name]);
  391. assert(res.statusCode === 200);
  392. res.on('data', data => {
  393. data = JSON.parse(data);
  394. assert(data.socket.port === lastPort);
  395. });
  396. res.on('end', () => {
  397. setTimeout(() => {
  398. // should keepalive 1 socket
  399. assert(!agent.sockets[name]);
  400. assert(agent.freeSockets[name].length === 1);
  401. done();
  402. }, 10);
  403. });
  404. });
  405. // has 1 request pedding in the requests queue
  406. assert(agent.requests[name].length === 1);
  407. });
  408. it('should keep 1 free socket', done => {
  409. const name = 'localhost:' + port + ':';
  410. const agent = new Agent({
  411. maxSockets: 2,
  412. maxFreeSockets: 1,
  413. });
  414. let lastPort = null;
  415. http.get({
  416. agent,
  417. port,
  418. path: '/',
  419. }, res => {
  420. assert(agent.sockets[name]);
  421. assert(res.statusCode === 200);
  422. res.on('data', data => {
  423. data = JSON.parse(data);
  424. lastPort = data.socket.port;
  425. assert(lastPort > 0);
  426. });
  427. res.on('end', () => {
  428. // should be reuse
  429. setTimeout(() => {
  430. assert(agent.freeSockets[name].length === 1);
  431. }, 100);
  432. });
  433. });
  434. http.get({
  435. agent,
  436. port,
  437. path: '/',
  438. }, res => {
  439. assert(agent.sockets[name]);
  440. assert(res.statusCode === 200);
  441. res.on('data', data => {
  442. data = JSON.parse(data);
  443. assert(data.socket.port !== lastPort);
  444. });
  445. res.on('end', () => {
  446. setTimeout(() => {
  447. // should keepalive 1 socket
  448. assert(!agent.sockets[name]);
  449. assert(agent.freeSockets[name].length === 1);
  450. done();
  451. }, 100);
  452. });
  453. });
  454. assert(!agent.requests[name]);
  455. });
  456. it('should keep 2 free socket', done => {
  457. done = pedding(2, done);
  458. const name = 'localhost:' + port + ':';
  459. const agent = new Agent({
  460. maxSockets: 2,
  461. maxFreeSockets: 2,
  462. });
  463. let lastPort = null;
  464. http.get({
  465. agent,
  466. port,
  467. path: '/',
  468. }, res => {
  469. assert(agent.sockets[name].length);
  470. assert(res.statusCode === 200);
  471. res.on('data', data => {
  472. data = JSON.parse(data);
  473. lastPort = data.socket.port;
  474. assert(lastPort > 0);
  475. });
  476. res.on('end', () => {
  477. // should be reuse
  478. process.nextTick(() => {
  479. assert(agent.freeSockets[name]);
  480. done();
  481. });
  482. });
  483. });
  484. http.get({
  485. agent,
  486. port,
  487. path: '/',
  488. }, res => {
  489. assert(agent.sockets[name].length);
  490. assert(res.statusCode === 200);
  491. res.on('data', data => {
  492. data = JSON.parse(data);
  493. assert(data.socket.port !== lastPort);
  494. });
  495. res.on('end', () => {
  496. setTimeout(() => {
  497. // should keepalive 2 free sockets
  498. assert(!agent.sockets[name]);
  499. assert(agent.freeSockets[name].length === 2);
  500. done();
  501. }, 10);
  502. });
  503. });
  504. assert(!agent.requests[name]);
  505. });
  506. it('should request /remote_close 200 status, after 500ms free socket close', done => {
  507. const name = 'localhost:' + port + ':';
  508. assert(!agentkeepalive.sockets[name]);
  509. http.get({
  510. agent: agentkeepalive,
  511. port,
  512. path: '/remote_close',
  513. }, res => {
  514. assert(res.statusCode === 200);
  515. res.resume();
  516. res.on('end', () => {
  517. assert(agentkeepalive.sockets[name]);
  518. assert(!agentkeepalive.freeSockets[name]);
  519. setTimeout(() => {
  520. assert(!agentkeepalive.sockets[name]);
  521. assert(!agentkeepalive.freeSockets[name]);
  522. done();
  523. }, 550);
  524. });
  525. });
  526. });
  527. it('should fire timeout callback', done => {
  528. done = pedding(2, done);
  529. const lastStatus = agentkeepalive.getCurrentStatus();
  530. http.get({
  531. agent: agentkeepalive,
  532. port,
  533. path: '/',
  534. }, res => {
  535. assert(res.statusCode === 200);
  536. res.resume();
  537. res.on('end', () => {
  538. const req = http.get({
  539. agent: agentkeepalive,
  540. port,
  541. path: '/hang',
  542. }, () => {
  543. throw new Error('should not call this');
  544. });
  545. req.setTimeout(400, () => {
  546. const status = agentkeepalive.getCurrentStatus();
  547. assert(status.timeoutSocketCount - lastStatus.timeoutSocketCount === 1);
  548. setTimeout(done, 300);
  549. });
  550. req.on('error', err => {
  551. assert(err.message === 'socket hang up');
  552. done();
  553. });
  554. });
  555. });
  556. });
  557. it('should free socket timeout and emit agent timeout event', done => {
  558. done = pedding(2, done);
  559. const name = 'localhost:' + port + ':';
  560. const agent = new Agent({
  561. keepAliveTimeout: 1000,
  562. });
  563. agent.on('timeout', done);
  564. let lastPort = null;
  565. http.get({
  566. agent,
  567. port,
  568. path: '/',
  569. }, res => {
  570. assert(agent.sockets[name].length === 1);
  571. assert(res.statusCode === 200);
  572. res.on('data', data => {
  573. data = JSON.parse(data);
  574. lastPort = data.socket.port;
  575. assert(lastPort > 0);
  576. });
  577. res.on('end', () => {
  578. process.nextTick(() => {
  579. assert(!agent.sockets[name]);
  580. assert(agent.freeSockets[name].length === 1);
  581. // free socket timeout after 1s
  582. setTimeout(() => {
  583. assert(!agent.freeSockets[name]);
  584. done();
  585. }, 1100);
  586. });
  587. });
  588. });
  589. });
  590. it('should working socket timeout and emit agent timeout event', done => {
  591. done = pedding(2, done);
  592. const name = 'localhost:' + port + ':';
  593. const agent = new Agent({
  594. timeout: 1000,
  595. });
  596. agent.on('timeout', done);
  597. http.get({
  598. agent,
  599. port,
  600. path: '/hang',
  601. }, () => {
  602. throw new Error('should not run this');
  603. }).on('error', err => {
  604. assert(err.message === 'socket hang up');
  605. assert(err.code === 'ECONNRESET');
  606. assert(!agent.sockets[name]);
  607. done();
  608. });
  609. assert(agent.sockets[name].length === 1);
  610. });
  611. it('should destroy free socket before timeout', done => {
  612. const name = 'localhost:' + port + ':';
  613. const agent = new Agent({
  614. keepAliveTimeout: 1000,
  615. });
  616. let lastPort = null;
  617. http.get({
  618. agent,
  619. port,
  620. path: '/',
  621. }, res => {
  622. assert(agent.sockets[name].length === 1);
  623. assert(res.statusCode === 200);
  624. res.on('data', data => {
  625. data = JSON.parse(data);
  626. lastPort = data.socket.port;
  627. assert(lastPort > 0);
  628. });
  629. res.on('end', () => {
  630. process.nextTick(() => {
  631. assert(!agent.sockets[name]);
  632. assert(agent.freeSockets[name].length === 1);
  633. agent.freeSockets[name][0].destroy();
  634. setTimeout(() => {
  635. assert(!agent.freeSockets[name]);
  636. done();
  637. }, 10);
  638. });
  639. });
  640. });
  641. assert(agent.sockets[name].length === 1);
  642. });
  643. it('should remove error socket and create new one handle pedding request', done => {
  644. done = pedding(2, done);
  645. const name = 'localhost:' + port + ':';
  646. const agent = new Agent({
  647. keepAliveTimeout: 1000,
  648. maxSockets: 1,
  649. maxFreeSockets: 1,
  650. });
  651. let lastPort = null;
  652. http.get({
  653. agent,
  654. port,
  655. path: '/error',
  656. }, () => {
  657. throw new Error('never run this');
  658. }).on('error', err => {
  659. assert(err.message === 'socket hang up');
  660. }).on('close', () => done());
  661. http.get({
  662. agent,
  663. port,
  664. path: '/',
  665. }, res => {
  666. assert(agent.sockets[name].length === 1);
  667. assert(res.statusCode === 200);
  668. res.on('data', data => {
  669. data = JSON.parse(data);
  670. lastPort = data.socket.port;
  671. assert(lastPort > 0);
  672. });
  673. res.on('end', () => {
  674. process.nextTick(() => {
  675. assert(!agent.sockets[name]);
  676. assert(agent.freeSockets[name].length === 1);
  677. done();
  678. });
  679. });
  680. });
  681. assert(agent.requests[name].length === 1);
  682. });
  683. it('should destroy all sockets', done => {
  684. done = pedding(2, done);
  685. const name = 'localhost:' + port + ':';
  686. const agent = new Agent({
  687. keepAliveTimeout: 1000,
  688. });
  689. let lastPort = null;
  690. http.get({
  691. agent,
  692. port,
  693. path: '/',
  694. }, res => {
  695. http.get({
  696. agent,
  697. port,
  698. path: '/',
  699. }).on('error', err => {
  700. assert(err.message === 'socket hang up');
  701. setTimeout(() => {
  702. assert(!agent.sockets[name]);
  703. assert(!agent.freeSockets[name]);
  704. done();
  705. }, 10);
  706. });
  707. assert(res.statusCode === 200);
  708. res.on('data', data => {
  709. data = JSON.parse(data);
  710. lastPort = data.socket.port;
  711. assert(lastPort > 0);
  712. });
  713. res.on('end', () => {
  714. agent.destroy();
  715. done();
  716. });
  717. });
  718. });
  719. it('should keep max sockets: bugfix for orginal keepalive agent', _done => {
  720. const agentkeepalive = new Agent({
  721. keepAlive: true,
  722. keepAliveMsecs: 1000,
  723. maxSockets: 2,
  724. maxFreeSockets: 2,
  725. });
  726. const done = pedding(2, err => {
  727. assert(!err);
  728. const pool = agentkeepalive.sockets[Object.keys(agentkeepalive.sockets)[0]];
  729. assert(!pool);
  730. // all sockets on free list now
  731. const freepool = agentkeepalive.freeSockets[Object.keys(agentkeepalive.freeSockets)[0]];
  732. assert(freepool.length === 2);
  733. _done();
  734. });
  735. http.get({
  736. agent: agentkeepalive,
  737. port,
  738. path: '/',
  739. }, res => {
  740. assert(res.statusCode === 200);
  741. res.resume();
  742. res.on('end', () => {
  743. const pool = agentkeepalive.sockets[Object.keys(agentkeepalive.sockets)[0]];
  744. assert(pool);
  745. setTimeout(done, 10);
  746. });
  747. });
  748. http.get({
  749. agent: agentkeepalive,
  750. port,
  751. path: '/',
  752. }, res => {
  753. assert(res.statusCode === 200);
  754. res.resume();
  755. res.on('end', () => {
  756. const pool = agentkeepalive.sockets[Object.keys(agentkeepalive.sockets)[0]];
  757. assert(pool);
  758. setTimeout(done, 10);
  759. });
  760. });
  761. });
  762. it('should timeout and remove free socket', done => {
  763. done = pedding(2, done);
  764. const _keepaliveAgent = new Agent({
  765. maxSockets: 1,
  766. maxFreeSockets: 1,
  767. keepAliveTimeout: 1000,
  768. });
  769. const options = {
  770. hostname: 'registry.npmjs.org',
  771. port: 80,
  772. path: '/',
  773. method: 'GET',
  774. agent: _keepaliveAgent,
  775. };
  776. let index = 0;
  777. const getRequest = () => {
  778. const currentIndex = index++;
  779. const req = http.request(options, res => {
  780. let size = 0;
  781. res.on('data', chunk => {
  782. size += chunk.length;
  783. });
  784. res.on('end', () => {
  785. console.log('#%d req end, size: %d', currentIndex, size);
  786. done();
  787. });
  788. });
  789. req.on('error', done);
  790. return req;
  791. };
  792. const req = getRequest();
  793. // Get a reference to the socket.
  794. req.on('socket', sock => {
  795. // Listen to timeout and send another request immediately.
  796. sock.on('timeout', () => {
  797. console.log('socket:%s timeout', sock._host);
  798. getRequest().end();
  799. });
  800. });
  801. req.end();
  802. });
  803. it('should not open more sockets than maxSockets when request success', done => {
  804. done = pedding(3, done);
  805. const name = 'localhost:' + port + ':';
  806. const agentkeepalive = new Agent({
  807. keepAlive: true,
  808. keepAliveTimeout: 1000,
  809. maxSockets: 1,
  810. maxFreeSockets: 1,
  811. });
  812. http.get({
  813. agent: agentkeepalive,
  814. port,
  815. path: '/hello1',
  816. }, res => {
  817. let info;
  818. assert(res.statusCode === 200);
  819. res.on('data', data => {
  820. info = JSON.parse(data);
  821. });
  822. res.on('end', () => {
  823. assert(info.url === '/hello1');
  824. assert(agentkeepalive.sockets[name].length === 1);
  825. done();
  826. });
  827. res.resume();
  828. });
  829. http.get({
  830. agent: agentkeepalive,
  831. port,
  832. path: '/hello2',
  833. }, res => {
  834. let info;
  835. assert(res.statusCode === 200);
  836. res.on('data', data => {
  837. info = JSON.parse(data);
  838. });
  839. res.on('end', () => {
  840. assert(info.url === '/hello2');
  841. assert(agentkeepalive.sockets[name].length === 1);
  842. done();
  843. });
  844. res.resume();
  845. });
  846. http.get({
  847. agent: agentkeepalive,
  848. port,
  849. path: '/hello3',
  850. }, res => {
  851. let info;
  852. assert(res.statusCode === 200);
  853. res.on('data', data => {
  854. info = JSON.parse(data);
  855. });
  856. res.on('end', () => {
  857. assert(info.url === '/hello3');
  858. assert(agentkeepalive.sockets[name].length === 1);
  859. done();
  860. });
  861. res.resume();
  862. });
  863. assert(Object.keys(agentkeepalive.sockets).length === 1);
  864. assert(agentkeepalive.sockets[name].length === 1);
  865. });
  866. it('should not open more sockets than maxSockets when request timeout', done => {
  867. const name = 'localhost:' + port + ':';
  868. const agentkeepalive = new Agent({
  869. keepAlive: true,
  870. timeout: 1000,
  871. maxSockets: 1,
  872. maxFreeSockets: 1,
  873. });
  874. http.get({
  875. agent: agentkeepalive,
  876. port,
  877. path: '/hang',
  878. }, () => {
  879. throw new Error('should not run this');
  880. })
  881. .on('error', () => {
  882. assert(agentkeepalive.sockets[name].length === 1);
  883. done();
  884. });
  885. http.get({
  886. agent: agentkeepalive,
  887. port,
  888. path: '/hang',
  889. }, () => {
  890. throw new Error('should not run this');
  891. })
  892. .on('error', () => {
  893. // do noting
  894. });
  895. http.get({
  896. agent: agentkeepalive,
  897. port,
  898. path: '/hang',
  899. }, () => {
  900. throw new Error('should not run this');
  901. })
  902. .on('error', () => {
  903. // do noting
  904. });
  905. assert(Object.keys(agentkeepalive.sockets).length === 1);
  906. });
  907. describe('keepAlive = false', () => {
  908. it('should close socket after request', done => {
  909. const name = 'localhost:' + port + ':';
  910. const agent = new Agent({
  911. keepAlive: false,
  912. });
  913. http.get({
  914. agent,
  915. port,
  916. path: '/',
  917. }, res => {
  918. assert(res.statusCode === 200);
  919. res.resume();
  920. res.on('end', () => {
  921. setTimeout(() => {
  922. assert(!agent.sockets[name]);
  923. assert(!agent.freeSockets[name]);
  924. done();
  925. }, 10);
  926. });
  927. });
  928. });
  929. });
  930. describe('getCurrentStatus()', () => {
  931. it('should get current agent status', () => {
  932. const status = agentkeepalive.getCurrentStatus();
  933. assert.deepEqual(Object.keys(status), [
  934. 'createSocketCount', 'createSocketErrorCount', 'closeSocketCount',
  935. 'errorSocketCount', 'timeoutSocketCount',
  936. 'requestCount', 'freeSockets', 'sockets', 'requests',
  937. ]);
  938. });
  939. });
  940. describe('getter statusChanged', () => {
  941. it('should get statusChanged', () => {
  942. const agentkeepalive = new Agent({
  943. keepAliveTimeout: 1000,
  944. maxSockets: 5,
  945. maxFreeSockets: 5,
  946. });
  947. assert(agentkeepalive.statusChanged === false);
  948. assert(agentkeepalive.statusChanged === false);
  949. agentkeepalive.createSocketCount++;
  950. assert(agentkeepalive.createSocketCount !== agentkeepalive.createSocketCountLastCheck);
  951. assert(agentkeepalive.statusChanged === true);
  952. assert(agentkeepalive.createSocketCount === agentkeepalive.createSocketCountLastCheck);
  953. assert(agentkeepalive.statusChanged === false);
  954. agentkeepalive.createSocketErrorCount++;
  955. assert(agentkeepalive.createSocketErrorCount !== agentkeepalive.createSocketErrorCountLastCheck);
  956. assert(agentkeepalive.statusChanged === true);
  957. assert(agentkeepalive.createSocketErrorCount === agentkeepalive.createSocketErrorCountLastCheck);
  958. assert(agentkeepalive.statusChanged === false);
  959. agentkeepalive.closeSocketCount++;
  960. assert(agentkeepalive.closeSocketCount !== agentkeepalive.closeSocketCountLastCheck);
  961. assert(agentkeepalive.statusChanged === true);
  962. assert(agentkeepalive.closeSocketCount === agentkeepalive.closeSocketCountLastCheck);
  963. assert(agentkeepalive.statusChanged === false);
  964. agentkeepalive.errorSocketCount++;
  965. assert(agentkeepalive.errorSocketCount !== agentkeepalive.errorSocketCountLastCheck);
  966. assert(agentkeepalive.statusChanged === true);
  967. assert(agentkeepalive.errorSocketCount === agentkeepalive.errorSocketCountLastCheck);
  968. assert(agentkeepalive.statusChanged === false);
  969. agentkeepalive.timeoutSocketCount++;
  970. assert(agentkeepalive.timeoutSocketCount !== agentkeepalive.timeoutSocketCountLastCheck);
  971. assert(agentkeepalive.statusChanged === true);
  972. assert(agentkeepalive.timeoutSocketCount === agentkeepalive.timeoutSocketCountLastCheck);
  973. assert(agentkeepalive.statusChanged === false);
  974. agentkeepalive.requestCount++;
  975. assert(agentkeepalive.requestCount !== agentkeepalive.requestCountLastCheck);
  976. assert(agentkeepalive.statusChanged === true);
  977. assert(agentkeepalive.requestCount === agentkeepalive.requestCountLastCheck);
  978. assert(agentkeepalive.statusChanged === false);
  979. });
  980. });
  981. describe('mock idle socket error', () => {
  982. it('should idle socket emit error event', done => {
  983. const agent = new Agent();
  984. const options = {
  985. host: 'registry.npmjs.org',
  986. port: 80,
  987. path: '/',
  988. agent,
  989. };
  990. const socketKey = agent.getName(options);
  991. const req = http.get(options, res => {
  992. let size = 0;
  993. assert(res.headers.connection === 'keep-alive');
  994. res.on('data', chunk => {
  995. size += chunk.length;
  996. });
  997. res.on('end', () => {
  998. assert(size > 0);
  999. assert(Object.keys(agent.sockets).length === 1);
  1000. assert(Object.keys(agent.freeSockets).length === 0);
  1001. process.nextTick(() => {
  1002. assert(agent.freeSockets[socketKey].length === 1);
  1003. setTimeout(() => {
  1004. // agent should catch idle socket error event
  1005. agent.freeSockets[socketKey][0].emit('error', new Error('mock read ECONNRESET'));
  1006. setTimeout(() => {
  1007. // error socket should be destroy and remove
  1008. assert(Object.keys(agent.freeSockets).length === 0);
  1009. done();
  1010. }, 10);
  1011. }, 10);
  1012. });
  1013. });
  1014. res.resume();
  1015. });
  1016. req.on('error', done);
  1017. });
  1018. });
  1019. describe('options.socketActiveTTL', () => {
  1020. it('should expire active socket when it is out of ttl', done => {
  1021. const name = 'localhost:' + port + ':';
  1022. const agent = new Agent({
  1023. keepAlive: true,
  1024. keepAliveMsecs: 1000,
  1025. maxSockets: 5,
  1026. maxFreeSockets: 5,
  1027. timeout: 30000,
  1028. freeSocketKeepAliveTimeout: 5000,
  1029. socketActiveTTL: 500,
  1030. });
  1031. http.get({
  1032. agent,
  1033. port,
  1034. path: '/',
  1035. }, res => {
  1036. assert(res.statusCode === 200);
  1037. res.resume();
  1038. res.on('end', () => {
  1039. const firstCreatedTime = agent.sockets[name].pop().createdTime;
  1040. setTimeout(function() {
  1041. http.get({
  1042. agent,
  1043. port,
  1044. path: '/',
  1045. }, res => {
  1046. assert(res.statusCode === 200);
  1047. res.resume();
  1048. res.on('end', () => {
  1049. const currentCreatedTime = agent.sockets[name].pop().createdTime;
  1050. assert(firstCreatedTime < currentCreatedTime);
  1051. done();
  1052. });
  1053. });
  1054. }, 600);
  1055. });
  1056. });
  1057. });
  1058. it('should not expire active socket when it is in ttl', done => {
  1059. const name = 'localhost:' + port + ':';
  1060. const agent = new Agent({
  1061. keepAlive: true,
  1062. keepAliveMsecs: 1000,
  1063. maxSockets: 5,
  1064. maxFreeSockets: 5,
  1065. timeout: 30000,
  1066. freeSocketKeepAliveTimeout: 5000,
  1067. socketActiveTTL: 1000,
  1068. });
  1069. http.get({
  1070. agent,
  1071. port,
  1072. path: '/',
  1073. }, res => {
  1074. assert(res.statusCode === 200);
  1075. res.resume();
  1076. res.on('end', () => {
  1077. const firstCreatedTime = agent.sockets[name].pop().createdTime;
  1078. setTimeout(function() {
  1079. http.get({
  1080. agent,
  1081. port,
  1082. path: '/',
  1083. }, res => {
  1084. assert(res.statusCode === 200);
  1085. res.resume();
  1086. res.on('end', () => {
  1087. const currentCreatedTime = agent.sockets[name].pop().createdTime;
  1088. assert(firstCreatedTime === currentCreatedTime);
  1089. done();
  1090. });
  1091. });
  1092. }, 600);
  1093. });
  1094. });
  1095. });
  1096. });
  1097. });