2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-05-29 03:06:56 -04:00
|
|
|
require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
2012-09-09 14:32:40 +02:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let requests_sent = 0;
|
|
|
|
let requests_done = 0;
|
|
|
|
const options = {
|
2012-09-09 14:32:40 +02:00
|
|
|
method: 'GET',
|
2016-05-29 03:06:56 -04:00
|
|
|
port: undefined,
|
2012-09-28 09:55:29 -07:00
|
|
|
host: '127.0.0.1',
|
2012-09-09 14:32:40 +02:00
|
|
|
};
|
|
|
|
|
2018-11-21 00:41:14 +05:30
|
|
|
const server = http.createServer((req, res) => {
|
2016-01-13 21:42:45 +01:00
|
|
|
const m = /\/(.*)/.exec(req.url);
|
|
|
|
const reqid = parseInt(m[1], 10);
|
2016-01-19 03:23:07 +01:00
|
|
|
if (reqid % 2) {
|
2019-03-22 03:44:26 +01:00
|
|
|
// Do not reply the request
|
2012-09-09 14:32:40 +02:00
|
|
|
} else {
|
2017-07-10 20:55:21 -04:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2012-09-09 14:32:40 +02:00
|
|
|
res.write(reqid.toString());
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-29 03:06:56 -04:00
|
|
|
server.listen(0, options.host, function() {
|
|
|
|
options.port = this.address().port;
|
2012-09-09 14:32:40 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
for (requests_sent = 0; requests_sent < 30; requests_sent += 1) {
|
2017-04-28 04:06:42 +03:00
|
|
|
options.path = `/${requests_sent}`;
|
2018-04-06 20:03:14 +02:00
|
|
|
const req = http.request(options);
|
2012-09-09 14:32:40 +02:00
|
|
|
req.id = requests_sent;
|
2018-11-21 00:41:14 +05:30
|
|
|
req.on('response', (res) => {
|
2012-09-09 14:32:40 +02:00
|
|
|
res.on('data', function(data) {
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`res#${this.req.id} data:${data}`);
|
2012-09-09 14:32:40 +02:00
|
|
|
});
|
|
|
|
res.on('end', function(data) {
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`res#${this.req.id} end`);
|
2012-09-28 09:55:29 -07:00
|
|
|
requests_done += 1;
|
2018-04-06 20:03:14 +02:00
|
|
|
req.destroy();
|
2012-09-09 14:32:40 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
req.on('close', function() {
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`req#${this.id} close`);
|
2012-09-09 14:32:40 +02:00
|
|
|
});
|
|
|
|
req.on('error', function() {
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`req#${this.id} error`);
|
2012-09-09 14:32:40 +02:00
|
|
|
this.destroy();
|
|
|
|
});
|
2015-05-19 13:00:06 +02:00
|
|
|
req.setTimeout(50, function() {
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`req#${this.id} timeout`);
|
2016-12-30 15:42:06 -05:00
|
|
|
this.abort();
|
2012-09-28 09:55:29 -07:00
|
|
|
requests_done += 1;
|
2012-09-09 14:32:40 +02:00
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
}
|
2012-09-25 15:00:01 +09:00
|
|
|
|
2012-09-25 11:15:44 -07:00
|
|
|
setTimeout(function maybeDone() {
|
|
|
|
if (requests_done >= requests_sent) {
|
2018-11-21 00:41:14 +05:30
|
|
|
setTimeout(() => {
|
2012-09-25 11:15:44 -07:00
|
|
|
server.close();
|
|
|
|
}, 100);
|
|
|
|
} else {
|
|
|
|
setTimeout(maybeDone, 100);
|
|
|
|
}
|
|
|
|
}, 100);
|
2012-09-09 14:32:40 +02:00
|
|
|
});
|
|
|
|
|
2018-11-21 00:41:14 +05:30
|
|
|
process.on('exit', () => {
|
2018-01-10 15:05:15 +05:30
|
|
|
console.error(`done=${requests_done} sent=${requests_sent}`);
|
2018-12-10 13:27:32 +01:00
|
|
|
// Check that timeout on http request was not called too much
|
2018-03-27 11:21:05 +03:00
|
|
|
assert.strictEqual(requests_done, requests_sent);
|
2012-09-09 14:32:40 +02:00
|
|
|
});
|