0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/test/gc/test-http-client-timeout.js

69 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
2012-05-03 19:16:25 +02:00
// just like test/gc/http-client.js,
// but with a timeout set
const common = require('../common');
2012-05-03 19:16:25 +02:00
function serverHandler(req, res) {
setTimeout(function() {
req.resume();
res.writeHead(200);
2012-05-03 19:16:25 +02:00
res.end('hello\n');
}, 100);
}
const http = require('http');
const weak = require(`./build/${common.buildType}/binding`);
const todo = 550;
let done = 0;
let count = 0;
let countGC = 0;
2012-05-03 19:16:25 +02:00
console.log('We should do ' + todo + ' requests');
2012-05-03 19:16:25 +02:00
const server = http.createServer(serverHandler);
server.listen(0, getall);
2012-05-03 19:16:25 +02:00
function getall() {
if (count >= todo)
return;
2012-05-03 19:16:25 +02:00
(function() {
function cb(res) {
res.resume();
done += 1;
}
2012-05-03 19:16:25 +02:00
const req = http.get({
hostname: 'localhost',
pathname: '/',
port: server.address().port
}, cb);
req.on('error', cb);
req.setTimeout(10, function() {
console.log('timeout (expected)');
});
count++;
weak(req, afterGC);
})();
setImmediate(getall);
2012-05-03 19:16:25 +02:00
}
for (let i = 0; i < 10; i++)
getall();
function afterGC() {
countGC++;
2012-05-03 19:16:25 +02:00
}
setInterval(status, 100).unref();
2012-05-03 19:16:25 +02:00
function status() {
global.gc();
2012-05-03 19:16:25 +02:00
console.log('Done: %d/%d', done, todo);
console.log('Collected: %d/%d', countGC, count);
if (countGC === todo) server.close();
2012-05-03 19:16:25 +02:00
}