1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23// In previous versions of Node.js (e.g., 0.6.0), this sort of thing would halt 24// after http.globalAgent.maxSockets number of files. 25// See https://groups.google.com/forum/#!topic/nodejs-dev/V5fB69hFa9o 26const common = require('../common'); 27const fixtures = require('../common/fixtures'); 28const assert = require('assert'); 29const http = require('http'); 30const fs = require('fs'); 31const Countdown = require('../common/countdown'); 32 33http.globalAgent.maxSockets = 1; 34 35const tmpdir = require('../common/tmpdir'); 36tmpdir.refresh(); 37 38const image = fixtures.readSync('/person.jpg'); 39 40console.log(`image.length = ${image.length}`); 41 42const total = 10; 43const responseCountdown = new Countdown(total, common.mustCall(() => { 44 checkFiles(); 45 server.close(); 46})); 47 48const server = http.Server(function(req, res) { 49 setTimeout(function() { 50 res.writeHead(200, { 51 'content-type': 'image/jpeg', 52 'connection': 'close', 53 'content-length': image.length 54 }); 55 res.end(image); 56 }, 1); 57}); 58 59 60server.listen(0, function() { 61 for (let i = 0; i < total; i++) { 62 (function() { 63 const x = i; 64 65 const opts = { 66 port: server.address().port, 67 headers: { connection: 'close' } 68 }; 69 70 http.get(opts, function(res) { 71 console.error(`recv ${x}`); 72 const s = fs.createWriteStream(`${tmpdir.path}/${x}.jpg`); 73 res.pipe(s); 74 75 s.on('finish', function() { 76 console.error(`done ${x}`); 77 responseCountdown.dec(); 78 }); 79 }).on('error', function(e) { 80 console.error('error! ', e.message); 81 throw e; 82 }); 83 })(); 84 } 85}); 86 87function checkFiles() { 88 // Should see 1.jpg, 2.jpg, ..., 100.jpg in tmpDir 89 const files = fs.readdirSync(tmpdir.path); 90 assert(total <= files.length); 91 92 for (let i = 0; i < total; i++) { 93 const fn = `${i}.jpg`; 94 assert.ok(files.includes(fn), `couldn't find '${fn}'`); 95 const stat = fs.statSync(`${tmpdir.path}/${fn}`); 96 assert.strictEqual( 97 image.length, stat.size, 98 `size doesn't match on '${fn}'. Got ${stat.size} bytes`); 99 } 100} 101