11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ci// In previous versions of Node.js (e.g., 0.6.0), this sort of thing would halt
241cb0ef41Sopenharmony_ci// after http.globalAgent.maxSockets number of files.
251cb0ef41Sopenharmony_ci// See https://groups.google.com/forum/#!topic/nodejs-dev/V5fB69hFa9o
261cb0ef41Sopenharmony_ciconst common = require('../common');
271cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
281cb0ef41Sopenharmony_ciconst assert = require('assert');
291cb0ef41Sopenharmony_ciconst http = require('http');
301cb0ef41Sopenharmony_ciconst fs = require('fs');
311cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cihttp.globalAgent.maxSockets = 1;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
361cb0ef41Sopenharmony_citmpdir.refresh();
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst image = fixtures.readSync('/person.jpg');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciconsole.log(`image.length = ${image.length}`);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst total = 10;
431cb0ef41Sopenharmony_ciconst responseCountdown = new Countdown(total, common.mustCall(() => {
441cb0ef41Sopenharmony_ci  checkFiles();
451cb0ef41Sopenharmony_ci  server.close();
461cb0ef41Sopenharmony_ci}));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciconst server = http.Server(function(req, res) {
491cb0ef41Sopenharmony_ci  setTimeout(function() {
501cb0ef41Sopenharmony_ci    res.writeHead(200, {
511cb0ef41Sopenharmony_ci      'content-type': 'image/jpeg',
521cb0ef41Sopenharmony_ci      'connection': 'close',
531cb0ef41Sopenharmony_ci      'content-length': image.length
541cb0ef41Sopenharmony_ci    });
551cb0ef41Sopenharmony_ci    res.end(image);
561cb0ef41Sopenharmony_ci  }, 1);
571cb0ef41Sopenharmony_ci});
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciserver.listen(0, function() {
611cb0ef41Sopenharmony_ci  for (let i = 0; i < total; i++) {
621cb0ef41Sopenharmony_ci    (function() {
631cb0ef41Sopenharmony_ci      const x = i;
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci      const opts = {
661cb0ef41Sopenharmony_ci        port: server.address().port,
671cb0ef41Sopenharmony_ci        headers: { connection: 'close' }
681cb0ef41Sopenharmony_ci      };
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci      http.get(opts, function(res) {
711cb0ef41Sopenharmony_ci        console.error(`recv ${x}`);
721cb0ef41Sopenharmony_ci        const s = fs.createWriteStream(`${tmpdir.path}/${x}.jpg`);
731cb0ef41Sopenharmony_ci        res.pipe(s);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci        s.on('finish', function() {
761cb0ef41Sopenharmony_ci          console.error(`done ${x}`);
771cb0ef41Sopenharmony_ci          responseCountdown.dec();
781cb0ef41Sopenharmony_ci        });
791cb0ef41Sopenharmony_ci      }).on('error', function(e) {
801cb0ef41Sopenharmony_ci        console.error('error! ', e.message);
811cb0ef41Sopenharmony_ci        throw e;
821cb0ef41Sopenharmony_ci      });
831cb0ef41Sopenharmony_ci    })();
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci});
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_cifunction checkFiles() {
881cb0ef41Sopenharmony_ci  // Should see 1.jpg, 2.jpg, ..., 100.jpg in tmpDir
891cb0ef41Sopenharmony_ci  const files = fs.readdirSync(tmpdir.path);
901cb0ef41Sopenharmony_ci  assert(total <= files.length);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  for (let i = 0; i < total; i++) {
931cb0ef41Sopenharmony_ci    const fn = `${i}.jpg`;
941cb0ef41Sopenharmony_ci    assert.ok(files.includes(fn), `couldn't find '${fn}'`);
951cb0ef41Sopenharmony_ci    const stat = fs.statSync(`${tmpdir.path}/${fn}`);
961cb0ef41Sopenharmony_ci    assert.strictEqual(
971cb0ef41Sopenharmony_ci      image.length, stat.size,
981cb0ef41Sopenharmony_ci      `size doesn't match on '${fn}'. Got ${stat.size} bytes`);
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci}
101